Bird
0
0

Identify the problem in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Loops and Iteration
Identify the problem in this Ruby code snippet:
for i in 1..3
  if i == 2
    break
  else
    next
  end
  puts i
end
AThe <code>break</code> keyword is used incorrectly inside a for loop.
BThe loop will never terminate because of <code>next</code>.
CThe <code>puts i</code> is unreachable due to <code>next</code> and <code>break</code> placement.
DThere is no problem; the code runs as expected.
Step-by-Step Solution
Solution:
  1. Step 1: Understand flow control

    next skips to next iteration, break exits loop.
  2. Step 2: Analyze code flow

    For i == 2, loop breaks; for others, next skips puts i.
  3. Step 3: Result

    puts i is never executed because next skips it and break exits before it.
  4. Final Answer:

    puts i is unreachable. -> Option C
  5. Quick Check:

    Next and break prevent puts from running [OK]
Quick Trick: Next skips puts, break exits early, so puts never runs [OK]
Common Mistakes:
  • Assuming puts runs before next
  • Thinking break is invalid in for loops

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes