Continue Statement in Java

In this lesson of our Java Course, we will look at Continue Statement in Java.One of the key control flow statements in Java is the “continue” statement. This statement is used to skip a specific iteration of a loop and continue with the next one. In this article, we will discuss how the Java continue statement works, how it can be used with nested loops, and the labeled continue statement.

1. Continue Statement in Java

The Java continue statement is used to skip the current iteration of a loop and move on to the next one. When the continue statement is encountered, the program execution jumps to the next iteration of the loop. This means that any code following the continue statement within the loop will not be executed.

The continue statement is typically used in conjunction with an if statement, which checks a specific condition before deciding whether or not to skip the current iteration. For example, the following code uses the continue statement to skip any odd numbers in a for loop:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 1) {
        continue;
    }
    System.out.println(i);
}

How Java Continue Statement Works?

Continue Statement in Java
Java Continue Statement

2. Using Java Continue with Nested Loops

Java continue statement can also be used with nested loops. When a continue statement is encountered within a nested loop, it will only skip the current iteration of the innermost loop. For example, the following code uses nested loops and the continue statement to print a multiplication table:

for (int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 10; j++) {
        if (j == 5) {
            continue;
        }
        System.out.print(i * j + " ");
    }
    System.out.println();
}

The above code will print the multiplication table, but the fifth column will be missing.

Java Continue with Nested Loops
Java Continue with Nested Loops

3. Using Labelled Continue Statement

Java also provides the ability to use labelled continue statements, which allow us to skip an iteration of a specific loop, even if it’s nested within other loops. To use a labelled continue statement, we first need to assign a label to the loop, and then use that label in the continue statement. For example:

outer: for (int i = 1; i <= 10; i++) {
    for (int j = 1; j <= 10; j++) {
        if (j == 5) {
            continue outer;
        }
        System.out.print(i * j + " ");
    }
    System.out.println();
}

This code will skip the 5th column of the multiplication table, but continue with the outer loop.

Using Java Labelled Continue Statement
Using Java Labelled Continue Statement

Summary

The Java continue statement is a powerful control flow statement that can be used to skip over specific iterations of a loop. It can be used in conjunction with an if statement to check specific conditions, and can also be used with nested loops to skip specific iterations of an inner loop. Additionally, labelled continue statements can be used to skip over specific iterations of a specific loop, even if it’s nested within other loops. Understanding how to use the continue statement can help us write more efficient and effective code. You can find the source code on our GitHub Repository.