Break and continue
What you will learn
Use break when the loop has finished its job early. Use continue when one item should be skipped but later items still need processing. Both change the normal execution path, so make their target and reason clear.
public class Main { public static void main(String[] args) { for (int slot = 1; slot <= 5; slot++) { System.out.println("Check " + slot); if (slot == 3) { break; } } System.out.println("Finished"); }}Expected output:
Check 1Check 2Check 3FinishedThe break exits after checking slot three. The statement following the loop still runs.
How it works
In ordinary loop code, break; exits the nearest enclosing loop (or switch statement if it is nearer). continue; skips the remainder of the current iteration of the nearest loop. In a basic for loop, continue still reaches the header update before the next condition check. In a while or do-while loop, continue transfers control to the condition, so a body update placed after continue may be skipped. In nested loops a label such as search: before the outer loop allows break search; to leave it; continue search; starts the next outer iteration, including the outer for update if present. A labeled break can also target an enclosing labeled non-loop statement; a labeled continue must target a loop. These examples have no finally blocks, which can affect control transfer and are taught later. Prefer a clear single-loop solution when nesting and labels are unnecessary.
public class Main { public static void main(String[] args) { for (int item = 1; item <= 5; item++) { if (item % 2 == 0) { continue; } System.out.println(item); } }}Expected output:
135Even item numbers are skipped. The for update runs even on those iterations.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { search: for (int row = 1; row <= 3; row++) { for (int seat = 1; seat <= 3; seat++) { if (row == 2 && seat == 1) { System.out.println("Found " + row + ":" + seat); break search; } } } System.out.println("End"); }}Expected output:
Found 2:1EndThe label identifies the outer loop. An unlabeled break would leave only the inner seat loop.
Common mistakes
In a while loop, placing continue before the only increment may leave the condition unchanged forever. Move the increment before any relevant continue or use a basic for with the update in its header. A break inside a switch nested in a loop normally exits the switch, not the loop. Labels are not arbitrary jumps to any line.
Try it yourself
Given a maximum item number from 0 to 20, inspect numbers from 1 to that maximum. Skip multiples of 3; stop completely before processing item 8, if reached. Print each processed number on its own line. Test maximum 10, 4 and 0.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Break and continue — knowledge check
Apply break and continue to examples and boundary conditions.
What does an unlabeled break in an inner for loop exit?
Does the update of a basic for run after a continue targeting that loop?
Where does continue in a while loop transfer control?
How can an inner loop directly leave an enclosing loop labeled search?
What must a labeled continue target?