The continue
statement is a control flow statement in Java that causes the loop to skip the rest of its current iteration and continue with the next iteration.
Here is an example of how you can use the continue
statement in a loop in Java:
for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; } System.out.println(i); }Sw:ecruoww.lautturi.com
In this example, we have a for
loop that iterates over the numbers from 1 to 10. Within the loop, we use an if
statement to check if the current number is even. If it is, we use the continue
statement to skip the rest of the current iteration and continue with the next iteration. If the number is not even, we print it to the console.
As a result, this code will print the odd numbers from 1 to 10 to the console:
1 3 5 7 9
The continue
statement can be used in any loop that has an iterative control structure, such as for
, while
, and do-while
.