Menu

Java For Loop

The for loop has three sections:

for (int i = 0; i < 3; i++) {}

First section runs once when we enter the loop.

Second section is the gate keeper, if it returns true, we run the statements in the loop, if it returns false, we exit the loop. It runs right after the first section for the first time, then every time the loop is finished and the third section is run.

The third section is the final statement that will run every time the loop runs.

So in the case we have just seen, the loop will run 3 times. Here is the order of the commands:

int i = 0;
i < 3 // 0 < 3 = true
      // Inside of loop
i++ // i is now 1
i < 3 // 1 < 3 = true
      // Inside of loop
i++ // i is now 2
i < 3 // 2 < 3 = true
      // Inside of loop
i++ // i is now 3
i < 3 // 3 < 3 = false
      // Loop is done...

We can omit the first and third section of the loop (although it will be weird), and the loop will still work:

for (;i < 5;) {}

For cases where we want to use a loop that look like that, we use a while loop.

Leave a Reply

Your email address will not be published. Required fields are marked *