简体   繁体   中英

Simple do while loop using while(true);

Many times in the examples of C programs, I came across these kind of loops. 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); Is it used for infinite looping? Can someone please explain what the above loop really does. I am new to C programming

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. 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). 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. So an explicit true in the condition really means "loop while true is true" and so loops forever.

此循环是无限的,如果您要在这种情况下终止程序,则需要在给定条件下具有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 ). Instead you just loop forever and in the middle of the code you can check a condition and 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.

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.

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.

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.

Often you'll see code where the developers don't seem to know about do-while in that they'll write:

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 1 for the true case):

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.

The second for loop is just moving the loop body into the per-iteration part of the for statement.

The first while is also sometimes seen in the wild, the do-while probably 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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