Java Break Statement
Java has no goto operator, but it does have three jump statements: break, continue, and return. They transfer control to another part of the program. This lesson covers the break statement in Java in detail.
The Java break statement can be used in the following cases:
- To terminate a statement sequence in a
switchblock, as discussed in the Switch Statement lesson. - To exit a loop early.
- As a "civilized" form of the
gotostatement (break with a label).
Using break to exit a loop
Let's look at an example of using the Java break statement to exit a while loop. The loop is set to run 100 times, but in case of an unexpected condition, we want to exit early using break.
public class BreakLoopExample1 {
public static void main(String[] args) {
int i = 0, n = 100;
while (i < n) {
if (i == 10) {
break;
}
System.out.println("i: " + i++);
}
System.out.println("Loop finished.");
}
} The break statement in Java should not be used as the regular way to leave a loop — that's the job of the loop condition. Reserve it for special cases. Also note that an unlabeled break is only legal inside a loop or a switch statement — anywhere else the compiler reports the error break outside switch or loop.
Easy to get wrong
If a switch statement sits inside a loop, a break inside it terminates only the switch, not the loop. To leave the loop directly from a case branch, you need a labeled break — a detail interviewers love to test.
Break inside nested loops
When multiple loops are nested, the break statement exits only the innermost loop. For example:
public class BreakLoopExample2 {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.print("Pass " + i + " : ");
for (int j = 0; j < 100; j++) {
if (j == 10) {
break;
}
System.out.print(j + " ");
}
System.out.println();
}
System.out.println("Loops finished");
}
} Output of the program:
Pass 0 : 0 1 2 3 4 5 6 7 8 9
Pass 1 : 0 1 2 3 4 5 6 7 8 9
Pass 2 : 0 1 2 3 4 5 6 7 8 9
Loops finished You can use more than one break statement in a Java loop, but it hurts readability — better to avoid it.
Java break statement with a label
Let's explore the labeled break in Java. It is often seen as a structured substitute for the goto statement, which Java does not support, unlike some other programming languages.
Syntax of a labeled break:
break label; Here, the label is an identifier marking a block of code — either a standalone code block or the target block of another statement, such as a loop. To label a block, place the label name followed by a colon at the beginning of the block.
A label is any valid Java identifier followed by a colon.
Labeled break to exit nested loops
The following code uses three labeled blocks: first, second, and third. When break second; is reached, execution leaves the second block.
public class BreakLoopExample3 {
public static void main(String[] args) {
boolean t = true;
first:
{
second:
{
third:
{
System.out.println("Before break.");
if (t) {
break second;
}
System.out.println("This line won't execute.");
}
System.out.println("This line won't execute.");
}
System.out.println("After second block.");
}
}
} Program output:
Before break.
After second block. Most often, a labeled break is used to exit several nested loops at once — to break out of the outer loop directly from the inner one:
public class BreakLoopExample4 {
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
System.out.print("Pass " + i + " : ");
for (int j = 0; j < 100; j++) {
if (j == 10) {
break outer; // exits both loops
}
System.out.print(j + " ");
}
System.out.println("This line won't be printed.");
}
}
} One important restriction: the label must be placed directly before the block or loop being exited. A labeled break cannot "jump" to an arbitrary place in the program the way a real goto does.
Break vs continue vs return
Break is not the only jump statement in Java. Let's compare it with continue and return:
| Statement | What it does | Where it is allowed |
|---|---|---|
| break | Terminates the whole loop or switch; with a label — the labeled block | Inside a loop or switch |
| continue | Skips the rest of the current iteration and moves on to the next one | Only inside a loop |
| return | Exits the entire method and returns control to the caller | Anywhere in a method |
Frequently Asked Questions
What is the difference between break and continue?
Break terminates the whole loop — execution continues with the first statement after the loop. Continue ends only the current iteration: control jumps to the condition check (in while) or to the update expression (in for), and the loop keeps running.
How do I break out of multiple nested loops at once?
A plain break exits only the innermost loop. To leave the outer loop, use a labeled break (break outer;) or extract the nested loops into a separate method and end it with return — the second option is often easier to read.
Can break be used outside a loop or switch?
No. An unlabeled break is only allowed inside a loop or a switch statement; otherwise the compiler reports "break outside switch or loop". A labeled break may also target an ordinary labeled code block.
Is a labeled break the same as goto?
No. Goto can jump to an arbitrary place in a program, while a labeled break can only exit a labeled block or loop that encloses it. You cannot jump forward or into another block, so the code stays structured.
Comments