1. Introduction
break and continue are jump statements that alter the normal flow of loops (and, in the case of break, switch statements) in Java. They give fine-grained control over when to exit a loop entirely or skip to its next iteration.
Cricket analogy: break is like an umpire calling off play entirely and ending the innings loop, while continue is like a bowler being told to skip this over's remaining balls and move straight to the next over.
Java also supports labeled break and labeled continue, a feature that lets these statements target an outer loop directly from within a nested loop — something not directly available in languages like C or C++ without goto.
Cricket analogy: A labeled break lets a nested "over loop" inside a "match loop" jump straight out of the entire match at once, something plain break in older languages like C couldn't do without a goto.
2. Syntax
// break: exits the nearest enclosing loop (or switch) immediately
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
// continue: skips the rest of the current iteration, proceeds to next
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}// Labeled break and continue for nested loops
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) {
continue outer; // skips to next iteration of outer loop
}
if (i == 2) {
break outer; // exits the outer loop entirely
}
System.out.println("i=" + i + ", j=" + j);
}
}3. Explanation
break immediately terminates the nearest enclosing loop (for, while, do-while) or switch statement, and control resumes at the statement immediately following that construct. continue, by contrast, does not exit the loop — it skips the remaining statements in the current iteration and jumps to the loop's next condition check (or update expression, in a for loop).
Cricket analogy: break in a bowling-over for-loop immediately stops that over and hands control to the next statement after it (drinks break), while continue skips the rest of this ball's commentary and jumps straight to the loop's next-ball condition check.
A label is an identifier followed by a colon placed immediately before a loop statement. Labeled break exits the labeled loop entirely (even from within an inner loop), while labeled continue skips directly to the next iteration of the labeled (outer) loop, bypassing any remaining inner loop iterations.
Cricket analogy: A label like matchLoop: placed before the outer over-loop lets an inner ball-loop's labeled break end the entire match immediately, while labeled continue skips straight to the next over, bypassing any remaining balls in the current over.
Labeled break/continue are Java-specific conveniences for nested-loop control; they avoid the need for boolean flag variables or goto-like workarounds common in other languages.
Exam trap: an unlabeled break or continue inside a nested loop only affects the innermost loop it is directly inside, not any outer loop — a label is required to target an outer loop.
4. Example
public class BreakContinueDemo {
public static void main(String[] args) {
// break example: stop at first multiple of 7
for (int i = 1; i <= 20; i++) {
if (i % 7 == 0) {
System.out.println("Found multiple of 7: " + i);
break;
}
}
// continue example: print only odd numbers 1-6
for (int i = 1; i <= 6; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println("Odd: " + i);
}
// labeled break example
search:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
System.out.println("Match at i=" + i + ", j=" + j);
break search;
}
}
}
}
}5. Output
Found multiple of 7: 7
Odd: 1
Odd: 3
Odd: 5
Match at i=1, j=16. Key Takeaways
- break exits the nearest enclosing loop or switch statement immediately.
- continue skips the rest of the current iteration and moves to the loop's next check/update.
- Unlabeled break/continue only affect the innermost enclosing loop.
- Labeled break exits the labeled outer loop entirely, even from a nested inner loop.
- Labeled continue skips to the next iteration of the labeled outer loop, bypassing remaining inner iterations.
- Labels are Java identifiers followed by a colon, placed directly before the target loop.
Practice what you learned
1. What does the break statement do inside a loop?
2. What does the continue statement do inside a loop?
3. In a nested loop, what does an unlabeled break do?
4. What is required to make break exit an outer loop from within a nested inner loop?
5. What does `continue outer;` do when outer labels an enclosing loop?
Was this page helpful?
You May Also Like
for Loop in Java
Understand Java's classic three-part for loop and the enhanced for-each loop for iterating over arrays and collections concisely.
while and do-while Loops in Java
Learn the difference between Java's entry-controlled while loop and exit-controlled do-while loop, including syntax, semantics, and common pitfalls.
switch Statement in Java
Master Java's switch statement for multi-way branching, covering classic case-break syntax, fall-through behavior, and modern Java 14+ switch expressions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics