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:
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:
- For
nfrom100to103the remaindern % 13is not zero, and the loop goes on. - At
104the remainder is zero:104is8 * 13. The program prints104. breakends the loop, and the numbers after104are 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:
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:
- Each iteration doubles
n:2,4,8and so on. - While
nis not greater than100,breakdoes not fire, and the program prints the number. - At
128thebreakfires, 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.
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:
- For
3and6the conditioni % 3 == 0is true, andcontinueskips the printing. - The other numbers are printed:
1 2 4 5. - The loop goes all the way to
6, as it would withoutcontinue.
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:
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
breakends the whole loop at once, and execution moves to the statement after it.continueskips the rest of the current iteration, and the loop goes on.- In a
for, the header's update runs aftercontinue, while in awhile,continuecan jump over the line that changes the variable. - You leave a
while (true)loop withbreak.