这是do-while循环的例子
Which example for a do-while loop?
在do-while循环和if else语句中,在语句结束时更改变量值并使用该值来创建while布尔表达式是否符合最佳利益,或者最好将while表达式设置为始终为真,然后在if else语句中使用break语句。
为了澄清,这里有两种方法。
do
{
cout << "What is your choice?" << endl;
cin >> choice;
if(choice == 1)
{
cout << "You chose 1" << endl;
doWhileModifier = 1;
} else {
cout << "That's not a correct choice" << endl;
doWhileModifier = 0;
}
}
while (doWhileModifier == 0);
和
do
{
cout << "What is your choice?" << endl;
cin >> choice;
if(choice == 1)
{
cout << "You chose 1" << endl;
break;
} else {
cout << "That's not a correct choice" << endl;
}
}
while (true);
我最初使用第一种方法,但现在我倾向于使用第二种方法。
非常感谢输入!
两个do-while循环都能完成任务,所以在代码中使用哪个do-while循环完全取决于您的个人偏好。但是如果你比我更喜欢第二个,我建议你只做一个while循环,而不是do-while循环,因为它更简单,更容易阅读,而且它做的事情和do-while循环是一样的。
相关文章:
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 如何使用 do while 循环确定最高值和最低值
- do while 循环中的 if 语句以 yes 或 no 结尾
- 使用 while 或 do while 退出循环
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 为什么我的 Do-while 循环无法在C++中运行菜单
- 如果语句不会在 do - while 循环中运行
- 如何在c++中读取do-while循环中的空白
- 将嵌套的 for 循环更改为 do-while 循环
- 如何修复不会在退出选择时终止的 do-while 循环
- 为什么这个do-while循环每隔一个循环只写入数组?
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- 卡在链表中的无限 Do-while 循环中
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 使用基本 do-while 循环求解泊松方程
- do-while循环不会退出C
- 如何在C++中使用带有"do while"循环的计数器?
- 无休止的 do while 循环C++代码