Table of Contents
What is a Continue Statement?
A 'continue' statement in Java is a control flow statement that skips the rest of the code inside the current loop iteration and immediately jumps back to the loop's condition check to begin the next iteration. It does not exit the loop; the loop keeps running normally after the skipped iteration. The continue statement can be used inside for loops, while loops, and do-while loops. It is most commonly used when a specific value or condition needs to be ignored during loop execution without stopping the entire loop.Syntax:
Example:continue;
// Java program to implement
// continue statement
public class TutorialforGeeks {
public static void main (String [] args)
{
for (int i = 1; i = 10; i++)
{
if (i == 5)
{
System.out.println("Skipping number: " + i);
continue;
}
System.out.println("Current value of i: " + i);
}
System.out.println("Loop has completed successfully.");
}
}
Output:
Current value of i: 1
Current value of i: 2
Current value of i: 3
Current value of i: 4
Skipping number: 5
Current value of i: 6
Current value of i: 7
Current value of i: 8
Current value of i: 9
Current value of i: 10
Loop has completed successfully.
Explanation:
- The for loop runs from i = 1 to i = 10.
- Inside the loop, an if condition checks whether i == 5.
- For values 1 through 4, the condition is false, so they are printed normally.
- When i reaches 5, the condition becomes true, the skip message is printed, and continue executes.
- The remaining body (the second println) is skipped for this iteration only.
- The loop resumes from i = 6 and continues normally until i = 10.
- Unlike a break, the loop does not exit; it simply skips iteration 5 and carries on.
Key Features of Continue Statement
- Skips Current Iteration Only: The continue statement skips only the remaining code in the current iteration. The loop itself continues running normally from the very next iteration; nothing else is affected.
- Works in All Loop Types: The continue statement works inside all three types of loops in Java: the for loop, the while loop, and the do-while loop. In each case it skips the current iteration and jumps back to the condition check.
- Does Not Exit the Loop: Unlike the `break` statement, `continue` never exits the loop. The loop always completes all its remaining iterations after the continue; it just misses the one where continue was triggered.
- Jumps to Update in For Loop: In a for loop specifically, when 'continue' executes, control jumps to the update expression first (such as 'i++') before going back to the condition check. This ensures the loop variable is always properly updated.
- Jumps Directly to Condition in While and Do-While: In a while loop and do-while loop, when continue executes, control jumps directly back to the condition check without executing any remaining statements in the loop body.
- Supports Labeled Continue: Just like labeled break, Java also supports labeled continue, which allows you to skip an iteration of a specific outer loop from inside a nested inner loop by attaching a label to the outer loop.
Continue in For Loop
The 'continue' statement is used inside a for loop to skip a specific iteration when a condition is met and move on to the next one.Syntax:
Example:for (int i = startValue; i = limit; i++) {
if (skipCondition) {
continue;
}
// loop body (skipped when condition is true)
}
// Java program to implement continue
// statement in for loop
public class TutorialforGeeks {
public static void main (String [] args)
{
System.out.println("Printing odd numbers from 1 to 10:");
for (int i = 1; i = 10; i++)
{
if (i % 2 == 0)
{
continue;
}
System.out.println(i);
}
}
}
Output:
Explanation:Printing odd numbers from 1 to 10:
1
3
5
7
9
- The for loop runs from i = 1 to i = 10.
- The condition i % 2 == 0 checks whether the current number is even.
- When i is even (2, 4, 6, 8, 10), the condition is true and continues to skip the println.
- When i is odd (1, 3, 5, 7, 9), the condition is false, so the number is printed.
- The update expression i++ still runs after every continue, so the loop always progresses normally.
Continue in While Loop
The 'continue' statement is used inside a 'while' loop to skip specific iterations based on a condition while letting the loop run to completion.Syntax:
Example:int i = startValue;
while (condition) {
i++;
if (skipCondition) {
continue;
}
// loop body (skipped when condition is true)
}
// Java program to implement continue
// statement in while loop
public class TutorialforGeeks {
public static void main (String [] args)
{
System.out.println("Skipping multiples of 3 between 1 and 15:");
int i = 0;
while (i 15)
{
i++;
if (i % 3 == 0)
{
System.out.println("Skipping: " + i);
continue;
}
System.out.println("Value: " + i);
}
}
}
Output:
Explanation:Skipping multiples of 3 between 1 and 15:
Value: 1
Value: 2
Skipping: 3
Value: 4
Value: 5
Skipping: 6
Value: 7
Value: 8
Skipping: 9
Value: 10
Value: 11
Skipping: 12
Value: 13
Value: 14
Skipping: 15
- The while loop runs from i = 1 to i = 15 with i++ placed at the very top of the body.
- i++ is placed before the continue condition so the loop variable always gets updated.
- When i is a multiple of 3, the continue statement skips the "Value" println for that iteration.
- For all other values, the number is printed normally.
- The loop completes all 15 iterations; it never exits early like 'break' would.
Continue in Do-While Loop
The 'continue' statement is used inside a do-while loop to skip the remaining body of the current iteration and jump directly to the condition check at the bottom.Syntax:
Example:int i = startValue;
do {
i++;
if (skipCondition) {
continue;
}
// loop body (skipped when condition is true)
} while (condition);
// Java program to implement continue
// statement in do-while loop
public class TutorialforGeeks {
public static void main (String [] args)
{
System.out.println("Skipping number 4 and 8 using do-while with continue:");
int i = 0;
do {
i++;
if (i == 4 || i == 8)
{
System.out.println("Skipping: " + i);
continue;
}
System.out.println("Number: " + i);
} while (i 10);
}
}
Output:
Explanation:Skipping number 4 and 8 using do-while with continue:
Number: 1
Number: 2
Number: 3
Skipping: 4
Number: 5
Number: 6
Number: 7
Skipping: 8
Number: 9
Number: 10
- The do-while loop starts with i = 0 and increments i at the top of the body.
- When i equals 4 or 8, the continue statement executes and skips the "Number" println.
- After continue, control jumps directly to the while (i 10) condition at the bottom.
- For all other values, the number is printed normally.
- The loop completes all 10 iterations and exits naturally when i becomes 10.
Labeled Continue in Nested Loop
A special form of 'continue' that skips an iteration of a specific outer loop directly from inside a nested inner loop using a label.Syntax:
Example:outerLabel:
for (int i = startValue; i = outerLimit; i++) {
for (int j = startValue; j = innerLimit; j++) {
if (skipCondition) {
continue outerLabel;
}
// inner loop body
}
}
// Java program to implement labeled
// continue statement in nested loop
public class TutorialforGeeks {
public static void main (String [] args)
{
System.out.println("Labeled continue example:");
outer:
for (int i = 1; i = 4; i++)
{
for (int j = 1; j = 4; j++)
{
if (j == 3)
{
System.out.println("Skipping rest of outer iteration at i = " + i + ", j = " + j);
continue outer;
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output:
Explanation:Labeled continue example:
i = 1, j = 1
i = 1, j = 2
Skipping rest of outer iteration at i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
Skipping rest of outer iteration at i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
Skipping rest of outer iteration at i = 3, j = 3
i = 4, j = 1
i = 4, j = 2
Skipping rest of outer iteration at i = 4, j = 3
- The label outer is attached to the outer for loop.
- Both loops run normally, printing each combination of i and j.
- When j == 3, the labeled continue continues to execute outer.
- This skips not just the rest of the inner loop body but the entire remaining inner loop for that outer iteration.
- The outer loop then moves to the next value of i, and the inner loop starts fresh from j = 1 again.
- Notice that j = 4 is never printed for any value of i because labeled continue exits the inner loop at j = 3.
How Continue Statement Works?
- Loop Begins Normally: The loop starts executing as it normally would. The loop variable is initialized, the condition is checked, and the loop body begins running for the first iteration.
- Continue Condition is Evaluated: Inside the loop body, an if statement checks a specific condition. When this condition is false, the loop body runs completely, and the iteration finishes normally.
- Continue Statement Executes: When the condition becomes true, the 'continue' statement executes immediately. All remaining statements below the 'continue' in the current loop body are completely skipped for this iteration.
- Control Jumps Back: In a for loop, control jumps to the update expression first (such as 'i++') and then to the condition check. In while and do-while loops, control jumps directly to the condition check.
- Next Iteration Begins: The condition is evaluated again with the updated loop variable. If the condition is still true, the next iteration begins. The loop continues running until all iterations are complete or a break statement is encountered.
Conclusion
The `continue` statement is a simple but very practical control flow tool in Java that gives programmers the ability to skip specific iterations of a loop without stopping the loop entirely. Whether you need to filter out even numbers, skip multiples of a value, ignore invalid input, or bypass a specific condition in a nested loop, the continue statement handles all of these cases cleanly and efficiently. The key difference to always remember is that 'break' exits the loop completely, while 'continue' only skips the current iteration and lets the loop carry on. The labeled continue extends this further by allowing you to skip an entire outer loop iteration from deep inside a nested structure. Mastering the continue statement alongside break gives you complete and fine-grained control over how your loops behave in every situation.Frequently Asked Questions
1. Can the continue statement be used inside a switch block in Java?2. What is the difference between 'continue' and 'return' in Java?No, the continue statement cannot be used directly inside a switch block; it only works inside loops and only affects the loop that directly contains it.
3. Does 'continue' affect the update expression in a for loop?'Continue' skips only the current loop iteration and keeps the loop running, while 'return' exits the entire method immediately regardless of where it is placed.
4. Can labeled continue be used to skip an iteration of the innermost loop?No, 'continue' does not skip the update expression in a for loop; it jumps to the update first and then to the condition, so the loop variable is always properly incremented.
5. Is it possible to use both break and continue inside the same loop in Java?Yes, a labeled continue can target any loop, including the innermost one, but in that case a plain continue without a label would produce the exact same result more simply.
Yes, both 'break' and 'continue' can be used inside the same loop. 'Break' handles the early exit condition, while 'continue' handles the skip condition, giving full control over the loop behaviour.
0 Comments