很多时候在C程序的例子中,我遇到过这种循环.这些循环真正做了什么?
do {
while (...) // Check some condition if it is true.
{
calculation 1
}
// Some new condition is checked.
} while(true);
Run Code Online (Sandbox Code Playgroud)
有什么需要while(true);用于无限循环?有人可以解释上面的循环真正做了什么.我是C编程的新手
当一个人想要永远循环并且循环中的爆发条件未知时,使用这些循环.Certiain条件在循环内设置,以及从循环中出来的break或return语句.例如:
while(true){
//run this code
if(condition satisfies)
break; //return;
}
Run Code Online (Sandbox Code Playgroud)
这些循环就像任何其他循环一样,循环具有停止循环的条件在while循环的主体中,否则它将永远运行(在所需的情况下,这绝不是代码的一部分).这取决于程序员的逻辑,只取决于他/她想做什么.
是的,它用于无限循环,在这种情况下,最佳实践是打破条件查看
do {
while () //check some condition if it is true
{
calculation 1
}
//some new condition is checked,if condition met then break out of loop
} while(true);
Run Code Online (Sandbox Code Playgroud)