Java continue Statement

In this tutorial, we will learn how to use the Java continue statement to skip the current iteration of a loop.

While working with loops, sometimes you might want to skip some statements or terminate the loop. In such cases, break and continue statements are used.

The continue statement skips the current iteration of the loop (for, while, do...while, etc).


1. Java continue statement

The syntax of the continue statement is:

continue;

Example 1: Java continue statement

class Main {
  public static void main(String[] args) {

    // for loop
    for (int i = 1; i <= 10; ++i) {

      // if value of i is between 4 and 9
      // continue is executed
      if (i > 4 && i < 9) {
        continue;
      }
      System.out.println(i);
    }
  }
}

Output:

1
2
3
4
9
10

2. Labeled continue Statement

Similar to the labeled break statement, we can use the labeled continue statement to skip the current iteration of the outer loop.

class Main {
    public static void main(String[] args) {

        first:
        for (int i = 1; i < 6; ++i) {
            for (int j = 1; j < 5; ++j) {
                if (i == 3 || j == 2)
                    continue first;
                System.out.println("i = " + i + "; j = " + j);
            }
        }
    }
}

Here, the continue first; statement skips the current iteration of the loop labeled first.


Key Takeaways

  • Action: Skips the rest of the current iteration.
  • Flow: Jumps to the next iteration (update step in for, condition in while).
  • Labeled Continue: Used to skip iteration of an outer loop.

Common Pitfalls

[!WARNING] Infinite While Loop: In a while loop, if you continue before incrementing your counter (e.g., i++), the loop will get stuck on the same value forever!

Challenge

Challenge

Task:

Use continue to skip printing 3 in a loop from 1 to 5.