Java continue

Summary: in this tutorial, you will learn how to use the Java continue statement to start a new iteration prematurely.

Introduction to the Java continue statement

In Java, the continue statement allows you to skip the current iteration of a loop and move to the next iteration:

continue;Code language: Java (java)

You can use the continue statement within a while loop, do while loop, and for loop.

When Java encounters the continue statement within a loop, it immediately transfers the control to the next iteration and skips the remaining code within the current iteration.

Typically, you use the continue statement with an if statement to control the flow of a loop when a certain condition is met, without executing the remaining code within the loop for that specific iteration.

if(condition) {
   continue;
}Code language: Java (java)

Using Java continue in for loop

Here’s the syntax for using the continue statement within a for loop:

for(initialization; condition; update) {
    //...
    if(skipCondition) {
       continue;
    }
    //...
}Code language: Java (java)

The following program illustrates how to use the continue statement inside a for loop:

public class App {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i == 2) {
                // Skip iteration when i is 2
                continue;
            }
            System.out.println(i);
        }
    }
}Code language: Java (java)

Output:

1
3
4
5Code language: Java (java)

In this example, when i is equal to 2, the condition i == 2 becomes true. As a result, the continue statement is executed and the loop skips to the current iteration without printing the number 2.

Using Java continue in while loop

The following shows how to use the continue statement inside a while loop:

while(condition) {
    // ... 
    if(skipCondition) {
        continue;
    }
    // ...
}Code language: Java (java)

For example, the following program uses a continue statement inside a while loop to display the odd numbers:

public class App {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            // Skip even numbers
            if (i % 2 == 0) {
                i++;
                continue;
            }
            // Display odd numbers
            System.out.println(i);
            i++;
        }
    }
}Code language: Java (java)

When the i becomes even such as 2 and 4, the condition i % 2 == 0 becomes true. The if block increases the value of i by one and executes the continue statement.

The continue statement skips the remaining code for the iterations that i is even. Therefore, the loop only displays the odd numbers.

Using Java continue in a do while loop

Here’s the syntax for using the continue statement inside a do while loop:

do {
    // ... 
    if(skipCondition) {
        continue;
    }
    // ...
} while(condition);Code language: Java (java)

The following example uses a continue statement inside a do while loop to display the odd numbers:

public class App {
    public static void main(String[] args) {
        int i = 1;
        do {
            // Skip even numbers
            if (i % 2 == 0) {
                i++;
                continue;
            }
            // Display odd numbers
            System.out.println(i);
            i++;
        } while (i <= 5);
    }
}Code language: Java (java)

Output:

1
3
5Code language: Java (java)

Summary

  • Use Java continue statement to skip the current iteration of a loop and move to the next iteration immediately.