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

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:

  1. break, next, and redo
Syntax
ruby
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

markup
(1..10).each do |i|
  next if i.even?
  break if i > 7
  puts i
end
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
  1. Expecting break to leave all nested loops, when it exits only the innermost.
  2. Placing next if after code that should be skipped, so those lines still run.
  3. Using redo without changing something, which repeats the same iteration forever.
Chapter Summary
  • if, elsif, else, unless, and case/when choose between paths.
  • while, until, for, each, and loop do repeat code.
  • break, next, and redo control loop flow.
🔒

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.