continue Statement in Java
0 120
Understanding the continue Statement in Java
The continue Statement in Java is a control flow tool used inside loops to skip the current iteration and immediately jump to the next one. It provides a way to selectively bypass certain parts of the loop body based on conditions without stopping the entire loop.
Where Can You Use continue?
The continue
statement can be applied in all types of loops in Java, including for
, while
, and do-while
loops. It is useful when you want to ignore specific cases but still continue looping over the remaining elements.
Syntax of continue Statement
The syntax is quite simple:
continue;
When this statement executes, the current loop iteration ends immediately, and the program proceeds with the next iteration, re-evaluating the loop condition as usual.
Example: Using continue in a for Loop
Consider this example where we print numbers from 1 to 10, but skip printing multiples of 3:
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) {
continue; // Skip the rest of the loop body for multiples of 3
}
System.out.println("Number: " + i);
}
Output:
Number: 1
Number: 2
Number: 4
Number: 5
Number: 7
Number: 8
Number: 10
Using continue in while Loop
Here’s how the continue
statement works in a while
loop:
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue; // Skip even numbers
}
System.out.println("Odd number: " + i);
}
Why Use continue Statement?
- To skip specific iterations without breaking the whole loop.
- Helps keep the loop logic clean by avoiding deeply nested
if
statements. - Improves readability and flow control in loops.
Important Notes
Be cautious when using continue
inside nested loops, as it only affects the innermost loop where it appears. Misusing it can sometimes make the logic harder to follow, so use it judiciously.
Conclusion
The continue Statement in Java is a handy tool to control loop execution by skipping unwanted iterations while continuing the loop itself. When used correctly, it simplifies loop management and enhances code readability in your Java programs.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments