Continue

In Java, the continue statement is used within loops (such as for, while, or do-while) to skip the remaining code inside the loop for the current iteration and proceed to the next iteration. This control flow statement is very useful when you need to skip certain conditions and continue processing without completely exiting the loop.

Syntax

It can be used in any loop structure where you want to skip the current iteration and move to the next one.

Examples

  • For loops: When the continue statement is encountered, it immediately skips the rest of the code inside the loop and jumps to the next iteration of the loop. The loop’s condition is re-evaluated before proceeding.

Output

The loop prints numbers from 1 to 10, but when i equals 5, the continue statement is executed. This causes the loop to skip the System.out.println(i) for that iteration, so 5 is never printed

  • While and do-while loops: Similarly, in while and do-while loops, the continue statement causes the loop to skip the rest of the current iteration and check the loop condition to decide whether to proceed with the next iteration.

Output

The loop prints numbers from 1 to 10, but when i equals 7, the continue statement is encountered, which skips the printing of 7. The value of i is incremented before the continue is executed to avoid an infinite loop.