-1

I know both of these create intentional infinite loops, and I have a friend who bugs me to no end about using for(;;) as opposed to while(true). Is there any difference between the two besides while(true) being more commonly accepted as the "correct" syntax, i.e. memory usage or cycle speed?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Hidro636
  • 35
  • 3

2 Answers2

3

They are exactly the same: Inspecting the generated assembly file (using -S) by GCC, one can see that the compiler generates identical instructions for both.

for loop:

.L2:
    jmp .L2

while loop:

.L4:
    jmp .L4
Andro
  • 2,232
  • 1
  • 27
  • 40
2

There is no functional difference between the two statements. while(true) might be a little clearer for a new user to understand, whereas for(;;) is a bit more cryptic. The compiler, however, reads them as basically the same.

The effect of them is to create an infinite loop.

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47