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),breakends the innermost loop at once. The loop's condition and update are not evaluated again. - Inside a classic
switch,breakends theswitchand prevents fall-through. - With a label,
break label;ends the labeled statement, typically an outer loop.
2. break in for, while and do-while Loops
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);
}
}
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;:
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
}
}
}
}
}
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.
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:
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);
}
}
}
}
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
| Statement | Effect | Use it to… |
|---|---|---|
break | Exits the innermost loop or switch (or a labeled statement) | Stop looping once a goal is reached |
| continue | Skips the rest of the current iteration | Ignore certain items but keep looping |
| return | Exits the whole method | Return a result as soon as it is known |
6. Rules and Notes
- An unlabeled
breakcan only appear inside a loop orswitch; elsewhere it is a compile error ("break outside switch or loop"). - A labeled
breakcan exit any labeled block, but in practice labels are used with loops. - Statements directly after
breakin the same block are unreachable and cause a compile error. breakcannot be used inside a lambda to stopforEach(); use a regular loop or a stream method such asanyMatch()ortakeWhile().- If
breakleaves atryblock, itsfinallyblock still runs.
7. Best Practices
- Use
breakto 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
returnfrom a helper method instead of complex labeled breaks.
8. Summary
breakimmediately exits the innermost loop orswitch.break label;exits an outer labeled loop.- In classic switch statements,
breakprevents fall-through. continueskips one iteration;returnleaves the method.
Related Tutorials
Reference: Oracle Java Tutorials: Branching Statements