In programming, the break and next statements are used to alter the flow of loops:
breakexits the loop entirely.nextskips the current iteration and moves to the next one.
You typically use break when a specific condition makes continuing the loop unnecessary.
The next statement is useful when certain values should be skipped. For example, skipping invalid input, odd numbers, or specific characters in a string.
Ruby break Statement
The break statement terminates the loop immediately when it's encountered.
Syntax
break
Working of Ruby break Statement
The above image shows the working of break statements in for and while loops.
Note: The break statement is usually used inside decision-making statements such as if...else.
Example 1: Ruby break Statement with for Loop
You can use the break statement with the for loop to terminate the loop when a certain condition is met. For example,
for i in 1..5
if i == 3
break
end
puts i
end
Output
1 2
In the above example, the following code terminates the loop when i is equal to 3:
if i == 3
break
Hence, the output doesn't include values after 2.
Example 2: Ruby break Statement with while Loop
You can terminate a while loop using the break statement. For example,
i = 0
while i < 10
if i == 3
break
end
puts i
i += 1
end
Output
0 1 2
Here, break terminates the loop when i is equal to 3.
Ruby next Statement
The next statement skips the current iteration of the loop, and the control flow of the program goes to the next iteration.
Syntax
next
Working of next Statement in Ruby
Example 3: Ruby next Statement with for Loop
Let's see how you can use the next statement with a for loop.
for i in 1..5
if i == 3
next
end
puts i
end
Output
1 2 4 5
In the above example, the following code skips the current iteration when i is equal to 3, and continues with the next iteration:
if i == 3
next
Hence, the output includes all values except 3.
Example 4: Ruby next Statement with while Loop
You can also use the next statement with a while loop. For example,
i = 0
while i < 5
i += 1
if i == 3
next
end
puts i
end
Output
1 2 4 5
Here, next skips the iteration when i equals 3, so it is not printed.
The value of i is increased before the condition, so when i becomes 3, next prevents puts i from running in that loop cycle.
More on Ruby Break and Next
You can use break or next inside nested loops to control the flow of the inner loop without affecting the outer loop. For example,
# Nested for loops
# Outer loop
for i in 1..2
# Inner loop
for j in 1..3
break if j == 2
puts "i: #{i}, j: #{j}"
end
end
Output
i: 1, j: 1 i: 2, j: 1
Here, the inner loop breaks when j == 2, but the outer loop continues.
You can use break and next inside a case block only if the case is part of a loop. This is because they control the flow of loops, not case blocks directly.
numbers = [1, 2, 3, 4, 5]
for num in numbers
case num
when 3
# Skip this iteration when num is 3
next
when 5
# Exit the loop completely when num is 5
break
else
puts "Printing: #{num}"
end
end
Output
Printing: 1 Printing: 2 Printing: 4
In this example, the loop skips printing when num is 3, and exits completely when num is 5.