continue Statement
The continue statement lets you skip the rest of the current loop iteration and jump straight to the next one. Think of it as saying “never mind the rest of this round — let’s move on.”
Basic Syntax
continue;
That’s it. When Java hits a continue, it immediately stops executing the current iteration’s remaining statements and transfers control to the loop’s update expression (in a for loop) or to the condition check (in a while or do-while loop).
continue in a for Loop
The most common use is skipping certain values inside a for loop:
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // skip even numbers
}
System.out.print(i + " ");
}
Output:
1 3 5 7 9
When i is even, continue fires and the System.out.print line is never reached for that iteration. The loop’s increment (i++) still runs before the condition is checked again.
Tip:
continuecan often replace a deeply nestedifblock. Instead of wrapping your main logic in anif, usecontinueto bail out early and keep the indentation flat.
continue in a while Loop
In a while loop, continue jumps directly to the condition check:
int i = 0;
while (i < 10) {
i++;
if (i == 5) {
continue; // skip printing 5
}
System.out.print(i + " ");
}
Output:
1 2 3 4 6 7 8 9 10
Warning: In a
whileloop, make sure any variable that the loop condition depends on is updated before thecontinue. Ifi++were placed after thecontinue, hittingi == 5would create an infinite loop becauseiwould never increment past 5.
continue in a do-while Loop
In a do-while loop, continue jumps to the while (condition) check at the bottom — the body’s remaining statements are skipped, but the condition is still evaluated:
int i = 0;
do {
i++;
if (i == 3) continue; // skip body remainder when i == 3
System.out.print(i + " ");
} while (i < 6);
Output:
1 2 4 5 6
continue in a for-each Loop
continue works the same way inside a for-each loop:
String[] fruits = {"apple", "banana", "cherry", "date", "elderberry"};
for (String fruit : fruits) {
if (fruit.startsWith("b") || fruit.startsWith("d")) {
continue; // skip fruits starting with b or d
}
System.out.println(fruit);
}
Output:
apple
cherry
elderberry
Labeled continue
When you have nested loops, a plain continue only skips the current (innermost) loop’s iteration. If you need to skip an iteration of an outer loop from inside an inner loop, use a labeled continue:
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outer; // skip to next iteration of outer loop
}
System.out.println("i=" + i + ", j=" + j);
}
}
Output:
i=1, j=1
i=2, j=1
i=3, j=1
The label outer: sits directly before the for statement it names. When j == 2, the rest of the inner loop is abandoned and the outer loop increments to the next i. Without the label, only the inner loop iteration would be skipped and j=3 would still print.
Note: Labels are valid Java but use them sparingly. Deeply labeled loops are hard to read. If you find yourself reaching for labeled
continueoften, consider extracting the inner loop into a separate method.
continue vs break
It’s easy to mix these two up. Here’s the key difference at a glance:
| Statement | What it does | Loop continues? |
|---|---|---|
continue | Skips the rest of the current iteration | Yes — the loop moves to the next iteration |
break | Exits the entire loop immediately | No — execution resumes after the loop |
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skips 3, loop keeps going
System.out.print(i + " ");
}
// Output: 1 2 4 5
System.out.println();
for (int i = 1; i <= 5; i++) {
if (i == 3) break; // stops the entire loop at 3
System.out.print(i + " ");
}
// Output: 1 2
Output:
1 2 4 5
1 2
Practical Example: Filtering Input
continue is great for skipping invalid or unwanted data while processing a list:
int[] scores = {85, -1, 92, -5, 78, 100, -3, 66};
int total = 0;
int validCount = 0;
for (int score : scores) {
if (score < 0) {
System.out.println("Skipping invalid score: " + score);
continue;
}
total += score;
validCount++;
}
System.out.println("Average score: " + (total / validCount));
Output:
Skipping invalid score: -1
Skipping invalid score: -5
Skipping invalid score: -3
Average score: 84
This pattern — validate early, continue on bad data, process good data — is common in data-processing pipelines and keeps the main logic uncluttered.
Under the Hood
How the Compiler Translates continue
There is no dedicated continue bytecode instruction in the JVM. Instead, the compiler emits a goto instruction that jumps to the appropriate point in the loop depending on the loop type:
| Loop type | continue jumps to |
|---|---|
for | The update expression (i++) label |
while | The condition check label |
do-while | The condition check label at the bottom |
For example, this for loop:
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
System.out.println(i);
}
compiles to roughly this bytecode structure:
LABEL_condition:
iload_1 // load i
iconst_5
if_icmpge LABEL_end // if i >= 5, exit
LABEL_body:
iload_1
iconst_2
if_icmpne LABEL_update // if i != 2, skip goto
goto LABEL_update // <<< this is the compiled "continue"
LABEL_print:
... // System.out.println(i)
LABEL_update:
iinc 1, 1 // i++
goto LABEL_condition
LABEL_end:
The continue becomes a single goto LABEL_update — jumping over the print but landing right at the increment, which then naturally flows back to the condition.
JIT and Branch Prediction
Modern CPUs execute loops faster when branches are predictable. A continue inside a loop adds a conditional branch. If the continue fires very rarely (e.g., skipping only invalid data in an otherwise clean dataset), the CPU’s branch predictor learns quickly and the overhead becomes negligible. If continue fires frequently (e.g., skipping half the iterations), the JIT compiler may restructure the loop or apply other optimizations. See JIT Compilation & Bytecode and How Loops Work (Bytecode & JIT) for more on how the JVM optimizes loops.
Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
Placing continue after the update variable in a while loop | Infinite loop — the variable never advances | Always update the loop variable before continue |
| Using a label pointing to the wrong loop | Skips the wrong level of nesting | Double-check which loop the label sits in front of |
Confusing continue and break | Logic errors — loop exits when you meant to skip | Remember: continue = next iteration, break = exit loop |
Overusing continue | Hard-to-read code | Use it for simple guard clauses; extract complex logic to methods |
Related Topics
- break Statement — exit a loop entirely, compared to continue’s iteration skip
- for Loop — the most common loop to use continue with
- while Loop — where continue jumps to the condition check
- do-while Loop — post-condition loop and continue interaction
- for-each Loop — using continue to filter collections elegantly
- How Loops Work (Bytecode & JIT) — deep dive into how the JVM compiles and optimizes loop control flow