continue Statement
The continue statement in Java is a loop control statement used to skip the remaining statements of the current iteration and move directly to the next iteration of the loop. It is commonly used to bypass specific conditions without terminating the loop.
What Is the continue Statement?
- Skips the rest of the current loop iteration
- Transfers control to the loop update/condition check
- Used only inside loops (for, while, do-while)
Basic Syntax
continue;
continue in a for Loop
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output:
1
2
4
5
Why it matters: Iteration i == 3 is skipped, not the loop itself.
continue in a while Loop
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
}
Important: Update the loop variable before continue to avoid infinite loops.
continue in a do-while Loop
int i = 0;
do {
i++;
if (i == 2) {
continue;
}
System.out.println(i);
} while (i <= 4);
Labeled continue (Advanced)
Labeled continue skips to the next iteration of an outer loop.
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outer;
}
System.out.println(i + "," + j);
}
}
Why it matters: Useful in nested loops, but should be used sparingly.
continue vs break
| Feature | continue | break |
|---|---|---|
| Purpose | Skip current iteration | Exit loop completely |
| Loop continues | ✅ Yes | ❌ No |
| Used In | Loops only | Loops and switch |
Common Use Cases
- Skip invalid inputs
- Ignore unwanted values
- Filter loop iterations
- Improve readability over nested if
Common Beginner Mistakes
- Forgetting loop variable update (infinite loop)
- Using continue outside loops (compile-time error)
- Overusing labeled continue
- Confusing continue with break
Interview-Ready Answers
Short Answer
The continue statement skips the current iteration of a loop and moves to the next iteration.
Detailed Answer
In Java, the continue statement is used inside loops to skip the remaining code of the current iteration and proceed with the next iteration. Unlike break, it does not terminate the loop.
Key Takeaway
The continue statement provides fine-grained control within loops. Use it to skip specific cases without stopping the entire loop—but apply it carefully to maintain readability.