0% completed
The continue statement in Java is used to skip the current iteration of a loop and proceed with the next iteration. It helps in controlling the flow within loops by bypassing specific portions of code when certain conditions are met, without terminating the entire loop.
continue;
Below is an example that demonstrates the use of continue within a for loop. The loop prints numbers from 1 to 5 but skips the number 3.
Explanation:
i
equals 3, the continue statement is triggered, so the remaining code within the loop is not executed for that iteration.Below is an example that shows how the continue statement can be used in a while loop to bypass certain conditions. In this example, odd numbers are skipped, and only even numbers are printed.
Explanation:
number
is less than or equal to 10.number
is odd (checked using number % 2 != 0
), the continue statement is executed and the remaining code for that iteration is skipped.By using the continue statement, you can fine-tune your loop's behavior by skipping unnecessary iterations under specific conditions. This helps in creating more efficient and readable code. Experiment with these examples to see how continue works within different loop structures.
.....
.....
.....