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.

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 .

📋 Quick Reference — Loops

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.