continue statement in Java with examples

Have you ever wanted to skip certain loop iterations in Java? Perhaps you encountered a specific condition that, when met, makes continuing with the rest of the loop's code for that iteration unnecessary. This is where the "continue" statement enters the picture. In this article, we'll look at the Java "continue" statement and how it can be used to optimize code flow in loops.

What is the continue statement used for in Java?

The "continue" statement in Java is used to move on to the next iteration of a loop while skipping the current one. In loops like the for, while, and do-while loops, it is frequently employed.

Java continue statement syntax

The general form or syntax of the "continue" statement in Java is as follows:

continue;

That is, the "continue" keyword followed by a semicolon makes it a "continue statement".

When the "continue" statement is encountered within a loop, the loop immediately proceeds to the next iteration, disregarding any code that follows the "continue" statement within the loop block.

Java continue statement example

Consider the following Java program as an example demonstrating the "continue" statement:

Java Code
public class ContinueStatementExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                continue;     // skip the iteration when i=5
            }
            System.out.print(i + " ");
        }
    }
}
Output
1 2 3 4 6 7 8 9 10

This program iterates through the numbers from 1 to 10 using a "for" loop. The "if" clause within the loop determines whether the value of "i" at the moment equals 5. If so, the loop skips the current iteration and moves on to the next one by executing the "continue" statement.

You can see that when the value of "i" is 5, then the "System.out.print()" statement is not executed. So, excluding "5", all the values from 1 to 10 are printed.

Some additional thoughts on the continue statement:

break vs. continue example in Java

Java Code
public class BreakVsContinueExample {
   public static void main(String[] args) {
   int i;
    
   System.out.println("Using the 'break' statement:\n");
      for (i = 1; i <= 10; i++) {
         if (i == 5) {
            break;
         }
         System.out.print(i + " ");
      }
    
   System.out.println("\nUsing the 'continue' statement:\n");
      for (i = 1; i <= 10; i++) {
         if (i == 5) {
            continue;
         }
      System.out.print(i + " ");
   }
}
Output
Using the 'break' statement:

1 2 3 4 

Using the 'continue' statement:

1 2 3 4 6 7 8 9 10 

The break statement is used in the first loop to stop the loop when "i" equals 5. As a result, it only prints the values of "i" ranging from 1 to 4.

When "i" equals 5, the second loop uses the continue statement to skip the iteration. As a result, it prints the "i" values from 1 to 4 and then from 6 to 10.

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!