I have to agree with sbi here - I like the middle-checking loop term. This kind of construct was more popular when Structured Programming started to roll and many languages had syntactic support for them.
That said, it is now widespread knowledge that while
loops are usually more mantainable, since it is easier to reason about invariants and they often handle the tricky empty case better.
In your particular case your loop is just equivalent to
for(; doSomething(), !condition(); doSomethingElse()){}
so I would only use the break
version if either doSomething
or doSomethingElse
involved multiple statements and I'd rather not put them away into separate functions like you did.
That said, if your loop is more complicated then a (start, check, increment) iteration then you should consider refactoring it into something simpler.
condition()
always returns false? I'd say it's an infinite loop with conditional breaks. – JohnL Dec 09 '11 at 09:30break
, the loop is not infinite (kill
, ctrl-alt-del, unplug...). So why bother with terminology details? – mouviciel Dec 09 '11 at 09:37Also see is while(true) bad programming practice?
– Bratch Dec 09 '11 at 16:26for(;;) { ... }
has no condition check and might run slightly faster (depending on your compiler or execution environment). – Roy Tinker Dec 09 '11 at 18:02for(;;) {}
doesn't throw warnings in the compiler, butwhile(true) {}
might, due to having a constant as the condition. Depends on the compiler and warning level. – Izkata Dec 10 '11 at 03:44while(true)...
is what in programming is called a thread. – Niklas Rosencrantz Dec 13 '11 at 17:00