100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

break and continue in Java

Understand how break and continue alter loop execution in Java, including labeled break/continue for controlling nested loops directly.

Control FlowBeginner7 min readJul 7, 2026
Analogies

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

java
// 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);
}
java
// 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

java
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

text
Found multiple of 7: 7
Odd: 1
Odd: 3
Odd: 5
Match at i=1, j=1

6. 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

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#BreakAndContinueInJava#Break#Continue#Syntax#Explanation#StudyNotes#SkillVeris