C语言 使用 while(true) 进行简单的 while 循环;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14934507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:23:09  来源:igfitidea点击:

Simple do while loop using while(true);

cloopsdo-while

提问by Dev

Many times in the examples of C programs, I came across these kind of loops. What do these kind of loops really do?

在 C 程序的例子中,我多次遇到过这种循环。这些循环到底有什么作用?

do {

    while (...) // Check some condition if it is true.
    { 
        calculation 1
    }

    // Some new condition is checked.

} while(true);

What is the need of while(true);Is it used for infinite looping? Can someone please explain what the above loop really does. I am new to C programming

需要什么while(true);是否用于无限循环?有人可以解释一下上面的循环到底做了什么。我是 C 编程的新手

回答by Sargam Modak

These loops are used when one wants to loop forever and the breaking out condition from loop is not known. Certiain conditions are set inside the loop along with either breakor returnstatements to come out of the loop. For example:

当想要永远循环并且不知道循环的中断条件时使用这些循环。某些条件在循环内设置,以及要退出循环的breakreturn语句。例如:

while(true){
    //run this code
    if(condition satisfies)
        break;    //return;
}

These loops are just like any other while loop with condition to stop the loop is in the body of the while loop otherwise it will run forever (which is never the intention of a part of the code until required so). It depends upon the logic of the programmer only what he/she wants to do.

这些循环就像任何其他 while 循环一样,带有停止循环的条件位于 while 循环的主体中,否则它将永远运行(这绝不是一部分代码的意图,除非需要)。它只取决于程序员的逻辑,他/她想做什么。

回答by Cris

yes it is used for infinite looping,in this case best practice is to break out of look on a condition

是的,它用于无限循环,在这种情况下,最佳实践是打破条件

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);

回答by Some programmer dude

In C all loops loop while a condition is true. So an explicit truein the condition really means "loop while trueis true" and so loops forever.

在 C 中,当条件为真时,所有循环都会循环。因此true,条件中的显式确实意味着“循环而true为真”,因此永远循环。

回答by Ivaylo Strandjev

This loop is infinite and if you what your program to ever terminate with such lop you need to have either a breakor a return(or in some cases throw an exception) statement under given condition in such loop otherwise such program will never terminate.

这个循环是无限的,如果你的程序以这样的 lop 终止,你需要在这样的循环中的给定条件下有一个break或一个return(或在某些情况下抛出异常)语句,否则这样的程序将永远不会终止。

回答by Bernd Elkemann

An infinite loop is useful if the check of the stop-condition can neither be done in front (as with forand while) nor at the back (as with do{}while). Instead you just loop forever and in the middle of the code you can check a condition and break: if(something) break;.

如果停止条件的检查既不能在前面(如forwhile)也不能在后面(如do{}while)完成,则无限循环很有用。相反,您只是永远循环,在代码中间,您可以检查条件并中断:if(something) break;

回答by La VloZ

sometimes we use it for example :

有时我们使用它,例如:

do
     recv(s , &buf, len, flags);
while(true)

an example from winsock windows api, by this way you can listen from a port.

来自 winsock windows api 的示例,通过这种方式您可以从端口侦听。

回答by krato

do {
  // code here
} while(true);

This loop runs infinitely, and it may result into an runtime erorr if not stopped. If you are doing these kinds of loop, be sure to have a break statement inside to assure that your loop will stop at some point.

这个循环无限运行,如果不停止,可能会导致运行时错误。如果您正在执行这些类型的循环,请确保在其中包含一个 break 语句,以确保您的循环会在某个时刻停止。

Similar to this

类似于这个

if(condition)
   break;

If your program reached some point where condition is true, it will automatically end the do-while loop and proceed to the code after that.

如果您的程序达到某个条件为真,它将自动结束 do-while 循环并继续执行之后的代码。

回答by paxdiablo

The general differentiating factor between the following loops:

以下循环之间的一般区分因素:

while (condition) {action}
do {action} while (condition)

is that the former is used for loops that happen zeroor more times whilst the latter is for loops that happen oneor more time.

是前者用于发生次或多次的循环,而后者用于发生一次或多次的循环。

In other words, the condition for whileis checked at the start of the loop, and for do while, it's checked at the end.

换句话说,条件 forwhile在循环开始时检查,而 fordo while则在循环结束时检查。

Often you'll see code where the developers don't seem to knowabout do-whilein that they'll write:

你会经常看到代码开发商似乎哪里不以了解有关do-while的,因为它们会写:

result = doSomething();
while (result == NOT_FINISHED) {
    result = doSomething();
}

which could be better written as:

可以更好地写为:

do {
    result = doSomething();
} while (result == NOT_FINISHED);


However, in your specific case where the condition is always true, it doesn't really matter. The following loops are basically equivalent (using 1for the true case):

但是,在条件始终为 的特定情况下true,这并不重要。以下循环基本上是等效的(1用于真实情况):

for (;;) { doSomething(); }
for (;;doSomething());

while (1) { doSomething(); }
do { doSomething(); } while (1);

while (doSomething(),1);

BADPAX: doSomething(); goto BADPAX;

The first forloop is probably the canonical way of doing an infinite loop, taking advantage of the fact that, if you omit the continuation condition for the loop, it assumes it to be always true.

第一个for循环可能是执行无限循环的规范方式,它利用了这样一个事实:如果您省略循环的继续条件,则它假定它始终为真。

The second forloop is just moving the loop body into the per-iteration part of the forstatement.

第二个for循环只是将循环体移动到for语句的每次迭代部分。

The first whileis also sometimes seen in the wild, the do-whileprobably less so. The only distinction here is that the former loops forever checking at the loop top, the latter loops forever checking at the loop bottom.

while一种有时也出现在野外,do-while可能不太常见。这里唯一的区别是前者循环永远在循环顶部检查,后者循环永远在循环底部检查。

The final whileloop is using the C comma operator in a way you probably never should :-)

最后一个while循环以一种您可能永远不应该使用的方式使用 C 逗号运算符:-)

That last one is very rare nowadays but is probably what they all optimise down to at the machine code level.

最后一个现在非常罕见,但可能是他们都在机器代码级别优化的。