- A outer
- B inner
- C both a and b
- D none of these
- Share this MCQ
Answer:
C
Explanation:
both a and b
When the "break" statement is executed within the outer loop, then the outer loop will stop.
public class BreakOuterLoopExample { public static void main(String[] args) { // Outer loop for (int i = 1; i <= 3; i++) { System.out.println("Outer loop iteration: " + i); // Inner loop for (int j = 1; j <= 2; j++) { System.out.println(" Inner loop iteration: " + j); if (i == 2 && j == 1) { System.out.println(" Breaking out of the outer loop."); break; // Breaks out of the outer loop } } if (i == 2) { break; // Ensures the outer loop stops after the break in the inner loop } } } }
Output:
Outer loop iteration: 1 Inner loop iteration: 1 Inner loop iteration: 2 Outer loop iteration: 2 Inner loop iteration: 1 Breaking out of the outer loop.
In this example, the break
statement inside the inner loop causes the outer loop to stop when i
is 2 and j
is 1. The outer loop does not continue after this point.
Related Topic: