Q: _______ statement is used to exit the current loop.
-
A
break
-
B
while
-
C
for
-
D
none of these
A
Answer:
A
Explanation:
The break statement is used to exit the current loop.
Here is an example in Java to demonstrate its usage:
public class BreakStatementExample {
public static void main(String[] args) {
// Loop from 1 to 5
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
System.out.println("Iteration: " + i);
}
System.out.println("Loop exited.");
}
}
Output:
Iteration: 1
Iteration: 2
Loop exited.
In this example, the break statement exits the loop when i equals 3, so the loop stops executing and the program continues with the next statement after the loop.
Related Topic:
Share Above MCQ