Java > Core Java > Control Flow > Labeled Break and Continue
Labeled Break Example
This code snippet demonstrates how to use a labeled break statement to exit a specific outer loop from within a nested loop.
Code Snippet
The code defines an outer loop labeled 'outerLoop'. When the condition i == 2 && j == 2 is met, the break outerLoop; statement is executed. This immediately exits the outer loop, skipping the remaining iterations of both the inner and outer loops.
public class LabeledBreakExample {
public static void main(String[] args) {
outerLoop: // Label for the outer loop
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 2 && j == 2) {
break outerLoop; // Exit the outer loop when i=2 and j=2
}
System.out.println("i=" + i + ", j=" + j);
}
}
System.out.println("Outer loop completed (or broken).");
}
}
Concepts Behind the Snippet
Labeled break allows you to exit a specific enclosing loop based on its label. This is particularly useful in nested loops where you need to break out of a loop that isn't the immediately enclosing one. Without labels, a simple break would only exit the innermost loop.
Real-Life Use Case
Consider searching for a specific element in a multi-dimensional array. If the element is found, you might want to immediately stop searching, even if you are deep within nested loops. A labeled break would allow you to exit the entire search process efficiently. Another use case is parsing complex data structures where a certain condition indicates the end of a particular section, requiring you to jump out of multiple nested loops.
Best Practices
Use labeled break judiciously. Overuse can make code harder to read and understand. Ensure that the label name is meaningful and clearly indicates the loop it is associated with. Prefer structuring your code to avoid deeply nested loops whenever possible. Consider refactoring complex logic into smaller, more manageable methods.
Interview Tip
Be prepared to explain how labeled break differs from a regular break statement. Also, be ready to discuss scenarios where labeled break can improve code clarity or efficiency. The interviewer might ask you to rewrite a piece of code using and without labeled break to demonstrate your understanding.
When to Use Them
Use labeled break when you need to exit a specific outer loop from within a nested loop, and a simple break would only exit the innermost loop. They are most beneficial when the logic for exiting the outer loop is intertwined with the logic of the inner loop.
Alternatives
Alternatives to labeled break include restructuring the code to avoid nested loops (e.g., using separate methods or streams) or setting a boolean flag that is checked by the outer loop to determine whether to continue. However, these alternatives can sometimes make the code more verbose or less readable.
Pros
Cons
FAQ
-
What happens if I use
breakwithout a label inside a nested loop?
A regularbreakstatement without a label will only exit the immediately enclosing loop. -
Can I use labeled
breakto exit multiple loops at once?
No, a labeledbreakstatement can only exit one specific labeled loop. To exit multiple loops, you would need multiple labeledbreakstatements or a different control flow structure.