Sometimes a loop should continue running, but the program needs to skip the remaining statements of the current iteration. Java provides the continue statement for this purpose.
Why Do We Need continue?
Imagine processing a list of numbers where you want to ignore negative values but continue processing all the remaining numbers. Stopping the entire loop would be unnecessary. Instead, the program can skip the current iteration and move to the next one.
The continue statement skips the remaining code in the current loop iteration and moves to the next iteration.
Real-World Analogy
Imagine a teacher checking a list of students. If one student is absent, the teacher does not stop checking the entire list. The teacher simply skips that student and continues with the next student.
Check each student
If student is absent
Skip this student
Continue checking the next student
That is the basic idea behind continue.
Basic Syntax
continue;
When Java reaches continue, it immediately skips the remaining statements in the current iteration and proceeds with the next iteration of the loop.
Simple Example
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
When i becomes 3, the continue statement executes. Java skips the System.out.println() statement for that iteration and moves to the next iteration.
Therefore, the output is 1, 2, 4, and 5.
Remember: continue does not terminate the loop. It only skips the current iteration.
How continue Works in a for Loop
In a for loop, continue skips the remaining body of the current iteration and then moves to the update expression before checking the loop condition again.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
When i is 3, Java executes continue, performs the loop update i++, and then checks the condition again.
Example: Print Only Even Numbers
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue;
}
System.out.println(i);
}
The condition checks whether the number is odd. If it is odd, continue skips the print statement. Therefore, only even numbers are printed.
Example: Skip Negative Numbers
int[] numbers = {10, -5, 20, -2, 30};
for (int number : numbers) {
if (number < 0) {
continue;
}
System.out.println(number);
}
Whenever a negative number is encountered, the current iteration is skipped. Positive numbers continue to the print statement.
continue with while Loop
The continue statement can also be used with a while loop.
int i = 0;
while (i < 5) {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
}
When i becomes 3, the continue statement skips the print statement and the loop proceeds to its next condition check.
When using continue in a while loop, make sure the loop-control variable is updated before the continue can be reached. Otherwise, an infinite loop can occur.
continue with do-while Loop
int i = 0;
do {
i++;
if (i == 3) {
continue;
}
System.out.println(i);
} while (i < 5);
When i becomes 3, the current iteration is skipped. The loop then proceeds to its condition check.
continue vs break
| Statement | Effect |
|---|---|
| continue | Skips the current iteration and continues with the next iteration. |
| break | Immediately terminates the nearest loop. |
This difference is extremely important. Use break when the loop should stop completely. Use continue when the loop should keep running but the current item should be skipped.
Example: break vs continue
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
This program skips only 3 and continues printing 4 and 5.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
This program stops completely when 3 is reached. It does not continue to 4 or 5.
continue in Nested Loops
In nested loops, an ordinary continue applies to the nearest enclosing loop.
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue;
}
System.out.println("i = " + i + ", j = " + j);
}
}
Here, the continue affects the inner loop. When j is 2, the current inner-loop iteration is skipped, while the outer loop continues normally.
Common Beginner Mistakes
- Thinking continue terminates the entire loop.
- Using continue when break is actually required.
- Forgetting to update the loop variable in a while loop.
- Using too many continue statements and making the control flow difficult to follow.
- Forgetting that an ordinary continue in nested loops affects the nearest loop.
Best Practices
- Use continue when skipping a particular category of input or iteration makes the logic clearer.
- Keep the condition that triggers continue easy to understand.
- Be especially careful with loop-variable updates in while and do-while loops.
- Avoid excessive use of continue when a simpler structure would be easier to read.
- Use break when the loop must stop completely.
Interview Insight
A common interview question is: “What is the difference between break and continue?”
The break statement terminates the nearest loop completely. The continue statement only skips the current iteration and allows the loop to continue with the next iteration.
Quick Revision
| Concept | Key Point |
|---|---|
| continue | Skips the current loop iteration. |
| Loop execution | Continues with the next iteration after continue. |
| break | Stops the entire nearest loop. |
| Nested loops | Ordinary continue affects the nearest enclosing loop. |
| while loop | Loop-control variables must still be updated correctly. |
The continue statement gives you fine-grained control over loops by allowing the program to ignore the current iteration without stopping the entire repetition. With break and continue now covered, all the listed chapters in the Control Flow section have been completed.
