break & continue in Java
Exit the loop vs skip to the next iteration.
break: get out now
break immediately exits the innermost loop (or switch). The rest of the body and all remaining passes are skipped; execution continues after the loop.
for (int i = 1; i <= 100; i++) {
if (i * i > 50) {
break;
}
System.out.print(i + " ");
} // 1 2 3 4 5 6 7continue: skip this one
continue skips the rest of the current pass and moves on to the next one. The loop itself keeps going. In a for loop, **the update (i++) still runs** before the next check.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // skip 3
}
System.out.print(i);
} // 1245Your turn
What prints?
for (int i = 1; i <= 6; i++) {
if (i % 3 == 0) continue;
if (i == 5) break;
System.out.print(i);
}12412451212456
Show the answer
1 and 2 print. At 3, continue skips the print. 4 prints. At 5, break exits before printing. Result: 124.
continue in a while loop
In a while loop the update is just a normal line in the body, and continue jumps over it. Below, when i hits 2, continue skips i++, so i stays 2 forever: an infinite loop. Increment before continuing, or use a for loop.
int i = 0;
while (i < 5) {
if (i == 2) {
continue; // skips i++!
}
System.out.println(i);
i++;
}Only the innermost loop
break and continue affect only the innermost loop they're in. A break inside a nested loop leaves the inner loop; the outer loop carries on with its next pass.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) break; // inner only
System.out.print(i + "" + j + " ");
}
} // 00 10 20Nested loops
The inner loop breaks when j > i. What prints?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j > i) break;
System.out.print(i + "" + j + " ");
}
}0000 10 11 20 21 2200 11 2200 01 02
Show the answer
Each break only ends the *inner* loop. Row 0 prints 00, row 1 prints 10 11, row 2 prints 20 21 22. The outer loop never stops early.
In real projects
Early exits save real time: stop searching once you've found the user, stop retrying once the upload succeeds. continue keeps code flat: "skip invalid rows" at the top of a loop instead of nesting everything inside an if. But a misplaced break can be catastrophic (see the fun fact).
Key takeaways
- break: leave the loop entirely
- continue: skip to the next iteration
- In a for loop, continue still runs the update (i++)
- In a while loop, continue can skip your i++ and loop forever
💡 break is walking out of the cinema; continue is skipping one boring scene and staying for the next.
On 15 January 1990, a misplaced break in C code was blamed for crashing AT&T's long-distance network for hours, leaving millions of calls unconnected.
Practice questions
What does this print?
for (int i = 0; i < 6; i++) {
if (i % 2 == 0) {
continue;
}
System.out.print(i);
}- 024
- 135
- 0
- 012345
Check your answer
135. For even i, continue skips the print. Only the odd values 1, 3 and 5 get printed.
What does this print?
int i = 0;
while (true) {
i++;
if (i == 3) continue;
if (i > 4) break;
System.out.print(i);
}- 1234
- 12
- 124
- 1245
Check your answer
124. i = 1, 2 print. At 3, continue skips the print. 4 prints. At 5, i > 4 triggers break.