Java Continue Statement

The "continue" statement in Java skips the current iteration of a loop and moves to the next iteration. It can be used in "for", "while", and "do-while" loops.

Table of Contents

The "continue" statement only stops the current iteration, but the loop continues to execute until the termination condition is met.

Examples

Here are some examples of using the "continue" statement in Java:

for loop

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

This code will print all the numbers from 0 to 9 except for the number 5.

while loop

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

This code will print all the numbers from 0 to 9 except for the number 5.

do-while loop

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

This code will print all the numbers from 0 to 9 except for the number 5.