0
0
Rubyprogramming~10 mins

Until loop in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Until loop
Initialize variable
Check condition
EXIT
Update variable
Back to Check
The until loop runs the code inside it until the condition becomes true. It checks the condition first, and if false, runs the loop body.
Execution Sample
Ruby
i = 0
until i > 3
  puts i
  i += 1
end
Prints numbers from 0 to 3 using an until loop that stops when i becomes greater than 3.
Execution Table
StepiCondition (i > 3)Condition ResultActionOutput
100 > 3FalsePrint i=0, i = i + 10
211 > 3FalsePrint i=1, i = i + 11
322 > 3FalsePrint i=2, i = i + 12
433 > 3FalsePrint i=3, i = i + 13
544 > 3TrueExit loop
💡 At step 5, condition i > 3 is True, so the loop stops.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
i012344
Key Moments - 2 Insights
Why does the loop stop when i is 4, not when i is 3?
Because the condition is 'i > 3'. When i is 3, 3 > 3 is false, so the loop runs. When i becomes 4, 4 > 3 is true, so the loop stops (see execution_table step 5).
Does the loop run if the condition is true at the start?
No, the until loop checks the condition before running. If the condition is true initially, the loop body does not run at all.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i at step 3?
A1
B2
C3
D4
💡 Hint
Check the 'i' column in execution_table row for step 3.
At which step does the condition become true and the loop stops?
AStep 5
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'Condition Result' column in execution_table to find when it becomes True.
If the initial value of i was 5, what would happen?
AThe loop runs once
BThe loop runs infinitely
CThe loop does not run at all
DThe loop runs twice
💡 Hint
Recall that until loop runs only if condition is false initially (see key_moments about initial condition).
Concept Snapshot
Ruby until loop syntax:
until condition
  # code runs while condition is false
end

It checks condition first.
Runs loop body only if condition is false.
Stops when condition becomes true.
Full Transcript
This visual execution shows how the Ruby until loop works. We start with i = 0. The loop checks if i > 3. Since 0 > 3 is false, it runs the loop body: prints i and adds 1 to i. This repeats until i becomes 4, where 4 > 3 is true, so the loop stops. The variable i changes from 0 to 4 step by step. The key point is that the until loop runs while the condition is false and stops when it becomes true. If the condition is true at the start, the loop does not run at all.