00:00

break Statement in Java – Explained with Examples

The break statement stops a loop or a switch immediately and continues with the code after it. It is how you say "I have what I need, stop now": stop searching once an item is found, stop reading when the data ends, or leave a switch after the matching case.

This tutorial shows break in every kind of loop, explains labeled break for nested loops, covers break in switch, and compares it with continue and return.

1. What Does break Do?

  • Inside a loop (for, while, do-while, for-each), break ends the innermost loop at once. The loop's condition and update are not evaluated again.
  • Inside a classic switch, break ends the switch and prevents fall-through.
  • With a label, break label; ends the labeled statement, typically an outer loop.
Flow of a for loop with a break statement

2. break in for, while and do-while Loops

BreakInLoops.java
public class BreakInLoops {
    public static void main(String[] args) {
        // for: stop searching once the item is found
        String[] shelf = {"pen", "notebook", "stapler", "glue", "tape"};
        for (int i = 0; i < shelf.length; i++) {
            System.out.println("checking " + shelf[i]);
            if (shelf[i].equals("stapler")) {
                System.out.println("found stapler at index " + i);
                break;                       // the remaining items are never checked
            }
        }

        // while: first number divisible by both 6 and 14
        int n = 1;
        while (true) {
            if (n % 6 == 0 && n % 14 == 0) break;
            n++;
        }
        System.out.println("smallest common multiple of 6 and 14: " + n);

        // do-while: stop when the running total passes 50
        int total = 0, day = 0;
        int[] sales = {12, 18, 9, 15, 20, 11};
        do {
            total += sales[day];
            day++;
            if (total > 50) break;
        } while (day < sales.length);
        System.out.println("target passed on day " + day + " with total " + total);
    }
}
Output
checking pen
checking notebook
checking stapler
found stapler at index 2
smallest common multiple of 6 and 14: 42
target passed on day 4 with total 54

In each loop, break skipped all remaining iterations as soon as the goal was reached. In the while (true) loop, break is the only way out, a common pattern when the exit test fits best in the middle of the body.

3. Labeled break for Nested Loops

A plain break exits only the loop it is in. To leave several nested loops at once, put a label (an identifier followed by a colon) before the outer loop and write break label;:

LabeledBreak.java
public class LabeledBreak {
    public static void main(String[] args) {
        int[][] seats = {
                {1, 1, 0, 1},
                {1, 1, 1, 1},
                {0, 1, 1, 0}
        };   // 1 = booked, 0 = free

        int freeRow = -1, freeCol = -1;
        search:                                   // label for the outer loop
        for (int r = 0; r < seats.length; r++) {
            for (int c = 0; c < seats[r].length; c++) {
                if (seats[r][c] == 0) {
                    freeRow = r;
                    freeCol = c;
                    break search;                 // exits BOTH loops
                }
            }
        }
        System.out.println("first free seat: row " + freeRow + ", column " + freeCol);

        // Without a label, break only leaves the inner loop
        for (int r = 0; r < seats.length; r++) {
            for (int c = 0; c < seats[r].length; c++) {
                if (seats[r][c] == 0) {
                    System.out.println("row " + r + ": first free column " + c);
                    break;                        // continues with the next row
                }
            }
        }
    }
}
Output
first free seat: row 0, column 2
row 0: first free column 2
row 2: first free column 0

With break search;, both loops ended at the first free seat. The plain break in the second version only left the inner loop, so the outer loop moved on to the next row.

Tip: labeled breaks are useful but can make code harder to follow. Moving the nested loops into a method and using return is often clearer.

4. break in a switch Statement

In a classic switch (with case X: labels), break ends the switch. Without it, execution falls through into the next case. Inside a loop, that break leaves only the switch, not the loop:

BreakInSwitch.java
public class BreakInSwitch {
    public static void main(String[] args) {
        String command = "stop";
        switch (command) {
            case "start":
                System.out.println("Starting...");
                break;                       // without break, "stop" would also run
            case "stop":
                System.out.println("Stopping...");
                break;
            default:
                System.out.println("Unknown command");
        }

        // Inside a loop, break in a classic switch exits the switch, not the loop
        for (int i = 1; i <= 3; i++) {
            switch (i) {
                case 2:
                    System.out.println("i = 2 (break leaves only the switch)");
                    break;
                default:
                    System.out.println("i = " + i);
            }
        }
    }
}
Output
Stopping...
i = 1
i = 2 (break leaves only the switch)
i = 3

Modern arrow cases (case X ->) never fall through, so they do not need break. See switch Statement.

5. break vs continue vs return

StatementEffectUse it to…
breakExits the innermost loop or switch (or a labeled statement)Stop looping once a goal is reached
continueSkips the rest of the current iterationIgnore certain items but keep looping
returnExits the whole methodReturn a result as soon as it is known

6. Rules and Notes

  • An unlabeled break can only appear inside a loop or switch; elsewhere it is a compile error ("break outside switch or loop").
  • A labeled break can exit any labeled block, but in practice labels are used with loops.
  • Statements directly after break in the same block are unreachable and cause a compile error.
  • break cannot be used inside a lambda to stop forEach(); use a regular loop or a stream method such as anyMatch() or takeWhile().
  • If break leaves a try block, its finally block still runs.

7. Best Practices

  • Use break to make searches efficient: stop as soon as the answer is found.
  • Keep the exit condition easy to see; many hidden breaks in one loop are hard to follow.
  • Prefer a clear loop condition when it expresses the end naturally.
  • Use return from a helper method instead of complex labeled breaks.

8. Summary

  • break immediately exits the innermost loop or switch.
  • break label; exits an outer labeled loop.
  • In classic switch statements, break prevents fall-through.
  • continue skips one iteration; return leaves the method.

Related Tutorials

Reference: Oracle Java Tutorials: Branching Statements