6

Possible Duplicate:
for ( ; ; ) or while ( true ) - Which is the Correct C# Infinite Loop?
Why choosing for (;;){} over while(1)?

What's the difference between while(true), while(1), and for(;;)? They all are infinite loops in languages like C# and C/C++. But is one better than the other one? Any ideas?

Community
  • 1
  • 1
  • See http://stackoverflow.com/questions/1401159/for-or-while-true-which-is-the-correct-c-sharp-infinite-loop?rq=1. Note the answer which demonstrates that the c# compiler will turn `for` into `while`. – Tim M. Dec 28 '12 at 02:14
  • And http://stackoverflow.com/questions/13545237/why-choosing-for-over-while1 for C++. – jogojapan Dec 28 '12 at 02:15
  • See [this](http://stackoverflow.com/questions/1401159/for-or-while-true-which-is-the-correct-c-sharp-infinite-loop) or [this](http://stackoverflow.com/questions/2288856/when-implementing-an-infinite-loop-is-there-a-difference-in-using-while1-vs-f) or [this](http://stackoverflow.com/questions/3505755/best-infinite-loop) or [this](http://stackoverflow.com/questions/10114122/which-infinite-loop-is-preferable-to-use). – chris Dec 28 '12 at 02:15

1 Answers1

14

There is no difference once the program is compiled.

Here are some excerpts from three C programs and the corresponding generated assembly for all of them.

Let's try the for loop first:

#include <stdio.h>
int main(){
   for(;;)
      printf("This is a loop\n");
   return 0;
}

Now we will try the while loop:

#include <stdio.h>
int main(){
   while(1)
      printf("This is a loop\n");
   return 0;
}

A terrible solution, the goto loop:

#include <stdio.h>
int main(){
   alpha:
      printf("This is a loop\n");
      goto alpha;
   return 0;
}

Now, if we examine the generated assemblies, using the command gcc -S loop.c, they all look like this (I didn't see any reason to post them separately, since they are identical):

   .file "loop.c"
   .section .rodata
.LC0:
   .string  "This is a loop"
   .text
.globl main
   .type main, @function
main:
   leal  4(%esp), %ecx
   andl  $-16, %esp
   pushl -4(%ecx)
   pushl %ebp
   movl  %esp, %ebp
   pushl %ecx
   subl  $4, %esp
.L2:
   movl  $.LC0, (%esp)
   call  puts
   jmp   .L2
   .size main, .-main
   .ident   "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"
   .section .note.GNU-stack,"",@progbits

This part is the loop. It declares a label, copies the address to the string into a register, calls a routine called puts, and jumps back to the label:

.L2:
   movl  $.LC0, (%esp)
   call  puts
   jmp   .L2

Since they all do exactly the same thing, clearly there is no technical advantage of any of them (at least if you are using gcc).

However, people have opinions, and may favor one over the others for whatever reason. Since for(;;) is only seven characters long, it is easier to type (this is my preference). On the other hand, while(1) gives the illusion of a test which always evaluates to true, which some may find more intuitive. Only a few crazy people like the goto solution best.

Edit: Apparently some compilers might produce a warning for while(1) because the condition is always true, but such warnings can be easily disabled and have no effect on the generated assembly.

ctype.h
  • 1,470
  • 4
  • 20
  • 34
  • 5
    @Cheersandhth.-Alf I guess you have a point. I was using `gcc`, which does not issue such warnings. Perhaps I'll have try it in Visual Studio. I used the generated assembly to back up my claim that all of those ways of creating infinite loops do exactly the same thing, as I feel that such claims should be backed up by hard evidence rather than pulled out of nowhere. – ctype.h Dec 28 '12 at 02:44
  • I've heard about cases where a compiler might warn about `while (1)` because the condition is always true, while it won't warn about `for (;;)` because there is no (explicit) condition. – Keith Thompson Mar 24 '13 at 04:47