Back to Blog
Java

Java Continue Statement: Loop Control Explained

java continue statement: Understand how the continue statement works in Java loops, including labeled continue for nested loops, edge cases, and maintainability tradeo...

java loopscontrol flowlabeled continuebreak vs continueloop iteration
Diagram showing a Java loop where one iteration branches off and skips ahead to the next cycle, illustrating the continue statement.
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } System.out.println(i); }

The java continue statement skips the remaining body of the current loop iteration and moves to the next iteration. It works in for, while, and do-while loops, and the exact behavior depends on the loop type.

How Continue Works in a for Loop

In a for loop, continue transfers control to the update expression (the third clause in the loop header), then the condition is re-evaluated. This means the loop counter still advances normally.

for (int i = 0; i < 5; i++) { if (i == 2) { continue; } System.out.println("Processing index " + i); }

Output:

Processing index 0
Processing index 1
Processing index 3
Processing index 4

The iteration where i == 2 is skipped entirely. The update expression i++ still runs, so the loop does not become infinite.

Continue in while and do-while Loops

In a while loop, continue jumps back to the condition check. Any code between continue and the end of the loop body is skipped.

int count = 0; while (count < 5) { count++; if (count == 3) { continue; } System.out.println("Count is " + count); }

Output:

Count is 1
Count is 2
Count is 4
Count is 5

A common mistake in while loops is placing continue before the counter update. If the increment appears after the continue statement, the loop can become infinite because the counter never advances.

// Dangerous: counter update is after continue int count = 0; while (count < 5) { if (count == 3) { continue; // count never reaches 4 or 5 } count++; }

In a do-while loop, continue jumps to the condition check at the bottom of the loop. The loop body after continue is skipped, but the condition is still evaluated.

Labeled Continue for Nested Loops

A labeled continue lets you skip an iteration of an outer loop from inside an inner loop. The label must appear immediately before the loop statement.

outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) { continue outer; } System.out.println("i=" + i + ", j=" + j); } }

Output:

i=0, j=0
i=1, j=0
i=2, j=0

When j == 1, control jumps to the next iteration of the outer loop, abandoning the inner loop for that outer iteration.

Without the label, continue would only skip the current inner-loop iteration:

for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) { continue; } System.out.println("i=" + i + ", j=" + j); } }

Output:

i=0, j=0
i=0, j=2
i=1, j=0
i=1, j=2
i=2, j=0
i=2, j=2

The unlabeled version skips only j == 1 for each i, so every outer iteration still runs all three inner values except the skipped one.

Continue vs break

continue and break serve different purposes. break terminates the loop entirely; continue skips only the current iteration.

StatementEffectTypical use
breakExits the loopStop processing when a condition is met
continueSkips the current iterationFilter out values that should not be processed

Both can be used with labels in nested loops.

for (int i = 0; i < 10; i++) { if (i == 5) { break; // loop stops entirely } if (i % 2 == 0) { continue; // skip even numbers } System.out.println(i); }

Output:

1
3

The loop stops at i == 5, so numbers 6 through 9 are never reached.

Common Edge Cases and Mistakes

Continue in a switch inside a loop

A continue inside a switch that is inside a loop applies to the loop, not the switch. This can be confusing because break inside a switch only exits the switch, not the loop.

for (int i = 0; i < 5; i++) { switch (i) { case 1: continue; // skips to next loop iteration default: System.out.println("Value: " + i); } }

Output:

Value: 0
Value: 2
Value: 3
Value: 4

Continue in a for-each loop

The enhanced for loop also supports continue. It skips to the next element in the collection or array.

List<String> names = List.of("Alice", "Bob", "Charlie"); for (String name : names) { if (name.startsWith("B")) { continue; } System.out.println(name); }

Output:

Alice
Charlie

Infinite loop risk

The most common bug with continue is placing it before the loop variable update in a while loop. Always ensure the condition can eventually become false.

Runtime Behavior and Performance

continue has no meaningful runtime cost. It is a compile-time control-flow construct that translates to a jump instruction in the bytecode. The JVM does not allocate objects or perform any special work when continue is executed.

The performance consideration is not the cost of continue itself but the structure of the loop. A loop that uses continue to skip many iterations still evaluates the condition on every iteration. If the condition is expensive, the loop pays that cost regardless of how many iterations are skipped.

For example, filtering a large collection with continue is no faster than using a conditional around the body:

// Option A: continue for (Item item : items) { if (!item.isActive()) { continue; } process(item); } // Option B: conditional body for (Item item : items) { if (item.isActive()) { process(item); } }

Both compile to equivalent bytecode. The choice is stylistic. continue can make the loop body flatter when there are multiple conditions that should skip processing.

Maintainability Considerations

continue can reduce nesting, but overuse makes control flow harder to follow. A loop with many continue statements scattered through the body forces the reader to track every jump point.

A practical guideline is to use continue when it removes a level of nesting without hiding the loop's logic. If a loop body has more than two or three continue statements, consider restructuring the condition or extracting the body into a method.

Labeled continue is more powerful but also more dangerous. It can make the control flow of nested loops difficult to reason about. Before using a labeled continue, consider whether extracting the inner loop into a method with an early return would be clearer.

// Instead of labeled continue, extract the inner loop for (int i = 0; i < 3; i++) { processRow(i); } void processRow(int i) { for (int j = 0; j < 3; j++) { if (j == 1) { return; // equivalent to labeled continue for this case } System.out.println("i=" + i + ", j=" + j); } }

This approach avoids the label entirely and keeps the control flow local to each method. The tradeoff is an extra method call per outer iteration, which is negligible in most applications. Choose the labeled form when the inner loop is short and the label makes the intent obvious, and the extracted method when the inner loop contains enough logic to justify its own unit.

java continue statement: Practical Usage and Code Examples | RYUSLOG DEV