简体   繁体   English

使用while(true)进行do while循环简单;

[英]Simple do while loop using while(true);

Many times in the examples of C programs, I came across these kind of loops. 在C程序示例中,很多次我都遇到了这种循环。 What do these kind of loops really do? 这些循环实际上是做什么的?

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); 有什么需要while(true); Is it used for infinite looping? 它用于无限循环吗? Can someone please explain what the above loop really does. 有人可以解释一下上述循环的实际作用。 I am new to C programming 我是C编程新手

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 break or return statements to come out of the loop. 在循环内部设置Certiain条件,以及将要退出循环的breakreturn语句。 For example: 例如:

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). 这些循环与任何其他有条件停止循环的while循环一样,都位于while循环的主体中,否则它将永远运行(除非需要,否则这永远不是一部分代码的意图)。 It depends upon the logic of the programmer only what he/she wants to do. 它仅取决于编程人员的逻辑,他/她想做什么。

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

In C all loops loop while a condition is true. 在C中,当条件为真时,所有循环都会循环。 So an explicit true in the condition really means "loop while true is true" and so loops forever. 因此,条件中的显式true确实意味着“循环,而true为true”,因此永远循环。

此循环是无限的,如果您要在这种情况下终止程序,则需要在给定条件下具有breakreturn (或在某些情况下引发异常)语句,否则该程序将永远不会终止。

An infinite loop is useful if the check of the stop-condition can neither be done in front (as with for and while ) nor at the back (as with do{}while ). 如果停止条件的检查既不能在前面(如forwhile ),也不能在后面(如do{}whileforwhile无限循环很有用。 Instead you just loop forever and in the middle of the code you can check a condition and break: if(something) break; 取而代之的是,您永远循环下去,并在代码中间检查条件并中断: if(something) break; .

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的示例,通过这种方式,您可以从端口进行侦听。

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循环并在此之后继续执行代码。

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 zero or more times whilst the latter is for loops that happen one or more time. 是前者用于发生次或多次的循环,而后者用于发生一次或多次的循环。

In other words, the condition for while is checked at the start of the loop, and for do while , it's checked at the end. 换句话说,对于条件while在循环的开始检查,并do while ,它在端氏检查。

Often you'll see code where the developers don't seem to know about do-while in 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. 但是,在您的条件始终为true特定情况下,这并不重要。 The following loops are basically equivalent (using 1 for the true case): 以下循环基本上是等效的(在真实情况下使用1 ):

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

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

while (doSomething(),1);

BADPAX: doSomething(); goto BADPAX;

The first for loop 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 for loop is just moving the loop body into the per-iteration part of the for statement. 第二个for循环只是将循环主体移到for语句的per-iteration部分。

The first while is also sometimes seen in the wild, the do-while probably less so. 有时在野外也能看到头whiledo-while可能少见。 The only distinction here is that the former loops forever checking at the loop top, the latter loops forever checking at the loop bottom. 这里唯一的区别是,前者在循环顶部永远循环检查,后者在循环底部永远循环检查。

The final while loop 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. 如今,最后一个非常罕见,但可能是它们都在机器代码级别进行了优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM