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.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 += j
⇒ I = 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
Discussion & Comments