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?
Asked
Active
Viewed 144 times
-1
Josh Crozier
- 233,099
- 56
- 391
- 304
Hidro636
- 35
- 3
-
1Ideally both are same as infinite loop. – Braj May 25 '14 at 18:05
-
Look at the Related column over on the right --> – ClickRick May 25 '14 at 18:05
-
If Iwere a compiler, I would generate the exact same assembly for both. – Jonathon Reinhart May 25 '14 at 18:07
-
I **am** a compiler, and I **do** generate the exact same assembly for both. – ClickRick May 25 '14 at 18:08
-
1does anyone noticed that this question specifies no language ? How you can offer even an opinion starting from such a question. – user2485710 May 25 '14 at 18:11
-
@user2485710 It's tagged as C – Andro May 25 '14 at 18:35
-
@CandyMan it was edited, the question still doesn't specifies anything . The original question was not tagged `C` or any other language. – user2485710 May 25 '14 at 18:42
2 Answers
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