Continue Statement

Java continue Statement

The continue statement in Java is a control flow statement that allows you to skip the current iteration of a loop and move on to the next iteration. It is particularly useful when you want to bypass certain parts of a loop based on a condition while still continuing the loop's execution. In this tutorial, we'll explore how the continue statement works in different contexts and provide detailed examples to help you understand its usage.


1. What is a continue Statement?

The continue statement is used to skip the remaining code in the current iteration of a loop and immediately proceed to the next iteration. It is applicable in for, while, and do-while loops. When the continue statement is encountered, the control flow jumps to the next iteration of the loop, bypassing any code that follows the continue statement in the current iteration.

Syntax:

continue;

2. Using continue in Loops

The continue statement is often used in loops to skip certain iterations based on a condition. This can be helpful when you want to avoid executing specific code for particular cases while still allowing the loop to continue.

2.1 Example: continue in a for Loop

Let's start with a simple example where we use the continue statement to skip printing the number 5 in a for loop:

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        continue;  // Skip the iteration when i is 5
    }
    System.out.println("i: " + i);
}

Explanation:

  • The loop starts with i equal to 1 and continues until i is less than or equal to 10.

  • When i reaches 5, the continue statement is executed, and the loop skips printing i for that iteration.

Output:

i: 1
i: 2
i: 3
i: 4
i: 6
i: 7
i: 8
i: 9
i: 10

In this example, the number 5 is skipped, and the loop continues with the next iteration.

2.2 Example: continue in a while Loop

You can also use the continue statement in a while loop to skip certain iterations:

int i = 1;

while (i <= 10) {
    if (i == 5) {
        i++;  // Increment i before continue to avoid an infinite loop
        continue;  // Skip the iteration when i is 5
    }
    System.out.println("i: " + i);
    i++;
}

Explanation:

  • The loop starts with i equal to 1 and continues until i is less than or equal to 10.

  • When i reaches 5, the continue statement is executed, and the loop skips printing i for that iteration.

Output:

i: 1
i: 2
i: 3
i: 4
i: 6
i: 7
i: 8
i: 9
i: 10

In this example, the number 5 is skipped, and the loop continues with the next iteration.

2.3 Example: continue in a do-while Loop

The continue statement works similarly in a do-while loop:

int i = 1;

do {
    if (i == 5) {
        i++;  // Increment i before continue to avoid an infinite loop
        continue;  // Skip the iteration when i is 5
    }
    System.out.println("i: " + i);
    i++;
} while (i <= 10);

Output:

i: 1
i: 2
i: 3
i: 4
i: 6
i: 7
i: 8
i: 9
i: 10

In this example, the number 5 is skipped, and the loop continues with the next iteration.


3. Nested Loops and continue

When you have nested loops, the continue statement will only affect the innermost loop in which it is used. If you want to skip iterations in an outer loop, you need to use labeled continue statements.

3.1 Example: continue in Nested Loops

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            continue;  // Skip the iteration when j is 2
        }
        System.out.println("i: " + i + ", j: " + j);
    }
}

Explanation:

  • The outer loop iterates through i values from 1 to 3.

  • The inner loop iterates through j values from 1 to 3.

  • When j equals 2, the continue statement skips the current iteration of the inner loop, but the outer loop continues.

Output:

i: 1, j: 1
i: 1, j: 3
i: 2, j: 1
i: 2, j: 3
i: 3, j: 1
i: 3, j: 3

In this example, the inner loop skips printing when j equals 2, but the outer loop continues as normal.


4. Labeled continue Statement

In situations where you want to skip iterations in an outer loop from within a nested loop, you can use a labeled continue statement. A label is simply an identifier followed by a colon (:) placed before a loop.

Example:

outerLoop:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            continue outerLoop;  // Skip the current iteration of the outer loop when j is 2
        }
        System.out.println("i: " + i + ", j: " + j);
    }
}

Explanation:

  • The label outerLoop is attached to the outer loop.

  • When j equals 2, the continue outerLoop statement skips the current iteration of the outer loop, not just the inner loop.

Output:

i: 1, j: 1
i: 2, j: 1
i: 3, j: 1

In this example, both loops skip the iteration when j equals 2 because the labeled continue statement affects the outer loop.


Conclusion

The continue statement is a useful tool in Java that allows you to control the flow of your loops by skipping specific iterations. Whether you're using it to skip certain values in a loop, manage complex nested loops, or control the flow with labeled continue statements, mastering the continue statement will make your code more efficient and easier to understand.

For more Java tutorials and resources, visit codeswithpankaj.com.

Last updated