Which statement is used to stop a loop in Java?

Welcome to Java MCQ (Mulitple Choice Question) series. In this post, we will see one more quiz question on Java Loops.

Which statement is used to stop a loop in Java?

a) return statement
b) continue statement
c) break statement
d) exit statement 

Answer:

c) break statement 

Explanation:

When a break statement is encountered within a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used in all types of loops - for, while, and do-while.

Here is an example:
for (int i = 1; i <= 10; i++) {
    if (i == 6) {
        break;
    }
    System.out.println(i);
}
In this example, the loop will terminate when i equals 6 and it will only print numbers from 1 to 5.

Comments