Is "for(;;)" faster than "while (TRUE)"? If not, why do people use it?
- It's not faster.
- If you really care, compile with assembler output for your platform and look to see.
- It doesn't matter. This never matters. Write your infinite loops however you like.
I prefer for(;;)
for two reasons.
One is that some compilers produce warnings on while(true)
(something like "loop condition is constant"). Avoiding warnings is always a good thing to do.
Another is that I think for(;;)
is clearer and more telling.I want an infinite loop. It literally has no condition, it depends on nothing. I just want it to continue forever, until I do something to break out of it.
Whereas with while(true)
, well, what's true got to do with anything? I'm not interested in looping until true becomes false, which is what this form literally says (loop while true is true). I just want to loop.
And no, there is absolutely no performance difference.
Personally I use for (;;)
because there aren't any numbers in it, it's just a keyword. I prefer it to while (true)
, while (1)
, while (42)
, while (!0)
etc etc.