Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Loops and Iteration
What will be the output of the following Ruby code?
count = 0
loop do
  puts count
  count += 1
  break if count == 3
end
A0 1 2 3
B0 1 2
C1 2 3
DInfinite loop printing numbers
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    Initially, count = 0. The loop prints count, then increments it by 1.
  2. Step 2: Check break condition

    Loop breaks when count == 3, so it stops after printing 0, 1, and 2.
  3. Final Answer:

    0 1 2 -> Option B
  4. Quick Check:

    Loop stops before printing 3 due to break condition [OK]
Quick Trick: Break stops loop before count reaches 3 [OK]
Common Mistakes:
  • Including 3 in output
  • Forgetting to increment count
  • Assuming infinite loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes