Java Continue Statement with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The continue statement in Java jumps immediately to the next iteration of a loop, skipping the remaining code in the current iteration. This allows you to selectively execute code within loop structures like for, while, and do-while loops.

The Java continue statement is a vital control statement that allows you to alter the program execution flow in loops. It works somewhat like a break statement, but instead of terminating the loop entirely, it simply skips the remaining code in the current iteration and then continues with the next one.

Some common use cases of continue are:

  • Skipping an iteration when a specific condition is met
  • Jumping to the next loop cycle when some exception occurs
  • Avoiding execution of code blocks within loops

When executed, the continue statement immediately jumps to the next iteration, skipping any remaining statements in the body of the loop in the current iteration. This, in effect, skips one cycle of the enclosing loop structure.

The continue statement can be used with all types of loops in Java – for loops, while loops and do-while loops.

Syntax of Java Continue Statement

The syntax of the continue statement is straightforward:

Continue;

The continue keyword is followed by a semicolon. It does not need any condition or expression.

Example: Continue in For Loop

Here is a simple example demonstrating the use of continue in a for loop:

public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                continue;
            }
            System.out.print(i + " ");
        }
    }
}

Output:
1 2 3 4 6 7 8 9 10

In this loop, the continue statement is executed when the value of i becomes 5. The loop then jumps back to the beginning, skipping the print statement for this iteration.

Continue in Nested Loops

The continue statement can also be used in nested loops with an inner loop and an outer loop.

For example:

public class ContinueExample2 {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    continue;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}

Here, when i = 2 and j = 2, the continue statement skips the current iteration of the inner loop and continues with the next value of j.

Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

So, the continue statement skipped the iteration for i = 2 and j = 2 in the inner loop.

Continue with Labeled Loops

From JDK 1.5 onwards, Java also supports labeled continue statements. This allows you to continue an outer loop instead of the innermost one selectively.

The syntax is:

label: 

for () {

  //inner loop

  continue label;

}

For example:

public class ContinueExample3 {
    public static void main(String[] args) {
        outer: for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    continue outer;
                }
                System.out.println(i + " " + j);
            }
        }
    }
}

Now, the continue statement with the label outer will continue the outer loop instead of the inner loop.

Output:

1 1
1 2
1 3
3 1
3 2
3 3

Here, the entire iteration for i = 2 was skipped.

Continue in While Loop

The continue statement can also be used in while loops:

public class ContinueWhileExample {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            i++;
            if (i == 5) {
                continue;
            }
            System.out.print(i + " ");
        }
    }
}

This will print from 1 to 10, except 5.

When i becomes 5, the continue statement executes, skipping the remaining statements in that iteration and jumping back to the start of the loop for the next iteration.

Output:
1 2 3 4 6 7 8 9 10

Continue in Do-While Loop

Similarly, you can use continue in do-while loops as well:

public class ContinueDoWhileExample {
    public static void main(String[] args) {
        int i = 0;
        do {
            i++;
            if (i == 5) {
                continue;
            }
            System.out.print(i + " ");
        } while (i < 10);
    }
}

Output:
1 2 3 4 6 7 8 9 10

Here, the continue statement works identically as in the while loop, skipping over the print statement for just the iteration when i = 5.

Conclusion

The continue statement is a helpful tool for loop control in Java. It immediately jumps to the next iteration of a loop based on a condition, allowing selective execution of code statements within loop structures.

The Java continue statement works with for, while, and do-while loops. Labeled continues can also be used selectively to jump outer loops. This provides fine-grained control over complex nested loop structures.

Understanding the continue statement is essential for efficient Java programming and control flow management in iterative logic.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

Leave a Reply

Your email address will not be published. Required fields are marked *