By WebNest Studio Engineering Team
Java - Core Tutorial
Java Break and Continue
break and continue are jump statements that alter the normal top-to-bottom flow of a loop from inside its body. break exits the loop entirely, while continue skips only the rest of the current iteration and moves straight to the next one. Both make it possible to express "stop early" or "skip this one" logic without wrapping the whole loop body in a large conditional.
Beyond simple loops, Java also supports labeled break and labeled continue, which extend this control to nested loops — letting inner-loop code affect an outer loop directly, something an unlabeled break or continue cannot do.
Why it matters
break and continue let a loop stop or skip as soon as the answer is known, instead of running to the end and tracking extra flags. Used well they make search and filter loops shorter; used carelessly, especially in nested loops, they hide the path the code takes — so it pays to know exactly which loop each one affects.
The break Statement
An unlabeled break immediately terminates the nearest enclosing loop (for, while, or do-while) or switch statement, and execution resumes at the first statement after that loop or switch. It is commonly used to stop searching once a target has been found, avoiding unnecessary further iterations.
The continue Statement
continue skips the remaining statements in the current iteration of the nearest enclosing loop and jumps directly to the loop's update/condition check — in a for loop that means the update expression still runs before the condition is re-tested. It is useful for filtering out cases that should be skipped without nesting the rest of the loop body inside an if block.
Labeled break and continue
A label is an identifier followed by a colon placed immediately before a loop, such as search: before a for loop. break search; exits that specific labeled loop (even from inside a nested inner loop), and continue search; skips to the next iteration of that specific labeled loop rather than the innermost one. Labels are the only way to break or continue an outer loop directly from inside a nested loop without extra boolean flags.
Examples
continue to skip even numbers and break to stop once a limit is exceeded
public class BreakContinueDemo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
if (i > 7) {
break;
}
System.out.println(i);
}
}
}
1
3
5
7
A labeled break exiting a nested loop as soon as a value is found in a 2D array
public class LabeledBreakDemo {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int target = 5;
boolean found = false;
search:
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] == target) {
found = true;
System.out.println("Found at [" + row + "][" + col + "]");
break search;
}
}
}
System.out.println("Found: " + found);
}
}
Found at [1][1]
Found: true
Common Mistakes
- Using an unlabeled break inside a nested loop expecting it to exit both loops — it only exits the innermost enclosing loop.
- Confusing break and continue in a for loop, forgetting that continue still runs the update expression before re-checking the condition.
- Placing the label colon incorrectly or on the wrong loop, so the labeled break/continue does not target the intended loop.
- Overusing break to simulate early returns instead of restructuring the loop or extracting a method, which can make control flow harder to follow.
Exercise
Find the first number between 1 and 200 that is divisible by both 7 and 9, and stop searching as soon as you find it.
Expected output: 63
public class FirstCommonMultiple {
public static void main(String[] args) {
int found = -1;
for (int i = 1; i <= 200; i++) {
// TODO: when i is divisible by 7 and by 9, store it and stop the loop
}
System.out.println(found);
}
}
Show hints
- A number is divisible by 7 when
i % 7 == 0. - Without
breakthe loop would keep going and overwritefoundwith 126 and then 189.
Show solution
public class FirstCommonMultiple {
public static void main(String[] args) {
int found = -1;
for (int i = 1; i <= 200; i++) {
if (i % 7 == 0 && i % 9 == 0) {
found = i;
break;
}
}
System.out.println(found); // 63
}
}
Quick quiz
1. A break appears inside the inner of two nested loops, with no label. Which loop does it end?
- A. Both loops
- B. Only the inner loop
- C. Only the outer loop
- D. The whole method
Show answer
B. An unlabelled break ends only the innermost loop (or switch) that contains it. The outer loop carries on.
2. In a for loop, where does execution go after continue?
- A. To the statement after the loop
- B. Back to the initialisation
- C. To the update expression, then the condition
- D. To the start of the method
Show answer
C. continue skips the rest of the body. In a for loop the update expression still runs before the condition is checked again.
3. What does this print? for (int i = 1; i <= 5; i++) { if (i == 3) continue; System.out.print(i + " "); }</code>
A. 1 2B. 1 2 3 4 5C. 3D. 1 2 4 5
Show answer
D. When i is 3, continue skips the print for that iteration only. The loop then carries on with 4 and 5.
Interview questions
What is a labelled break and when would you use one?
Show answer
A label is a name placed before a loop, such as outer:. break outer; ends that named loop even from inside a nested one. It is useful for leaving several levels at once — for example, stopping a search through a 2D array the moment the value is found — without an extra boolean flag.
What is the difference between break and return inside a loop?
Show answer
break ends the loop, and execution continues with the statement after it in the same method. return ends the whole method immediately, so nothing after the loop runs either (apart from a finally block).
Key Points to Remember
- break exits the nearest enclosing loop or switch entirely; continue skips only the rest of the current iteration.
- In a for loop, continue still triggers the update expression before the condition is re-checked.
- Labeled break and continue can target a specific outer loop from inside nested loops.
- A label is written as an identifier followed by a colon directly above the loop it names.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering