Summary: In this tutorial, you will learn about continue statement in Java with the help of examples.

continue Statement in Java

The continue statement is used in a loop control structure to skip the rest of the current iteration and immediately start the next iteration.

The syntax for the continue statement in Java is as follows:

continue;

Here is an example of how the continue statement can be used in a loop:

for (int i = 0; i < 10; i++) {
   if (i == 5) {
      continue;
   }
   System.out.println(i);
}

This code will output the numbers 0 through 9, except for the number 5. When the value of i is 5, the continue statement is executed and the rest of the current iteration is skipped. The loop then proceeds to the next iteration with the value of i set to 6.

Note that the continue statement can only be used within a loop control structure. It cannot be used outside of a loop.

Example: Continue Statement in While Loop

Here is an example of how the continue statement can be used in a while loop:

int i = 0;
while (i < 10) {
   i++;
   if (i == 5) {
      continue;
   }
   System.out.println(i);
}

This code will output the numbers 1 through 9, except for the number 5. When the value of i is 5, the continue statement is executed and the rest of the current iteration is skipped. The loop then proceeds to the next iteration with the value of i set to 6.

The continue statement can also be used in a do-while loop or a for loop.

continue Statement in Nested Loop

The continue statement can be used in a nested loop to skip the rest of the current iteration of the innermost loop and immediately start the next iteration.

Here is an example of how the continue statement can be used in a nested loop:

for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
      if (j == 1) {
         continue;
      }
      System.out.println("i = " + i + ", j = " + j);
   }
}

This code will output the following:

i = 0, j = 0
i = 0, j = 2
i = 1, j = 0
i = 1, j = 2
i = 2, j = 0
i = 2, j = 2

As you can see, when the value of j is 1, the continue statement is executed and the rest of the current iteration of the inner loop is skipped.

The inner loop then proceeds to the next iteration with the value of j set to 2. The outer loop continues to run until its condition is no longer satisfied.

Labeled continue Statement

A labeled continue statement allows you to specify the name of a loop to continue with, rather than just continuing with the innermost loop.

This can be useful when you have nested loops and you want to continue with a loop that is not the innermost one.

Here is an example of how a labeled continue statement can be used:

outer: for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
      if (i == 1 && j == 1) {
         continue outer;
      }
      System.out.println("i = " + i + ", j = " + j);
   }
}

This code will output the following:

i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2

As you can see, when the values of i and j are both 1, the continue statement with the label outer is executed and the rest of the current iteration of the inner loop is skipped. The outer loop then continues with the next iteration, with the value of i set to 2.

To use a labeled continue statement, you must first specify a label for the loop you want to continue with, by placing a label before the loop.

The label is simply an identifier followed by a colon (:). You can then use the continue statement followed by the label to continue with that loop.


Leave a Reply