Java continue Statement


In Java, the continue statement is used to skip the current iteration of a loop and continue with the next iteration. This can be helpful when certain conditions within a loop make it unnecessary to execute the remaining statements for that particular iteration, but you want the loop to continue running for the next iterations.

The continue statement works differently than the break statement, which terminates the loop altogether. Instead, the continue allows the loop to skip the current iteration and proceed to the next iteration based on the loop's condition.

In this guide, we'll explore the continue statement in detail, its syntax, usage in loops, and provide examples to illustrate its behavior.

Table of Contents

  1. What is the Java continue Statement?
  2. Syntax of the continue Statement
  3. How the continue Statement Works in Loops
  4. Java continue Statement Example
  5. Best Practices for Using the continue Statement

What is the Java continue Statement?

The continue statement is used inside loops to skip the current iteration and move to the next iteration of the loop. When the continue statement is executed, the remaining code inside the loop for that iteration is skipped, and the loop proceeds to the next iteration based on the condition.

Unlike the break statement, which terminates the loop entirely, the continue statement only affects the current iteration.


Syntax of the continue Statement

The syntax of the continue statement is simple:

continue;
  • The continue statement can be used without any condition, and it will simply skip the remaining code inside the loop and move to the next iteration.

In loops with multiple conditions, such as for or while loops, the continue statement can be particularly useful to skip the rest of the current iteration when certain conditions are met.


How the continue Statement Works in Loops

When the continue statement is encountered inside a loop, it immediately skips the remaining part of the current iteration and proceeds to the next iteration of the loop.

In a for loop:

  1. The loop’s condition is checked.
  2. If the continue statement is encountered, the remaining part of the current iteration is skipped, and the control moves to the next iteration. The loop’s increment/decrement statement is executed before checking the condition again.

In a while or do...while loop:

  1. The loop condition is checked first.
  2. If the continue statement is executed, it skips the remaining part of the loop’s body and goes directly to the next iteration.

Java continue Statement Example

Let’s explore some examples to understand how the continue statement works in various loop types.

Example 1: Using continue in a for Loop

In this example, we use the continue statement inside a for loop to skip printing even numbers and print only odd numbers between 1 and 10.

public class ContinueInForLoop {
    public static void main(String[] args) {
        // Loop from 1 to 10
        for (int i = 1; i <= 10; i++) {
            // Skip even numbers
            if (i % 2 == 0) {
                continue; // Skip the current iteration if i is even
            }
            System.out.println(i); // Print the odd numbers
        }
    }
}

Explanation:

  • The loop runs from 1 to 10.
  • When i is even, the continue statement is executed, skipping the System.out.println(i) statement for that iteration.
  • The program prints only the odd numbers.

Output:

1
3
5
7
9

Example 2: Using continue in a while Loop

In this example, we use the continue statement inside a while loop to skip printing numbers divisible by 3.

public class ContinueInWhileLoop {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 10) {
            if (i % 3 == 0) {
                i++; // Increment before continue to avoid infinite loop
                continue; // Skip numbers divisible by 3
            }
            System.out.println(i);
            i++;
        }
    }
}

Explanation:

  • The while loop runs as long as i is less than or equal to 10.
  • If i is divisible by 3, the continue statement is triggered, skipping the System.out.println(i) statement.
  • The program prints numbers that are not divisible by 3.

Output:

1
2
4
5
7
8
10

Example 3: Using continue in a nested Loop

In this example, we use the continue statement inside a nested loop to skip printing even numbers in the inner loop.

public class ContinueInNestedLoop {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 5; j++) {
                // Skip even numbers in the inner loop
                if (j % 2 == 0) {
                    continue;
                }
                System.out.println("i = " + i + ", j = " + j);
            }
        }
    }
}

Explanation:

  • The outer loop runs from 1 to 3.
  • The inner loop runs from 1 to 5.
  • The continue statement skips even numbers in the inner loop, printing only the odd values of j.

Output:

i = 1, j = 1
i = 1, j = 3
i = 1, j = 5
i = 2, j = 1
i = 2, j = 3
i = 2, j = 5
i = 3, j = 1
i = 3, j = 3
i = 3, j = 5

Best Practices for Using the continue Statement

  1. Use continue for specific conditions: The continue statement is useful when you want to skip certain iterations based on specific conditions. For example, skipping even numbers or invalid inputs in a loop.

  2. Keep your loops readable: Overusing the continue statement can make your loops harder to read and understand. Use it when necessary but avoid excessive complexity.

  3. Control the flow clearly: Make sure that the condition for skipping the iteration is clearly defined and understood by others reading your code.

  4. Avoid infinite loops: When using continue, always ensure that the loop's condition will eventually be satisfied to avoid creating an infinite loop.