Bird
0
0

Which of these is a valid use of an until loop in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Loops and Iteration
Which of these is a valid use of an until loop in Ruby?
Auntil x != 10 do puts x x += 1 end
Buntil x < 10 do puts x x += 1 end
Cuntil x > 10 do puts x x -= 1 end
Duntil x == 10 do puts x x += 1 end
Step-by-Step Solution
Solution:
  1. Step 1: Check condition logic for each option

    until x == 10 do puts x x += 1 end runs until x equals 10, incrementing x each time, which is valid.
  2. Step 2: Identify invalid conditions

    until x < 10 do puts x x += 1 end runs until x is less than 10, but if x starts less than 10, loop never runs. until x > 10 do puts x x -= 1 end decrements x but condition is x > 10, which may cause infinite loop. until x != 10 do puts x x += 1 end increments x until x is not 10, which may cause infinite loop if x starts at 10.
  3. Final Answer:

    until x == 10 do puts x x += 1 end -> Option D
  4. Quick Check:

    Valid until loop condition = A [OK]
Quick Trick: Until loops run until condition becomes true [OK]
Common Mistakes:
  • Using conditions that never become true
  • Incrementing/decrementing in wrong direction
  • Confusing condition logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes