break, next, and redo
In this page:
break, next, and redo
break exits the nearest enclosing loop entirely, next skips to the next iteration (like continue in other languages), and redo re-runs the current iteration from the start without re-checking the loop's condition. redo is rarely used but can be handy for retry-style logic within a single iteration. All three work inside while, until, for, and block-based iteration like each.
Note: redo is Ruby's least common loop control keyword -- reach for it only when you specifically need to repeat the current iteration, not move to the next one.
Example: break, next, and redo
(1..10).each do |i|
next if i.even?
break if i > 7
puts i
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: