Java > Core Java > Control Flow > Break and Continue Statements
Continue Statement Example: Skipping Odd Numbers
This example demonstrates how the continue
statement can be used to skip certain iterations of a loop based on a condition. Here, we print only the even numbers from a sequence.
Code Snippet: Continue Statement
The code iterates through numbers 1 to 10. Inside the loop, it checks if the current number `i` is odd using the modulo operator (`%`). If `i % 2` is not equal to 0, it means the number is odd. In this case, the `continue` statement is executed, which skips the rest of the current iteration and proceeds to the next iteration of the loop. If the number is even, the `System.out.println` statement is executed, printing the even number.
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue; // Skip odd numbers
}
System.out.println("Even number: " + i);
}
}
}
Concepts Behind the Snippet
The continue
statement skips the rest of the current iteration of a loop (for
, while
, or do-while
) and proceeds to the next iteration. It's useful when you want to avoid executing certain parts of the loop body based on a specific condition, without terminating the entire loop.
Real-Life Use Case
Processing a stream of data where you want to ignore certain data points based on validation rules. For instance, skipping records with invalid data formats during data processing.
Best Practices
Use continue
statements to improve code clarity when skipping specific iterations is a natural part of the algorithm. Avoid using continue
in ways that make the loop's logic harder to follow. Use descriptive comments to explain why certain iterations are being skipped.
Interview Tip
Be prepared to differentiate between break
and continue
, explaining their respective effects on loop execution. Understand how continue
affects the flow of control within a loop.
When to Use Them
Use continue
when: You want to skip certain iterations based on a condition. You want to process only certain elements in a collection. You want to avoid executing specific parts of the loop body under certain conditions.
Alternatives
Instead of using `continue`, you can usually invert the condition and wrap the code that you want to execute in an `if` statement. For example, instead of `if (condition) continue; // ... rest of the loop body`, you could write `if (!condition) { // ... rest of the loop body }`. This alternative might be more readable in some cases, but it can also lead to deeper nesting.
Pros
Improves code readability by skipping certain iterations based on some defined conditions. Avoids complex conditional logic inside the loop.
Cons
Can make the code harder to follow if overuse. Invert the condition and wrap the code within an `if` statement is a suitable alternative in most cases.
FAQ
-
What happens if I use
continue
inside a nested loop?
Thecontinue
statement will only skip the rest of the current iteration of the innermost loop in which it is used. It will not affect the outer loops. -
Is it always necessary to use
continue
?
No, often you can achieve the same result by restructuring your loop's condition or usingif
statements to conditionally execute code. However,continue
can sometimes make the code more concise and readable when skipping iterations is a natural part of the logic.