← Back to Ruby Course | Chapter 6: Control Flow | Lesson 4 of 7

while and until

In this page:

  1. while and until

while and until

while condition repeats its block as long as the condition is truthy, checked before each iteration. until condition is the inverse, repeating as long as the condition is falsy. Both also support a trailing modifier form for a single statement, like i += 1 while i < 10.

Note: until often reads more naturally than while !condition for conditions phrased as a stopping point.

Example: while and until

markup
count = 1

while count <= 3
  puts "While count: #{count}"
  count += 1
end

count = 1
until count > 3
  puts "Until count: #{count}"
  count += 1
end
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.