Ruby: Exit Loop

By Xah Lee. Date: .

Exit Loop

# ruby

for xx in 1..9 do
  p xx
  if xx == 4 then break end
end

# prints 1 to 4

while-loop with break:

# ruby

ii = 1

while ii < 9 do
puts ii;
if ii == 5 then break end
ii += 1
end

# prints 1 to 5

Ruby, loop, iteration