Continue Statement in Java
Quick Answer
Continue Statement explains the continue statement in Java is a control flow statement used inside loops to skip the current iteration and proceed with the next one.
Learning Objectives
- Explain the purpose of Continue Statement in a practical learning context.
- Identify the main ideas, terms, and decisions involved in Continue Statement.
- Apply Continue Statement in a simple real-world scenario or practice task.
Introduction
The continue statement in Java is a control flow statement used inside loops to skip the current iteration and proceed with the next one.
It helps manage loop execution by allowing selective skipping of code blocks within loops based on conditions.
Control your loops, control your program flow.
Understanding the Continue Statement
The continue statement is used inside loops such as for, while, and do-while to skip the rest of the current iteration.
When the continue statement executes, the loop immediately jumps to the next iteration, bypassing any code that follows it within the loop body.
- Works with for, while, and do-while loops.
- Skips remaining code in the current iteration.
- Proceeds to the next iteration of the loop.
Syntax of Continue Statement
The syntax of the continue statement is straightforward and does not require any parameters.
It can be used as a simple statement within the loop body.
- continue;
Using Continue in Different Loops
The continue statement behaves slightly differently depending on the type of loop it is used in.
In for loops, it skips to the update expression, then checks the loop condition again.
In while and do-while loops, it skips directly to the loop condition check.
- In for loops: skips to the increment/decrement step.
- In while loops: skips to the condition evaluation.
- In do-while loops: skips to the condition evaluation after the loop body.
Examples of Continue Statement
Let's look at practical examples demonstrating how the continue statement works in Java loops.
Example 1: Using Continue in a For Loop
This example prints numbers from 1 to 10 but skips printing the number 5 using the continue statement.
Example 2: Using Continue in a While Loop
This example uses a while loop to print numbers from 1 to 10 but skips even numbers using continue.
When to Use Continue Statement
The continue statement is useful when you want to skip certain iterations based on a condition without exiting the loop entirely.
It helps keep the code clean by avoiding nested if-else blocks.
- Skip processing for specific values or conditions.
- Improve readability by reducing nested conditions.
- Control loop flow precisely.
Practical Example
This loop prints numbers from 1 to 10 but skips printing 5 by using continue.
This while loop prints only odd numbers from 1 to 10 by skipping even numbers with continue.
Examples
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the number 5
}
System.out.println(i);
}This loop prints numbers from 1 to 10 but skips printing 5 by using continue.
int i = 1;
while (i <= 10) {
if (i % 2 == 0) {
i++;
continue; // Skip even numbers
}
System.out.println(i);
i++;
}This while loop prints only odd numbers from 1 to 10 by skipping even numbers with continue.
Best Practices
- Use continue to simplify loop logic and avoid deep nesting.
- Ensure the loop variable is updated properly to avoid infinite loops when using continue.
- Use continue sparingly to maintain code readability.
Common Mistakes
- Forgetting to update loop variables before continue, causing infinite loops.
- Using continue outside loops, which causes compilation errors.
- Overusing continue leading to confusing and hard-to-read code.
Hands-on Exercise
Skip Multiples of 3
Write a for loop that prints numbers from 1 to 20 but skips multiples of 3 using the continue statement.
Expected output: Numbers from 1 to 20 excluding 3, 6, 9, 12, 15, and 18.
Hint: Use the modulus operator (%) to check for multiples of 3.
Print Odd Numbers Using Continue
Use a while loop to print only odd numbers between 1 and 15 by skipping even numbers with continue.
Expected output: 1 3 5 7 9 11 13 15
Hint: Check if a number is even and use continue to skip it.
Interview Questions
What does the continue statement do in Java?
InterviewThe continue statement skips the current iteration of a loop and proceeds with the next iteration.
Can continue be used outside of loops?
InterviewNo, the continue statement can only be used inside loops such as for, while, and do-while.
What is the difference between continue and break statements?
InterviewThe continue statement skips the current iteration and continues the loop, while break exits the loop entirely.
MCQ Quiz
1. What is the primary function of the continue statement in Java loops?
Select one option to check your answer.
2. In a for loop, what happens immediately after a continue statement is executed?
Select one option to check your answer.
3. Which of the following is a common mistake when using the continue statement in loops?
Select one option to check your answer.
4. Can the continue statement be used outside of loops in Java?
Select one option to check your answer.
5. How does the continue statement behave differently in while and do-while loops compared to for loops?
Select one option to check your answer.
Key Takeaways
- The continue statement in Java is a control flow statement used inside loops to skip the current iteration and proceed with the next one.
- It helps manage loop execution by allowing selective skipping of code blocks within loops based on conditions.
- The continue statement is used inside loops such as for, while, and do-while to skip the rest of the current iteration.
- When the continue statement executes, the loop immediately jumps to the next iteration, bypassing any code that follows it within the loop body.
- The syntax of the continue statement is straightforward and does not require any parameters.
Frequently Asked Questions
Can continue be used in nested loops?
Yes, continue affects only the innermost loop in which it is used.
What happens if continue is used without a loop?
Using continue outside a loop results in a compilation error.
Is continue the same as break?
No, continue skips to the next iteration, while break exits the loop completely.
Summary
The continue statement in Java is a useful control flow tool to skip the current iteration of a loop and proceed to the next one.
It works with all loop types and helps simplify loop logic by avoiding unnecessary nested conditions.
Proper use of continue improves code readability and control over loop execution.





