Loops (while, until, loop)
A loop is a control structure that repeats a block of code, and Ruby gives you several flavours — while and until repeat based on a condition, while loop repeats forever until you break out.
Learn Loops (while, until, loop) in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll also meet the idiomatic numeric iterators times , upto , downto , and step , plus the flow keywords next , break , and redo .
What You'll Learn in This Lesson
1️⃣ while & until
A while loop repeats as long as its condition stays true; until is the inverse, repeating until the condition becomes true. Both can be written after a single statement as a compact postfix loop. Just remember to change the condition's variable inside the body, or you'll loop forever.
2️⃣ loop do ... end
loop runs its block endlessly, so you control the exit with break . It shines when the stop condition is decided in the middle of the body — like retry-until-success. It also auto-handles StopIteration , which makes it pair nicely with an enumerator's .next .
3️⃣ Numeric Iterators & Flow Control
When you want to repeat a fixed number of times, reach for times , upto , downto , or step rather than a manual counter — they're clearer and harder to get wrong. Inside any loop, next skips to the next iteration and break leaves entirely.
Your turn. Fill in each ___ blank with the right keyword or method, then run it.
Mini-Challenge: A Guess Checker
Walk a list of guesses with loop , using next to print a hint and continue, and break to stop on the correct answer. Run with ruby guess.rb .
Common Errors (and the fix)
- ❌ Infinite loop (condition never changes) → ✅ update the variable inside the body, e.g. count += 1 .
- ❌ Off-by-one with times → ✅ remember 3.times yields 0, 1, 2 , not 1, 2, 3 .
- ❌ Using return to exit a loop in a script → ✅ use break ; return exits the whole method.
- ❌ Expecting next to leave the loop → ✅ next only skips one iteration; use break to exit.
- ❌ Relying on a for loop variable leaking out → ✅ it does leak; use each for a clean block scope.
Pro Tips
- 💡 Prefer iterators: n.times and arr.each beat manual counters for clarity.
- 💡 break returns a value: x = loop { break 42 } sets x to 42 .
- 💡 step for intervals: 0.step(100, 10) walks in tens without arithmetic in the body.
📋 Quick Reference — Loops
Concept
Ruby Syntax
Condition loop
while x < 10 ... end
Inverse loop
until done ... end
Forever + break
loop { break if ... }
Repeat N times
3.times { |i| ... }
Count up / down
1.upto(3) , 3.downto(1)
Skip / exit
next , break
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ while / until loop on a condition; both have a postfix form
- ✅ loop do ... end runs forever until break
- ✅ times , upto , downto , step are the idiomatic counters
- ✅ next skips an iteration; break exits the loop
- ✅ Prefer each over for for clean scoping
- ✅ Next lesson: case/when conditionals
Practice quiz
A while loop keeps running as long as its condition is:
- false
- nil
- true
- zero
Answer: true. while repeats while the condition stays true.
until x == 0 is equivalent to which while loop?
- while x != 0
- while x == 0
- while x > 0
- while x
Answer: while x != 0. until runs while the condition is false, so until x == 0 is while x != 0.
How do you exit a loop do ... end block?
- return
- stop
- exit
- break
Answer: break. loop runs forever until you break out of it.
What numbers does 3.times yield to its block?
- 1, 2, 3
- 0, 1, 2
- 0, 1, 2, 3
- 3, 2, 1
Answer: 0, 1, 2. times is zero-based: 3.times yields 0, 1, 2.
What does 1.upto(3) iterate over?
- 1, 2, 3
- 0, 1, 2
- 1, 2
- 3, 2, 1
Answer: 1, 2, 3. upto counts inclusively upward: 1, 2, 3.
Which counts downward from 3 to 1?
- 3.times
- 1.upto(3)
- 3.downto(1)
- 3.step(1)
Answer: 3.downto(1). downto counts down inclusively: 3, 2, 1.
What does next do inside a loop?
- exits the loop
- skips the rest of the current iteration
- restarts the program
- returns from the method
Answer: skips the rest of the current iteration. next jumps to the next iteration, like continue.
Inside loop, why does pairing it with an enumerator's .next stop cleanly?
- loop ignores errors
- enumerators never end
- next returns false at the end
- loop quietly rescues StopIteration
Answer: loop quietly rescues StopIteration. loop rescues StopIteration, so it ends gracefully at the last element.
What is the value of x = loop { break 42 }?
- nil
- 42
- true
- 0
Answer: 42. break can return a value, which becomes the loop's result.
Why is for...in discouraged compared with each?
- it is slower
- it cannot iterate arrays
- its loop variable leaks out because for has no new scope
- it is deprecated syntax
Answer: its loop variable leaks out because for has no new scope. for does not create a new scope, so its variable leaks; each keeps it local.
Continue this course
- Previous: nil, true, false & Truthiness
- Next: case/when Conditionals