11

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

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Dev
  • 153
  • 1
  • 1
  • 6
  • 3
    I've never seen a `do while(true)`. It's an infinite loop, but the idiomatic (and clear) way to write one if `for (;;)`. – Fred Foo Feb 18 '13 at 10:52

8 Answers8

7

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.

Sargam Modak
  • 743
  • 8
  • 25
1

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);
Cris
  • 12,799
  • 5
  • 35
  • 50
1

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    In `for(;;)` where or what is the condition? :-) – Jens Feb 18 '13 at 11:06
  • @Jens I only have access to the C++11 spec, but it says that a `for` loop is equivalent to a `while` loop, and §2 of section 6.5.3 says "A missing condition makes the implied `while` Clause equivalent to `while(true)`" – Some programmer dude Feb 18 '13 at 11:12
  • 1
    @Jens In 6.8.5.3 (2): "An omitted _expression-2_ is replaced by a nonzero constant", where _expression-2_ is the loop condition. So the condition is implicitly inserted if you don't write one (but of course under the as-if rule, you'll get an unconditional jump often). – Daniel Fischer Feb 18 '13 at 12:26
0

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

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

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

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
0

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.

La VloZ
  • 102
  • 10
0
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.

krato
  • 1,226
  • 4
  • 14
  • 30