Skip to content
Theory · Loops · lesson 2 of 6

The for loop, break and continue

In the previous lesson the counter lived in three places: the start value before while, the condition in parentheses and the step $i++ at the end of the body. A for loop gathers all three parts into one line.

On the running track, these are the start line, the checkpoint arch before each lap and the mark on the counter after the lap. In the for header they stand side by side, separated by semicolons:

PHP
<?php

$total = 0;
for ($i = 1; $i <= 4; $i++) {
    $total += $i;
}
echo $total, "\n"; // 10
echo $i, "\n"; // 5

The three parts of the header

The header for ($i = 1; $i <= 4; $i++) has three parts:

  • $i = 1 runs once, before the first iteration — the runner steps onto the start line;
  • $i <= 4 is checked before each iteration — this is the checkpoint arch;
  • $i++ runs after each iteration — this is the mark on the counter.

Let's walk through the program step by step:

  1. $total = 0; sets up the sum.
  2. The loop runs iterations with $i equal to 1, 2, 3 and 4 and adds them up: $total becomes 10.
  3. After the fourth iteration, the step makes $i equal to 5, the condition 5 <= 4 is false, and the loop ends.
  4. The two echo statements print 10 and 5.

The variable $i does not disappear after the loop: in PHP it keeps the value that made the condition false. for is the choice when you know in advance where to count from and to, and while when the number of iterations depends on what happens in the body.

Two ways to cut an iteration short

Sometimes the runner leaves the track early or cuts the rest of a lap. A loop has two statements for this. Both are written on a line of their own, most often inside an if.

break

break ends the whole loop at once, and the program continues from the line after the loop. This loop looks for the first number greater than 50 that is divisible by 7:

PHP
for ($i = 51; $i <= 100; $i++) {
    if ($i % 7 === 0) {
        break;
    }
}
echo $i, "\n"; // 56

For the numbers from 51 to 55 the remainder is not zero, and the loop goes on. At 56 the break fires: the runner leaves the track, and the step $i++ no longer runs. That is why $i keeps the number that was found.

break sits inside the if, but it leaves the loop. An if is not a loop, so break ends the nearest loop or switch around it.

continue

continue skips only the rest of the current iteration. The runner cuts the rest of the lap but still makes the mark on the counter: after continue, the step $i++ runs, and then the condition is checked.

PHP
for ($i = 1; $i <= 7; $i++) {
    if ($i % 3 === 0) {
        continue;
    }
    echo $i; // 12457
}
echo "\n";

For 3 and 6 the echo is never reached, and the other numbers are printed in a row: 12457. The loop still runs to the end, up to 7.

Where continue and break jump

Let's put both statements into one program. This is where people go wrong most often: where continue jumps and what $i holds after break. Press "Next step" and watch $i in the {main} frame on the iterations where continue and break fire.

Livecontinue and break in a for loopstep 1 of 12 · frames: 1

$total = 0 sets up the sum.

Watch $i on the iterations with continue and break

The rule the widget shows:

  • continue jumps to the step $i++, and then to the condition;
  • break leaves the loop at once, and the step no longer runs;
  • after break, $i keeps the value of the iteration on which the loop was cut short.

If loops are nested one inside another, break leaves only the inner one, and break 2 leaves both at once.

Here the picture with the mark holds only for for. In while the step $i++ sits in the body, and a continue written before it jumps over the step as well. Then $i does not change, and the loop repeats the same iteration forever.

Common mistake

A semicolon was put right after the for header:

PHP
for ($i = 1; $i <= 3; $i++); { // ✗
    echo $i, "\n";
}

The program prints a single line, 4, and PHP gives no message. The semicolon here is an empty body: the loop runs three empty laps, and the block in curly braces runs once, after the loop. By then $i holds 4. Remove the ;, and the loop prints 1, 2 and 3.

Key points

  • The for header has three parts: the start runs once, the condition before each iteration, the step after each iteration.
  • continue skips the rest of the iteration, and then the step and the condition check run.
  • break ends the loop at once, and the step $i++ no longer runs.
  • After the loop, $i holds its last value: the one that made the condition false or the one at which break fired.