6

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?

John
  • 3,238
  • 4
  • 22
  • 25

6 Answers6

7

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
  • 913
  • 1
  • 6
  • 13
  • 2
    FYI, question is asking about infinite loops, not the semantics of for vs while in general. – Ben Zotto Aug 21 '10 at 01:17
  • 3
    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 Aug 21 '10 at 01:29
  • 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 Aug 21 '10 at 03:14
  • Your answer is inaccurate. "(int x = 0; y; z), the scope of x is the for block and is out of scope after the loop ends" true in all versions. "int x; while (y) x"(syntax error) and will have locality of the parent of while in all versions. So that part didn't quite hit the mark. – iantonuk Mar 25 '16 at 02:47
2

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
  • 24,777
  • 4
  • 73
  • 129
  • 2
    This difference definitely does not exist with any optimizations enabled. – Ben Voigt Aug 21 '10 at 01:46
  • 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 Aug 21 '10 at 01:48
  • And the difference in `jmp` is just a slightly different offset. – tc. Aug 21 '10 at 01:57
  • I like your approach. That's the real-type answer but I'd rather added more assembler code with comments on lines. There's a lot going on in for loop. the dis-asm-ed code for for(;;) will never reflect it because your compiler **optimizes** it which kills the whole purpose. (For example while(0){..} will ne optimized to nothing). – iantonuk Mar 25 '16 at 02:54
1

There's no difference.

But

while() foo();

isn't the same that

for(;;foo();)

Remember! If you break the while before the foo() statement, foo() doesn't execute, but if you break the for, foo() executes...

1

There's no difference.

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

See also: 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. :)

Community
  • 1
  • 1
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
0

they're both the same.. modern compilers emit identical code for both.. interestingly (historically?) the for(;;) was more popular.. pascal programmers did a #define (;;) ever, and used forever {//code}

Karthik
  • 16
  • 2
0

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
  • 108,024
  • 16
  • 131
  • 187