Java > Core Java > Control Flow > Labeled Break and Continue
Labeled Continue Example
This code snippet demonstrates the use of a labeled continue
statement to skip to the next iteration of 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 continue outerLoop;
statement is executed. This skips the remaining iterations of the inner loop and continues to the next iteration of the outer loop.
public class LabeledContinueExample {
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) {
continue outerLoop; // Skip to the next iteration of the outer loop
}
System.out.println("i=" + i + ", j=" + j);
}
System.out.println("Outer loop iteration " + i + " completed.");
}
System.out.println("Outer loop finished.");
}
}
Concepts Behind the Snippet
Labeled continue
allows you to skip to the next iteration of a specific enclosing loop based on its label. This is useful in nested loops when you want to avoid executing the rest of the inner loop's current iteration and proceed to the next iteration of the outer loop.
Real-Life Use Case
Imagine processing a batch of records, where each record contains multiple fields. If a specific field in a record is invalid, you might want to skip processing the rest of that record and move on to the next record in the batch. A labeled continue
would allow you to efficiently skip the remaining fields in the invalid record and continue with the next record in the batch.
Best Practices
Use labeled continue
sparingly, as overuse can make code harder to understand. Make sure the label name is descriptive and clearly indicates the loop it is associated with. Consider refactoring your code to avoid deeply nested loops whenever possible. If the logic for skipping iterations is complex, consider extracting it into a separate method.
Interview Tip
Be prepared to explain the difference between labeled break
and labeled continue
. Also, be ready to discuss situations where labeled continue
can improve code readability or efficiency. The interviewer might ask you to demonstrate how to use labeled continue
in a given code scenario.
When to Use Them
Use labeled continue
when you need to skip to the next iteration of a specific outer loop from within a nested loop, and a simple continue
would only skip the current iteration of the innermost loop.
Alternatives
Alternatives to labeled continue
include using a boolean flag to control the inner loop's execution or restructuring the code to avoid nested loops altogether. However, these alternatives can sometimes lead to less readable or more verbose code.
Pros
Cons
FAQ
-
What happens if I use
continue
without a label inside a nested loop?
A regularcontinue
statement without a label will only skip to the next iteration of the immediately enclosing loop. -
Can I use labeled
continue
to skip to a previous iteration of the loop?
No, labeledcontinue
only skips to the next iteration of the specified loop. It doesn't allow you to go back to a previous iteration.