Ruby until Loop
Execute code until a condition becomes true
🔄 What is an until Loop?
The until loop is the opposite of while. It executes code repeatedly until a condition becomes true. It's more readable when you want to continue until something happens.
# Basic until loop
count = 1
until count > 3
puts "Count: #{count}"
count += 1
end
Output:
Count: 1 Count: 2 Count: 3
until vs while
until Syntax
Runs until condition is true
until condition
# code
end
while Syntax
Runs while condition is true
while condition
# code
end
Readability
Use until for negative conditions
until done
work
end
Equivalence
until is opposite of while
# Same result
until x >= 5
while x < 5
🔹 Basic until Loop
The until loop continues executing until the condition becomes true. It's perfect when you want to express "keep doing this until something happens."
# Count up until reaching 5
number = 1
until number > 5
puts "Number: #{number}"
number += 1
end
puts "Reached 5!"
Output:
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 Reached 5!
🔹 until vs while Comparison
Both loops can achieve the same result, but until is more readable when expressing negative conditions. Here's a side-by-side comparison:
# Using until (more readable for this case)
attempts = 0
until attempts == 3
puts "Attempt #{attempts + 1}"
attempts += 1
end
# Using while (requires negative logic)
attempts = 0
while attempts != 3
puts "Attempt #{attempts + 1}"
attempts += 1
end
# Both produce the same output!
Output (both loops):
Attempt 1 Attempt 2 Attempt 3
🔹 Practical until Loop Examples
Here are real-world scenarios where until loops make your code more readable and intuitive:
# Wait until target is reached
score = 0
until score >= 10
score += 3
puts "Score: #{score}"
end
puts "Target reached!"
# Build a string until length is sufficient
text = ""
until text.length >= 10
text += "Hi"
end
puts "Text: #{text}"
puts "Length: #{text.length}"
Output:
Score: 3 Score: 6 Score: 9 Score: 12 Target reached! Text: HiHiHiHiHi Length: 10
🔹 One-line until Loop
Ruby allows you to write until loops in a single line for simple operations. This is called a modifier form and is very concise.
# One-line until loop
x = 0
puts x += 1 until x == 3
# Traditional multi-line equivalent
y = 0
until y == 3
puts y += 1
end
Output:
1 2 3 1 2 3
🔹 until Loop with Multiple Conditions
You can combine multiple conditions using logical operators (&&, ||) to create more complex loop controls:
# Loop until either condition is met
count = 0
sum = 0
until count >= 5 || sum >= 10
count += 1
sum += count
puts "Count: #{count}, Sum: #{sum}"
end
puts "Loop ended: Count=#{count}, Sum=#{sum}"
Output:
Count: 1, Sum: 1 Count: 2, Sum: 3 Count: 3, Sum: 6 Count: 4, Sum: 10 Loop ended: Count=4, Sum=10
🔹 When to Use until
Choose until over while when it makes your code more readable:
Use until when:
- Waiting for completion: "Keep trying until successful"
- Reaching a goal: "Continue until target is reached"
- Negative conditions: "Run until NOT something"
- Natural language: When "until" sounds more natural than "while not"
Use while when:
- Positive conditions: "While something is true"
- Active states: "While running" or "While available"
- Conventional patterns: Standard counting loops
# Good use of until (reads naturally)
until game_over
play_turn
end
# Good use of while (reads naturally)
while player_alive
continue_game
end