break, next, and redo
break leaves a loop completely, next skips to the following turn, and redo repeats the current turn. It is like leaving a game, skipping your turn, or trying your turn again.
In this page:
Syntax
loop do
next if skip_condition
break if stop_condition
# body
end
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Expecting
breakto leave all nested loops, when it exits only the innermost. - Placing
next ifafter code that should be skipped, so those lines still run. - Using
redowithout changing something, which repeats the same iteration forever.
Chapter Summary
if,elsif,else,unless, andcase/whenchoose between paths.while,until,for,each, andloop dorepeat code.break,next, andredocontrol loop flow.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: