Labeled loops in Java (outer/inner): determine the printed value of I.\n\nint I = 0;\nouter:\nwhile (true)\n{\n I++;\n inner:\n for (int j = 0; j < 10; j++)\n {\n I += j;\n if (j == 3)\n continue inner;\n break outer;\n }\n continue outer;\n}\nSystem.out.println(I);

Difficulty: Medium

Correct Answer: 1

Explanation:


Introduction / Context:
This problem checks mastery of labeled break/continue in Java and how they affect nested loops. The code includes an outer labeled while and an inner labeled for loop with both continue inner and break outer present.


Given Data / Assumptions:

  • I starts at 0 and is incremented before entering the inner loop.
  • The inner loop begins at j = 0.
  • I += j executes before the conditional control statements.
  • break outer immediately exits the outer while loop entirely.


Concept / Approach:
Evaluate the first pass carefully. Because the break outer comes before any iteration reaches j == 3, the continue inner never triggers. Exiting the outer loop fixes the final value of I.


Step-by-Step Solution:
Initialize: I = 0.Enter outer once: I++I = 1.Enter inner with j = 0: perform I += jI = 1.Check j == 3 ⇒ false.Execute break outer ⇒ exit all loops immediately.Print I: output is 1.


Verification / Alternative check:
Move the break outer after the if or set the check to j == 0 to see how the flow changes.


Why Other Options Are Wrong:
They assume further accumulation or additional iterations that never occur due to the early break outer.


Common Pitfalls:
Expecting the continue inner to run; it does not for j = 0.


Final Answer:
1

More Questions from Flow Control

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion