×

ruby · level 1 · unit 5 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

stops a loop early. skips a turn.

Picture a lift going up a tall building. You ride past each floor, and when you reach yours, you get out. You do not ride to the top. break is getting out: the loop stops straight away.

Some buildings have no floor 13, so the lift skips it and carries on. next is that skip: Ruby drops the rest of this turn and jumps to the next one.

Both usually come with a short if at the end of the line, so they only happen when its condition is true. Any lines after them in the same turn do not run.

10.upto(15) do |floor|
  next if floor == 13
  break if floor == 15
  puts "Ding! Floor #{floor}"
end

Floor 13 is skipped by next, and break ends the ride at floor 15.