Skip to content
Theory · Loops · lesson 5 of 8

break and continue

So far the runner has run laps until the condition says "enough". But sometimes you need to stop in the middle of a workout. The runner is looking for a friend in the stands: once they see the friend, they leave the track, since there is no point in running on. Or there is a puddle on the track: the runner cuts the rest of the lap across the grass and starts the next one at once.

A loop that searches for something should also stop as soon as it finds it. The break statement ends a loop at once. This program looks for the first number from 100 that divides by 13 with no remainder:

JAVA
public class Main {
    public static void main(String[] args) {
        for (int n = 100; n <= 200; n++) {
            if (n % 13 == 0) {
                System.out.println(n); // 104
                break;
            }
        }
    }
}

Let's walk through the iterations:

  1. For n from 100 to 103 the remainder n % 13 is not zero, and the loop goes on.
  2. At 104 the remainder is zero: 104 is 8 * 13. The program prints 104.
  3. break ends the loop, and the numbers after 104 are never checked.

Leaving the loop, skipping a lap

Both statements go inside the loop body, almost always inside an if. They differ in what they cut short: the whole loop or one iteration.

break: leave the track

break ends the whole loop. The rest of the body and all the following iterations are skipped, and execution moves to the first statement after the loop. You have already met break in switch, and it did the same thing there: it led outside.

Without break, the program above would check every number up to 200 and also print 117, 130 and so on up to 195.

break is often placed in a while (true) loop. Its condition is always true, and you leave such a loop with break. This form is handy when the decision to leave is made in the middle of the body:

JAVA
int n = 1;
while (true) {
    n = n * 2;
    if (n > 100) {
        break;
    }
    System.out.print(n + " "); // 2 4 8 16 32 64
}

Let's see what happens here:

  1. Each iteration doubles n: 2, 4, 8 and so on.
  2. While n is not greater than 100, break does not fire, and the program prints the number.
  3. At 128 the break fires, and printing never happens.

continue: straight to the next lap

continue skips only the rest of the current iteration, and the loop goes on. In a for, the update runs after continue, and then the condition is checked, the same as after a normal end of the body. The runner cuts the rest of the lap, but the number on the board still goes up.

JAVA
for (int i = 1; i <= 6; i++) {
    if (i % 3 == 0) {
        continue;
    }
    System.out.print(i + " "); // 1 2 4 5
}

Let's see what happens:

  1. For 3 and 6 the condition i % 3 == 0 is true, and continue skips the printing.
  2. The other numbers are printed: 1 2 4 5.
  3. The loop goes all the way to 6, as it would without continue.

If you replace continue with break, the program prints only 1 2: the loop ends when i is 3.

continue is handy when unsuitable values must be skipped and the rest processed. Interviewers often show a loop with both continue and break and ask what it prints.

Common mistake

In a while loop, continue comes before the line that changes the variable:

JAVA
int i = 0;
while (i < 5) {
    if (i == 2) {
        continue; // ✗
    }
    System.out.print(i + " ");
    i++;
}

The program prints 0 1 and hangs. When i is 2, continue skips the rest of the body, and i++ with it. i does not change, the condition is true again, and the loop gets stuck on the two for good.

Here the board picture falls short. In a for, the header's update raises the number on the board, and continue does not skip it. In a while, the update is an ordinary line of the body, and continue jumps over it. Increase i before continue, or rewrite the loop as a for.

Key points

  • break ends the whole loop at once, and execution moves to the statement after it.
  • continue skips the rest of the current iteration, and the loop goes on.
  • In a for, the header's update runs after continue, while in a while, continue can jump over the line that changes the variable.
  • You leave a while (true) loop with break.