An until loop repeats the code inside it as long as the condition is false. It stops when the condition becomes true.
Click to reveal answer
beginner
How is an until loop different from a while loop?
An until loop runs while the condition is false, but a while loop runs while the condition is true. They are opposites.
Click to reveal answer
beginner
Write a simple Ruby until loop that prints numbers from 1 to 5.
number = 1
until number > 5
puts number
number += 1
end
Click to reveal answer
beginner
Can an until loop run zero times? When?
Yes, if the condition is already true at the start, the until loop will not run at all.
Click to reveal answer
beginner
What keyword ends an until loop block in Ruby?
The end keyword is used to close the until loop block.
Click to reveal answer
What condition does an until loop check to continue running?
ACondition is zero
BCondition is true
CCondition is nil
DCondition is false
✗ Incorrect
An until loop runs while the condition is false.
Which keyword ends an until loop in Ruby?
Aend
Bstop
Cfinish
Dclose
✗ Incorrect
Ruby uses end to close blocks including until loops.
What will this code print?
count = 3
until count > 5
puts count
count += 1
end
A3 4
B4 5
C3 4 5
DNothing
✗ Incorrect
It prints 3, 4, and 5 because the loop stops when count becomes 6 (count > 5).
If the condition is true at the start, how many times does an until loop run?
AOnce
BZero times
CInfinite times
DTwice
✗ Incorrect
The until loop runs zero times if the condition is true initially.
Which loop is the opposite of until in Ruby?
A<code>while</code>
B<code>loop</code>
C<code>for</code>
D<code>each</code>
✗ Incorrect
while loops run while the condition is true, opposite to until.
Explain how an until loop works in Ruby and give a simple example.
Think about repeating something until a condition becomes true.
You got /3 concepts.
Compare until and while loops in Ruby. When would you use each?
They are opposites but both repeat code based on conditions.
You got /3 concepts.
Practice
(1/5)
1. What does an until loop do in Ruby?
easy
A. Repeats code while the condition is false
B. Repeats code while the condition is true
C. Runs code only once
D. Stops code immediately
Solution
Step 1: Understand the until loop condition
An until loop runs as long as its condition is false.
Step 2: Compare with while loop
Unlike while which runs when true, until runs when false.
Final Answer:
Repeats code while the condition is false -> Option A
Quick Check:
Until loop runs while condition is false [OK]
Hint: Until loops run while condition is false, opposite of while [OK]
Common Mistakes:
Thinking until runs while condition is true
Confusing until with while loop
Assuming until runs only once
2. Which of the following is the correct syntax for an until loop in Ruby?
easy
A. until condition then
# code
end
B. until (condition) {
# code
}
C. until condition:
# code
D. until condition do
# code
end
Solution
Step 1: Recall Ruby loop syntax
Ruby uses until condition do ... end or just until condition ... end.
Step 2: Check each option
until condition do
# code
end matches Ruby syntax; others use wrong symbols or keywords.
Final Answer:
until condition do\n # code\nend -> Option D
Quick Check:
Ruby until loop uses do/end syntax [OK]
Hint: Use do/end blocks for until loops in Ruby [OK]
Common Mistakes:
Using curly braces {} instead of do/end
Using colon : like Python
Adding 'then' keyword incorrectly
3. What will be the output of this Ruby code?
count = 1
until count > 3 do
puts count
count += 1
end
medium
A. 1 2 3 4
B. 1 2 3
C. 2 3 4
D. No output
Solution
Step 1: Analyze the loop condition and variable
The loop runs until count > 3. Starting from 1, it prints and increments count.
Step 2: Trace each iteration
Prints 1, increments to 2; prints 2, increments to 3; prints 3, increments to 4; now count > 3, loop stops.
Final Answer:
1 2 3 -> Option B
Quick Check:
Prints 1 to 3 before stopping [OK]
Hint: Until runs while condition false, stops when count > 3 [OK]
Common Mistakes:
Including 4 in output by mistake
Thinking loop runs while condition true
Forgetting to increment count
4. Identify the error in this Ruby code using an until loop:
i = 0
until i < 5
puts i
i += 1
end
medium
A. Infinite loop because i never changes
B. Missing do keyword after until condition
C. The loop condition should be > instead of <
D. No error, code runs correctly
Solution
Step 1: Analyze the loop condition
until i < 5 with i=0: 0 < 5 is true, so condition true and loop never executes.
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.
Final Answer:
The loop condition should be > instead of < -> Option C
Quick Check:
Wrong condition direction: loop never runs [OK]
Hint: Flip < to > for until when adapting while logic [OK]
Common Mistakes:
Confusing condition direction (< vs >)
Assuming no do needed for until
Thinking i doesn't change causing infinite loop
5. You want to write an until loop that prints numbers from 10 down to 1. Which code correctly does this?
hard
A. num = 10
until num < 1 do
puts num
num -= 1
end
B. num = 10
until num == 1 do
puts num
num -= 1
end
C. num = 10
until num > 0 do
puts num
num -= 1
end
D. num = 10
until num < 0 do
puts num
num -= 1
end
Solution
Step 1: Understand the goal and loop condition
We want to print from 10 down to 1, so loop should run while num is >= 1.
Step 2: Check each option's condition
num = 10
until num < 1 do
puts num
num -= 1
end uses until num < 1, meaning loop runs while num >= 1, printing 10 to 1 correctly.
Final Answer:
num = 10\nuntil num < 1 do\n puts num\n num -= 1\nend -> Option A
Quick Check:
Until runs while condition false, so until num < 1 runs until num is 0 [OK]
Hint: Use until num < 1 to count down from 10 to 1 [OK]
Common Mistakes:
Using until num < 0 (prints 0 too)
Using until num > 0 (never runs)
Stopping at num == 1 (doesn't print 1)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.