0
0
JavaHow-ToBeginner · 3 min read

How to Use Labeled Break in Java: Syntax and Examples

In Java, use a labeled break to exit an outer loop directly by placing a label before the loop and calling break with that label inside nested loops. This helps control flow clearly when multiple loops are involved.
📐

Syntax

A labeled break uses a label name followed by a colon before a loop. Inside nested loops, use break labelName; to exit the labeled loop immediately.

Parts:

  • labelName: - a custom name before the loop
  • break labelName; - exits the loop with that label
java
labelName:
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (someCondition) {
            break labelName;
        }
    }
}
💻

Example

This example shows how to use a labeled break to exit both inner and outer loops when a condition is met.

java
public class LabeledBreakExample {
    public static void main(String[] args) {
        outerLoop:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                System.out.println("i = " + i + ", j = " + j);
                if (i == 2 && j == 2) {
                    break outerLoop; // Exit outer loop immediately
                }
            }
        }
        System.out.println("Exited loops.");
    }
}
Output
i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 1 i = 2, j = 2 Exited loops.
⚠️

Common Pitfalls

Common mistakes include:

  • Using break; without a label inside nested loops only exits the innermost loop, not the outer one.
  • Placing the label on a statement other than a loop causes a compile error.
  • Labels must be unique and placed immediately before the loop.
java
public class WrongBreak {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    break; // Only breaks inner loop, outer continues
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }
    }
}

// Correct way:
public class CorrectBreak {
    public static void main(String[] args) {
        outer:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    break outer; // Exits outer loop
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }
    }
}
📊

Quick Reference

Labeled Break Cheat Sheet:

PartDescription
labelName:Place before a loop to name it
break labelName;Exit the labeled loop immediately
Use caseExit multiple nested loops at once
RestrictionLabel must be before a loop, not a block
PartDescription
labelName:Place before a loop to name it
break labelName;Exit the labeled loop immediately
Use caseExit multiple nested loops at once
RestrictionLabel must be before a loop, not a block

Key Takeaways

Use labeled break to exit outer loops directly from inner loops.
Place the label immediately before the loop you want to break.
Without a label, break only exits the innermost loop.
Labels must be unique and cannot be placed on non-loop statements.
Labeled break improves readability when breaking nested loops.