What is the purpose of the continue statement in a loop?

Java MCQ: What is the purpose of the continue statement in a loop?

a) To exit the loop immediately
b) To skip the current iteration and move to the next iteration
c) To terminate the program
d) To execute a specific block of code

Answer:

b) To skip the current iteration and move to the next iteration

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top