To my way of thinking, a for loop is used to iterate over a known or determinable range.

String[] names = //something;
for ( int i = 0; i < names.length; i++ ) { //do stuff }

which is equivalent (scoping of i aside) to :

String[] names = //something;
int i = 0;
while (i < names.length )
   // do stuff

In other words, the for loop is simply a (highly useful) syntactic sugar for a commonly used while construct.

However, I'm seeing a lot of for(;;) constructs on the web which are functionally equivalent to while(true)

What is the reasoning for this? Why would the infinite for loop be preferred over the infinite while loop?

// I even saw a java textbook that didn't use while loops at all! Leading to such monstrous constructs as:

String input = getInput();
for( ; !inputIsValid(input) ; )
  • The main reason is preference. Readability should come into play when making the choice however YMMV. Nov 10 '11 at 15:39
  • Why would anyone prefer the awkward construct? Nov 10 '11 at 15:41
  • 10
    You could swap while and for here and the question wouldn't change. while(true) and for(;;) mean the same thing. You obviously have a strong preference for while, others may have an equally strong preference for for. It's impossible to say that one is more correct than the other.
    – Caleb
    Nov 10 '11 at 16:12
  • 3
    @chris, I don't find for(;;) confusing at all. It's a standard C idiom, one which you'll documented in section 3.5 of K&R(2e). I understand that you don't like it; you should understand that others obviously prefer it (else you'd never see it). It may be more or less acceptable in languages other than C; you've tagged this language-agnostic which only decreases the possiblity of a definitive answer. Again, I voted to close because the Q isn't constructive; if I were offended I would have flagged as offensive instead of or in addition to closing. That is all.
    – Caleb
    Nov 10 '11 at 16:55
  • 1
    Personally I think there's a real opportunity here to have another syntax entirely for infinite loops: something like wheeeeeeee { ... }
    – detly
    Nov 11 '11 at 1:40

It's a hold-over from old programming practices on the PDP-11 (yes, I said old). It used to save a single instruction, which was useful for making loops run faster.

See the following for additional information: http://www.flounder.com/exceptions.htm

  • 3
    That's precisely what I was looking for. There is a real, legitimate reason, but it is no longer valid. Nov 10 '11 at 16:47
  • 1
    @chris Nnnope, modern compilers will sometimes complain about using a constant in the condition of a loop. It's not purely a holdover.
    – Izkata
    Nov 10 '11 at 21:25
  • There is more to it than that. I can remember writing C in the early 1990's on unix systems and while(true) {} was not an option. There was no standard boolean type, but several unix systems defined their own in C header files. From memory at least one unix vendor defined TRUE as zero supposidly to aid to process function returns as the convention was to return zero on success, and positive numbers were failures. This did mean that while(TRUE) {} was not portable. Nov 10 '11 at 21:40
  • 1
    I can't find it right now, but I could swear that a few years ago Dennis Ritchie wrote a post on Usenet stating that this was simply wrong, and he/they use the for(;;) syntax as (in their opinion) a more direct statement of intent that there was no criteria being stated for exit from the loop. Nov 10 '11 at 22:04
  • @Ptolemy: Well you could certainly have written while(1) { }.
    – Ed S.
    Nov 11 '11 at 1:04

some compilers will give a warning (something like conditional expression is constant) when using while( 1 ) but with for( ; ; ) there's nothing to warn about. Programmers want code without warnings, so they use the for variant.

  • Some things compilers warn about are perfectly valid, though, and even (in some special cases) unavoidable. So some of us treat warnings as, well, warnings. If you ignore them, of course you can get swamped in the damned things and not see the important ones - but there's pragmas and options to disable warnings, locally or globally. Basically, yes it's better to not have warnings, but I usually need a stronger reason than that to adopt a worse code style. Here it's not worse just different, but "infinite" loops are rare (or else very bad style) anyway.
    – Steve314
    Nov 11 '11 at 20:54

It's a habit acquired from C programming where there isn't a boolean type. While(1) would be the equivalent potentially but For(;;) is often used as it shows up in K&R if I remember correctly. I suspect that there was a hardware reason in there somewhere as well.

  • 4
    ...suspect there was a hardware reason... Nov 10 '11 at 16:07
  • 2
    As Edward Robertson mentioned, the reason is because the C compiler on the PDP-11 didn't realize that the true in "while(true)" was a compile time constant and would add an extra instruction (a compare) when generating assembly. Because of the limited resources available on the PDP-11, programmers used the for loop to optimize that one extra instruction out of the program.
    – Jetti
    Nov 10 '11 at 16:08

for(;;) can be read as "forever" which some find more natural than "while true".

  • While this isn't the original reason, it is a secondary reason why the preference stuck long after the original reason became obsolete. It's very hard to trot out any evidence to support this, though, other than to ask long-experienced programmers.
    – DarenW
    Nov 11 '11 at 2:47

All the experienced programmers I have asked can recognize for(;;) more quickly than while(true) or while(1).

  • 2
    Any programmer that's worth a damn should be able to recognize all three instantly. All three are common enough that anyone who's spent more than 5 minutes reading code has seen each of them a bunch of times.
    – cHao
    Nov 11 '11 at 3:38
  • 1
    Studies have shown that experienced programmers can comprehend for(;;) 14ms faster than while(1) :-) Nov 11 '11 at 12:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.