How does a for loop work, specifically for(;;)?

49,628

Solution 1

A for loop in java has the following structure -

for (initialization statement; condition check; update)
    loop body;

As you can see, there are four statements here -

  1. Initialization statement: This statement is executed only once, when the loop is entered for the first time. This is an optional statement, meaning you can choose keep this field blank. It is generally used for some initialization purpose.
  2. Conditional check: This statement is probably the most important one. It checks to verify whether or not certain expression evaluates to true. If it is, then the loop execution continues. You can choose to keep this field empty, which will be evaluated to true.
  3. Update: This statement list is executed from left to right, typically used to increment/decrement some variable.
  4. loop body: The body of the loop, which will be executed again and again based on the conditional check's truth value.

Basically this is how the execution follows - first, when the loop is entered for the first time, the initialization statement is executed once. Then the conditional check is executed to see if it evaluated to true. If it is, then the the loop body is executed, otherwise the loop execution is finished. After that, the Update statement(s) is(are) executed. Next, the conditional check is executed again, and if it evaluates to true, then again the loop body is executed, then update statement is executed, then again the conditional check....you get the picture.

Now about your for( ; ; ) syntax. It has no initialization statement, so nothing will be executed. Its conditional check statement is also empty, which means it evaluates to true after that the loop body is executed. Next, since the update statement is empty, nothing is executed. Then the conditional check is performed again which will again evaluates to true and then this whole process will again repeat.

So you see, this is basically an infinite loop which has no initialization statement, whose conditional check will always evaluates to true, and which has no update statement. This is equivalent to -

while(true)
{
    .....
}

which is another popular loop construct in java.

When you use an infinite loop like this, it's important pay attention to the breaking condition as in most cases you can't let a loop to run indefinitely. To break out of these kinds of loops, you can use the break statement. The structure is as follows -

if(some_condition_is_true)
    break;        // This will cause execution to break out of its nearest loop

or

if(some_condition_is_false)
    break;

Solution 2

This is the same as:

while(true) {
  //Some Stuff
}

Basically, an alternate syntax for an infinite loop.

Solution 3

These are all infinite loops

for(;;) {
   // endlessly
}

while(true) {
   // endlessly
}

do {
   // endlessly
} while(true);

Solution 4

This loop has no guard and acts as a while(true) loop. It will loop infinitely until a break.

Solution 5

It's an infinite loop. Not exactly good coding because it isn't intuitive that is would actually compile or not throw a runtime error. Rewriting as while(true) { /* code */ } would be much more readable to indicate a infinite loop.

Share:
49,628
Frank
Author by

Frank

Learning as I go

Updated on July 05, 2022

Comments

  • Frank
    Frank almost 2 years

    Looking through some old company code, I came across a for loop that looks like this:

    for (;;) {
        //Some stuff
    }
    

    I tried Google but couldn't find any answers. Did I fall asleep in a programming class or is this an unusual loop?

  • Aram Kocharyan
    Aram Kocharyan almost 13 years
    Although we're taught to avoid using infinite loops, I'm guilty of a while(true) on more than one occasion.
  • user489041
    user489041 almost 13 years
    I feel like they have their moments where they are useful
  • Travis Nelson
    Travis Nelson almost 13 years
    you are right that my code would not pickup the closing bracket as written because of the inline comment, but I just wanted to have it all on one line.
  • PeterT
    PeterT over 11 years
    At least if you use while(true) the person reading the code seems more likely to understand it...
  • Andy Guibert
    Andy Guibert over 8 years
    Just wasted 5 minutes reading the accepted answer when you explained it in 5 seconds.
  • Wes
    Wes about 7 years
    I shouldn't imagine this stack overflows as there is no stack going back to the beginning of a loop.
  • Rodney
    Rodney over 5 years
    or a throw, or a continue to an outer loop