Java MCQ: What is the purpose of the continue statement in a loop?
Answer:
Explanation:
The continue
statement in Java is used within loops to skip the remaining code in the current iteration and move directly to the next iteration. When the continue
statement is encountered, the loop condition is re-evaluated, and if it is still true, the loop continues with the next iteration. This statement is particularly useful when certain conditions require bypassing specific parts of the loop’s body.
For example, consider the following for
loop:
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
In this example, the loop prints only odd numbers from 1 to 9. When i
is even, the continue
statement skips the System.out.println(i)
statement and moves to the next iteration.
The continue
statement is useful for skipping specific iterations based on conditions within loops, providing more control over the flow of the loop.
Reference links:
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html