Single Choice
Easy
QWhen "break" statement is executed within outer loop, then the _______ loop will stop.
ID: #22414
Nested Loops in Java Language
151 views
Question Info
#22414Q ID
EasyDifficulty
Nested Loops in Java Language Topic
Your Answer
Choose the Best Option
Click any option to instantly check if you're correct.
Correct Answer
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.
Continue Practice
Share
Share This Question
Challenge a friend or share with your study group.
More from This Topic