while and until
In this page:
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
count = 1
while count <= 3
puts "While count: #{count}"
count += 1
end
count = 1
until count > 3
puts "Until count: #{count}"
count += 1
end
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: