Bird
0
0

Identify the error in this Ruby code using an until loop:

medium📝 Debug Q14 of 15
Ruby - Loops and Iteration
Identify the error in this Ruby code using an until loop:
i = 0
until i < 5
  puts i
  i += 1
end
AInfinite loop because i never changes
BMissing do keyword after until condition
CThe loop condition should be > instead of <
DNo error, code runs correctly
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop condition

    until i < 5 with i=0: 0 < 5 is true, so condition true and loop never executes.
  2. Step 2: Identify the correct error

    Syntax is fine (newline ok without do). Logic error: condition backwards. Should be until i > 4 or similar. The loop condition should be > instead of < identifies this.
  3. Final Answer:

    The loop condition should be > instead of < -> Option C
  4. Quick Check:

    Wrong condition direction: loop never runs [OK]
Quick Trick: Flip < to > for until when adapting while logic [OK]
Common Mistakes:
MISTAKES
  • Confusing condition direction (< vs >)
  • Assuming no do needed for until
  • Thinking i doesn't change causing infinite loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes