0Pricing
Java Academy · Lesson

Labeled continue

Continue an outer loop.

Labeled continue is a free Java Academy lesson on CoddyKit — lesson 3 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.

Continuing an Outer Loop

A plain continue only restarts the inner loop. Sometimes you want to skip to the next pass of the outer loop instead.

For that, Java offers a labeled continue.

It pairs a loop label with the continue keyword to control which loop advances.

Labeled continue Syntax

The form is continue label; where the label names an enclosing loop.

Instead of moving the inner loop forward, it jumps to the next iteration of the labeled loop.

Everything left in the inner loop for that round is skipped.

continue outer;  // jump to next iteration of 'outer'

A First Example

Here, when col equals 2 we continue the outer loop.

This abandons the rest of the inner loop and moves to the next row right away.

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 (col == 2) {
                    continue outer;
                }
                System.out.println("row " + row + ", col " + col);
            }
        }
    }
}

Reading the Output

Each row only printed col 1. When col reached 2, continue outer jumped straight to the next row.

Columns 2 and 3 were never printed for any row.

The inner loop restarted from col 1 on each new row.

continue vs continue label

Compare with a plain continue. It would skip only col 2 and still print col 3.

Run this and notice col 1 and col 3 both appear for 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 (col == 2) {
                    continue;
                }
                System.out.println("row " + row + ", col " + col);
            }
        }
    }
}

The Key Difference

Plain continue only skips the current inner pass, so the inner loop keeps going.

Labeled continue outer abandons the entire inner loop and restarts the outer loop's next round.

That single label changes how much code is skipped.

Skipping a Whole Row

Labeled continue is handy when one bad value should invalidate the rest of an inner pass.

Here, if any number in a row is negative we skip the rest of that row's work and move on.

public class Main {
    public static void main(String[] args) {
        int[][] data = {{1, 2, 3}, {4, -1, 6}, {7, 8, 9}};
        rows:
        for (int r = 0; r < data.length; r++) {
            int sum = 0;
            for (int c = 0; c < data[r].length; c++) {
                if (data[r][c] < 0) {
                    System.out.println("Row " + r + " skipped");
                    continue rows;
                }
                sum += data[r][c];
            }
            System.out.println("Row " + r + " sum = " + sum);
        }
    }
}

Tracing That Example

Rows 0 and 2 printed their sums. Row 1 hit a negative value, printed "Row 1 skipped", and jumped to the next row.

Importantly, the sum line for row 1 never ran because the labeled continue skipped past it.

Filtering Pairs

Another use: skip an entire outer item the moment one inner condition fails.

This loop prints pairs but skips any i whose product with j would exceed 6.

public class Main {
    public static void main(String[] args) {
        loopI:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i * j > 6) {
                    continue loopI;
                }
                System.out.println(i + " x " + j + " = " + (i * j));
            }
        }
    }
}

Label Names and Placement

The labeled loop must enclose the continue statement. You cannot continue a label that is not above you.

Pick descriptive names such as rows or loopI so readers know which loop advances.

A clear label is documentation in itself.

When It Helps vs Hurts

Labeled continue helps when a condition should invalidate the rest of an inner pass and move on.

But overusing labels makes flow hard to trace. If a plain continue or an early method return reads more simply, prefer that.

Use labels only where they truly clarify.

Quick Check

Predict the effect of a labeled continue.

Recap

continue label; skips the rest of the inner loop and advances the named outer loop to its next iteration.

It differs from a plain continue, which only skips one inner pass. Use labels when they make multi-loop logic clearer, not just shorter.

Frequently asked questions

Is the “Labeled continue” lesson free?

Yes — the full text of “Labeled continue” 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 continue”?

Continue an outer loop. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Labeled continue” 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.

All lessons in this course

  1. break and continue
  2. Labeled break
  3. Labeled continue
  4. Avoiding Spaghetti Flow
← Back to Java Academy