views:

288

answers:

7

Possible Duplicate:
When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?

In languages where while (true) or for (;;) both mean "loop forever", which should I use?

+10  A: 

In my opinion, while(true) is clearer.

Any half-decent compiler will compile both identically (at least with optimizations).
Therefore, you should choose whichever form you find clearer.

SLaks
I thought so too, but some people here think that `for (;;)` is clearer. I was wondering why...
Mr. X
No idea. Maybe they like that it's shorter.
SLaks
It is sead that `for(;;)` should be read as `forever` but I find that you have to keep explaining that to people whereas `while(true)` is intuitively understood.
slebetman
[Repeating a comment made at the answer from rmx] Before true/false was introduced, it was "while(1)" which is less obvious, and visually not that different from "while(i)". OTOH, "for(;;)" stands out visually. (After introducing the "true" keyword and with syntax-coloring, this argument has lost its validity)
Sjoerd
`#define ever ;;`
Jon Purdy
I once saw a piece of code where the developer wrote "do while 1<>2", although I shouldn't have been surprised. After all, the developer was using VB ;)
baultista
@baultista: that was a commented-out block of code in VB, which lacks a multiline comment construct. Very ugly indeed.
Jordão
+1  A: 

I prefer while(true) because I think it is more intuitive, elegant and philosophically interesting.

However it is ultimately just a matter of style.

mikera
+3  A: 

In order of importance:

  1. Whatever your current code style guide says
  2. Whatever is in use in the current code
  3. Whatever your manager prefers
  4. Whatever your co-workers prefer
  5. Whatever you prefer

Order of 3 and 4 could be reversed in some circumstances ;)

EDIT: I personally prefer "while (true)" (including space), but I seldomly arrive at point 5 in the list.

Sjoerd
I have found before that manager's coding preferences must carry no weight.
Joshua
@Joshua: They do carry some weight, as to what you should _not_ do....
Jordão
+8  A: 
for(;;)

has no obvious semantic value. Whereas

while(true)

could pretty much be understood by any reasonably intelligent non-programmer due to being far closer to the natural-language equivalent.

rmx
Before true/false was introduced, it was "while(1)" which is less obvious, and visually not that different from "while(i)". OTOH, "for(;;)" stands out visually. (After introducing the "true" keyword and with syntax-coloring, this argument has lost its validity).
Sjoerd
A: 

A question I asked recently: http://stackoverflow.com/questions/3347477/difference-between-for-and-while-true-in-c

Although mine was language specific, several of them expressed views on this very subject.

Personally, I prefer while (true) because I feel like for (;;) is leaving out almost necessary pieces. I'm a bit OCD about it, and I know they're not NECESSARY, but I feel that a for without parameters just looks incomplete. Obviously, there are situations where you don't want some of the parameters, but while (true) looks more syntactically complete to me in situations where a loop like this is warranted.

All in all, it's pretty moot (in a good way). Developers you work with should be able to understand that they're the same thing, and (at least in C# according to my question) they compile to the exact same thing. Anybody that complains because you did one instead of the other seems like they have their priorities a touch out of order.

Corey Ogburn
+1  A: 

any decent developer will pass over both without getting confused. use whatever you feel like.

tenfour
+1  A: 

I prefer

do {
  // ....
} while(true);

It demotes the implementation choice for the infinite loop concept from the prominent place of the start of the loop.

If this is C++ or C, just create your own macro to abstract the concept better (in this case, the use of while(TRUE) or for(;;) is not that relevant):

#define forever for(;;)

This can be somehow adapted to C# too:

  forever: {
    // ...
    goto forever;
  }

(goto is not evil in this case)

Jordão
"do { } while" is more difficult to read than "while { }" in my opinion, and should be avoided as much as possible
gustavogb
@gustavogb: Me too, it's just that for infinite loops I prefer `do {} while(true)`. But I'd have no problem using `while(true)` as well.
Jordão