Course contentsShow
← Java
Lesson 182 of 2,8704. Control Flow StructuresFree lesson

continue with Labels: Skipping Outer Loop Iterations

Apply labeled continue to skip to the next iteration of an outer loop from within nested loops.

continue with Labels: Skipping Outer Loop Iterations

What you'll learn: How to use a labeled continue to skip back to the next iteration of an outer loop, not just the innermost one.

The Core Idea

Normally, continue only affects the loop it's directly inside—it jumps to the next iteration of that immediate loop. But what if you're deep inside nested loops and want to skip to the next iteration of an outer loop instead? That's where labeled continue comes in.

By placing a label before an outer loop and using continue labelName;, you can jump directly to the next iteration of that labeled loop, skipping any remaining code in both the inner and outer loop bodies for the current iteration.

How It Works

outerLoop:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            continue outerLoop; // Skip to next iteration of outer loop
        }
        System.out.println("i=" + i + ", j=" + j);
    }
    System.out.println("End of outer iteration " + i);
}

Output:

i=1, j=1
i=2, j=1
i=3, j=1

Notice that when j equals 2, the labeled continue skips the rest of the inner loop and the "End of outer iteration" statement, jumping straight to i++ and the next outer iteration.

Think of It Like This

Imagine nested tasks: sorting files into folders (outer) while checking individual documents (inner). If you find a problematic document, labeled continue lets you say "abandon this entire folder and move to the next one" rather than just "skip this document."

Key Takeaway: Labeled continue skips to the next iteration of a specific outer loop, bypassing all remaining code in both nested and outer loop bodies for the current iteration.