Java continue Statement
Skip the rest of the current loop iteration in Java with the continue statement, including labeled continue.
continue is break's lighter cousin. Where break says stop the loop entirely, continue says skip the rest of this iteration and start the next one. The loop keeps going.
Basic use
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
System.out.println(i);
}Output:
0
1
3
4When i == 2, continue jumps straight to the loop's update step (i++), skipping the println. The loop then re-checks its condition and proceeds with i == 3.
How loops resume
The behavior depends on the loop kind:
for— runs the update expression, then checks the conditionwhile— checks the condition immediatelydo/while— checks the condition immediately (at the bottom of the loop)- enhanced
for— moves to the next element
A continue in a while that forgets to advance state can quickly become an infinite loop:
int i = 0;
while (i < 5) {
if (i == 2) {
continue; // i never advances — infinite loop!
}
System.out.println(i);
i++;
}This is a classic mistake. Either increment before the continue, or use a for loop so the update step runs automatically.
Filtering elements
The most common use of continue is "skip the elements I don't care about":
int[] nums = {3, 7, 2, 8, 5, 4};
for (int n : nums) {
if (n % 2 != 0) {
continue; // skip odd numbers
}
System.out.println(n);
}Output: 2 8 4.
continue vs. inverting the condition
You can often rewrite a continue as an if that wraps the rest of the body:
for (int n : nums) {
if (n % 2 == 0) {
System.out.println(n);
}
}Functionally identical. Which to prefer is a style call:
- Use
continuewhen there are multiple skip conditions; a series of early-out guards is flatter than deeply nestedifs. - Use a wrapping
ifwhen the skip condition is simple and the body is short.
// continue style — easy to add another skip condition
for (User u : users) {
if (u == null) continue;
if (!u.isActive()) continue;
if (u.isBanned()) continue;
process(u);
}continue only skips one level
Like break, continue affects the innermost enclosing loop:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) continue; // only skips inner iteration
System.out.println(i + "," + j);
}
}Output:
0,0
0,2
1,0
1,2
2,0
2,2To continue the outer loop from inside the inner one, use a labeled continue — covered in labeled statements.
A worked example
What's next
When you combine for loops, you get nested loops — the foundation for working with grids, tables, and 2D arrays.
Practice
What does this loop print? for (int i = 0; i < 5; i++) { if (i == 2) continue; System.out.print(i); }