2

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?

Community
  • 1
  • 1
Mr. X
  • 29
  • 1
  • 2
  • 1
    It sounds like this has been covered before: http://stackoverflow.com/questions/2288856/when-implementing-an-infinite-loop-is-there-a-difference-in-using-while1-vs-fo, http://stackoverflow.com/questions/885908/while-1-vs-for-is-there-a-speed-difference, http://stackoverflow.com/questions/1379246/any-reason-to-replace-whilecondition-with-forcondition-in-c – gnovice Aug 06 '10 at 15:45
  • Sometimes I write `while 1+1=2 do` (Delphi syntax) just because it looks cool (and is very clear, intuitively). I suppose the optimizing compiler realizes that `1+1=2` is the constant expression `true`. :) – Andreas Rejbrand Aug 06 '10 at 15:46
  • @gnovice: Those are not duplicates, they deal with implementation and performance; while this question opens up the debate for "style". This should be reopened. – Jordão Aug 06 '10 at 16:04
  • @Jordão: The question just asks "which should I use?", but gives no criteria (style or performance). If the focus is performance, it's been covered before. If the focus is style, it's pretty subjective, and SO isn't really a place for such debate or discussion. – gnovice Aug 06 '10 at 16:15
  • This is curious. Maybe the other ones should be closed, because this one is broader. But that's not how it works is it? – Jordão Aug 06 '10 at 16:26
  • Let's say that I ask an even broader question: __"What are the ways and tradeoffs (performance, readability, etc) to implement infinite loops in C-style languages (C, C++, Java, C#, ...)?"__ Would that be deemed inappropriate and closed as well? – Jordão Aug 06 '10 at 16:37

7 Answers7

13

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
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I thought so too, but some people here think that `for (;;)` is clearer. I was wondering why... – Mr. X Aug 06 '10 at 15:03
  • No idea. Maybe they like that it's shorter. – SLaks Aug 06 '10 at 15:03
  • 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 Aug 06 '10 at 15:12
  • 3
    [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 Aug 06 '10 at 15:16
  • 7
    `#define ever ;;` – Jon Purdy Aug 06 '10 at 15:16
  • 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 ;) – ravibhagw Aug 06 '10 at 15:30
  • @baultista: that was a commented-out block of code in VB, which lacks a multiline comment construct. Very ugly indeed. – Jordão Aug 06 '10 at 16:07
10
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.

Nobody
  • 4,731
  • 7
  • 36
  • 65
  • 2
    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 Aug 06 '10 at 15:15
4

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
  • 55,340
  • 13
  • 112
  • 144
  • "do { } while" is more difficult to read than "while { }" in my opinion, and should be avoided as much as possible – ggarber Aug 06 '10 at 16:24
  • @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 Aug 06 '10 at 16:41
3

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
  • 6,837
  • 31
  • 44
2

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
  • 105,238
  • 25
  • 256
  • 415
2

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

tenfour
  • 36,141
  • 15
  • 83
  • 142
1

A question I asked recently: 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.

Community
  • 1
  • 1
Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188