views:

134

answers:

5

I see the different conventions used in many books I had read, where you would create infinite loops with either loop structure such as:

while()
   foo();
for(;;)
   foo();

But really, what are the differences I should know about? which one is better?

A: 

The first one will not compile. You need at least: while( true ). They are semantically equivalent. It is a matter of style/personal choice.

dirkgently
+1  A: 

There's no difference.

Except in the while loop, you have to put some true condition there, e.g. while(1).

See also: http://stackoverflow.com/questions/2611246/is-for-faster-than-while-true-if-not-why-do-people-use-it

Also, the "better" one might be the one that isn't infinite. :)

quixoto
Good find with the other question.
Nullw0rm
+5  A: 

They're semantically the equivalent. (x;y;z) { foo; } is equivalent to x; while (y) { foo; z; }. They're not exactly equivalent in further versions of the standard, in the example of for (int x = 0; y; z), the scope of x is the for block and is out of scope after the loop ends, whereas with int x; while (y) x it's still in scope after the loop ends.

Another difference is that for interprets missing y as TRUE, whereas while must be supplied with an expression. for (;;) { foo; } is fine, but while() { foo; } isn not.

Nullw0rm
FYI, question is asking about infinite loops, not the semantics of for vs while in general.
quixoto
If you want to get around the locality of statement `x`, you might say `for (x; y; z) { foo; }` is equivalent to `{ x; while (y) { foo; z; } }`.
Jon Purdy
As for the semantics, C language standard for for-loops allows one to construct a for-loop which is identical to that of a while-loop. This, however, conflicts with the deeper semantic understanding of a for-loop, which invariably contains the loop variant (which is often defined to be identically positive for convenience reasons).For consistent semantics, a clear separation between loops where the iteration count is known before the execution of the loop (for-loops), and loops where the iteration count is not known before execution (while-loops) should be made.
Schedler
A: 

Here is one small difference I saw with the VS2010 disassembly in debug mode. Not sure, if it is sufficient enough to count as a significant and universally true difference (across all compiler and with all optimizations).

So conceptually these loops are same, but at a processor level, with infinite message loops, the clock cycles for the additional/different instructions could be different and make some difference.

   while(1)
004113DE  mov         eax,1                       **// This is the difference**
004113E3  test        eax,eax                     **// This is the difference**
004113E5  je          main+2Eh (4113EEh)  
      f();
004113E7  call        f (4110DCh)  
004113EC  jmp         main+1Eh (4113DEh)          **// This is the difference**
   for(;;)
      f();
004113EE  call        f (4110DCh)  
004113F3  jmp         main+2Eh (4113EEh)          **// This is the difference**
} 
Chubsdad
This difference definitely does not exist with any optimizations enabled.
Ben Voigt
The difference is only because debug mode has optimizations turned off by default. The first 3 lines are actually checking if `1 != 0`, i.e. if the condition is true.
casablanca
And the difference in `jmp` is just a slightly different offset.
tc.
+1  A: 

They are the same. However, I prefer "for(;;)" loop because "while(1)" loop gives you a compiler warning whereas "for(;;)" gives you no warnings (in case of Microsoft Visual C++).

EJP