In the spirit of questions like Do your loops test at the top or bottom?:
Which style do you use for an infinite loop, and why?
- while (true) { }
- do { } while (true);
- for (;;) { }
- label: ... goto label;
In the spirit of questions like Do your loops test at the top or bottom?:
Which style do you use for an infinite loop, and why?
That's how I roll.
I prefer
while(1)
orwhile(true)
-- it's the clearest.do { } while(true)
seems like needless obfuscation. Likewise,for(;;)
can be confusing to people that have never seen it before, whereaswhile(true)
is very intuitive. And there's absolutely no reason to dolabel: ... goto label;
, it's just more confusing.When writing code for myself I use for(;;). Other people tend to be confused by its syntax and so for code that other people must see/use, I use while(true).
I use
for (;;)
in C-style languages andwhile true
in languages that don't support that construct.I learned the
for (;;)
method in K&R and it has always felt like idiomatic C to me.