Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Loops and Iteration  ›  Capstone Project

break, next and redo

break 15 minnext skips the rest of the current pass, break leaves the loop at once and can return a value, and redo repeats the current pass without advancing, so it needs a limit.

break, next and redo

Bean Cart's barista works through the queue of orders. A cancelled order should be skipped. If the milk runs out, the barista stops for the day. Loops need the same controls: skip this one, stop completely, and, rarely, try this one again. Ruby's words for those are next, break and redo.

The rule

next skips to the next turn, break leaves the loop, redo repeats this turn.

next ends the current pass at once and moves on to the next item:

orders = ["latte", "cancelled", "tea", "mocha"]

orders.each do |drink|
  next if drink == "cancelled"
  puts "Make a #{drink}"
end
# Make a latte, Make a tea, Make a mocha

break leaves the whole loop immediately. Nothing after it in the loop runs, for this item or any later one:

milk = 2
orders.each do |drink|
  next if drink == "cancelled"
  break if milk.zero?
  puts "Make a #{drink}"
  milk -= 1
end
# Make a latte, Make a tea   (then milk is 0, so it stops before mocha)

break can also carry a value out of the loop. That value becomes the result of the whole loop expression:

first_big = [2, 6, 12, 3].each { |n| break n if n > 5 }
# first_big is 6

Without a break, each returns the original array, so it is worth knowing which one you get.

Both keywords work in while, until, loop, each, times and similar. They are usually written with a modifier (next if ..., break if ...) as a guard near the top of the loop body.

redo restarts the current pass without moving to the next item and without re-checking the loop condition. It is rarely needed and easy to misuse: unless something inside changes, redo repeats the same pass forever. If you do use it, pair it with a limit, like a retry counter.

The shortcut

For each item, ask three questions in order: Should I stop everything? break. Should I skip just this one? next. Otherwise, do the work. Putting the guards at the top of the body keeps the rest of it simple.

Tracing Bean Cart's queue with 2 milkStep 1 of 4
  1. "latte"

    Not cancelled, and milk is 2, not zero. Make it; milk drops to 1.

Drills

Answer fast — trust the rule. Drills two to four use these loops:

[1, 2, 3, 4].each { |n| break if n == 3; print n }        # loop A
[1, 2, 3, 4].each { |n| next if n.even?; print n }        # loop B
result = [3, 8, 10].each { |n| break n if n > 5 }         # loop C
quick drill

What does next do inside an each block?

quick drill

What does loop A print?

quick drill

What does loop B print?

quick drill

After loop C, what is result?

quick drill

Why is redo risky?

Remember

  • next skips to the next turn, break leaves the loop (and can return a value), redo repeats this turn.
  • Ask in order: stop everything? skip this one? otherwise, work. Put the guards at the top.
  • Trap: redo without a limit loops forever, and each returns the array unless you break with a value.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWorking the queue

Bean Cart's queue is latte, cancelled, tea, mocha, and there is milk for 2 drinks. Describe a loop that skips cancelled orders and stops when the milk runs out, say which drinks get made, and explain the difference between next and break.