Labeled break
Exit outer loops cleanly.
Labeled break is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Problem with Nested Loops
A plain break only exits the inner loop. But often you want to leave both loops at once.
Imagine searching a grid for one value. Once you find it, you want to stop everything, not just the inner loop.
Java solves this with a labeled break.
What Is a Label?
A label is a name you attach to a loop, followed by a colon.
For example outer: placed just before a for loop names that loop "outer".
Labels by themselves do nothing. They become useful when paired with break or continue.
outer:
for (int i = 0; i < 3; i++) {
// 'outer' is now a name for this loop
}Labeled break Syntax
Write the label name after the break keyword: break outer;
This tells Java to jump out of the loop named outer, even from deep inside a nested loop.
Both the inner and outer loops stop immediately.
break outer; // exits the loop labeled 'outer'Labeled break in Action
Here we search a 3x3 grid. When we find col 2 in row 2, we break the outer loop.
This stops both loops at once, unlike a plain break.
public class Main {
public static void main(String[] args) {
outer:
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
if (row == 2 && col == 2) {
System.out.println("Found at " + row + "," + col);
break outer;
}
}
}
System.out.println("Search over");
}
}Why It Stopped Everything
The output was "Found at 2,2" then "Search over".
Because we said break outer, both loops ended. Row 3 was never visited.
A plain break would have continued to row 3 after leaving only the inner loop.
Comparing the Two Breaks
Look at the same grid with a plain break instead.
It leaves only the inner loop, so the outer loop keeps marching through every row.
public class Main {
public static void main(String[] args) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 3; col++) {
if (row == 2 && col == 2) {
break;
}
System.out.println(row + "," + col);
}
}
}
}Searching a 2D Array
A real use case is finding a target inside a matrix.
Once the value is located, a labeled break ends the whole search cleanly.
public class Main {
public static void main(String[] args) {
int[][] grid = {{1, 2}, {3, 9}, {5, 6}};
int target = 9;
search:
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] == target) {
System.out.println("At row " + r + " col " + c);
break search;
}
}
}
}
}The Label Must Be on a Loop
A label sits directly in front of the loop it names. You cannot break to a label that is not on an enclosing loop.
Choose clear names like outer, rows, or search so the code reads naturally.
Avoid vague names like x: that hide your intent.
Three Levels Deep
Labels also work with three or more nested loops.
Here a labeled break jumps straight out of all three at once when a condition is met.
public class Main {
public static void main(String[] args) {
done:
for (int a = 1; a <= 2; a++) {
for (int b = 1; b <= 2; b++) {
for (int c = 1; c <= 2; c++) {
if (a + b + c == 4) {
System.out.println(a + "," + b + "," + c);
break done;
}
}
}
}
}
}When Labeled break Helps
Labeled break shines when you must abandon several loops the instant a result is found.
It avoids extra flag variables like boolean found checked at every level.
Used sparingly, it keeps search loops short and clear.
Use It Sparingly
Although powerful, too many labels make code hard to follow.
Reach for a labeled break mainly in genuine nested searches. For simple loops, a plain break is clearer.
Readability always wins.
Quick Check
Decide what a labeled break does in nested loops.
Recap
A label names a loop, and break label; exits that named loop, escaping all loops nested inside it.
This is ideal for stopping a whole nested search at once. Next you will see how labels work with continue.
Frequently asked questions
Is the “Labeled break” lesson free?
Yes — the full text of “Labeled break” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Labeled break”?
Exit outer loops cleanly. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Labeled break” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.