Ruby break Statement Tutorial

  • Post author:
  • Post category:Ruby

In this section, we will learn what the break statement is and how to use it in Ruby.

Note: we’re assuming you’re already familiar with one of the loops in Ruby (like for or while loop).

The break Statement in Ruby

The break statement is used to break a loop and terminate its work.

After calling the break statement, the loop is skipped and your program will continue to run the instructions after the loop.

Ruby break Statement Syntax:

break

The break statement doesn’t take any argument! Simply put, the word `break` in a loop and that loop will break the moment the statement is called.

Note that we usually use an if statement within the body of a loop in order to control when a call to the break statement should happen.

Example: for loop break statement

list  = ["Jack","Omid","Ellen","Elon"]

for element in list do 
    if element == "Ellen"
        break
    end 
    puts element
end 

puts "This is the instruction after the for loop"

Output:

Jack

Omid

This is the instruction after the for loop

In this example, the for loop is used to iterate through the elements of the list. Now within the body of the loop we have an if statement which it checks each element of the list against the value “Ellen”. If the value was equal to “Ellen” then a call to the `break` statement will occur and so the loop will terminate immediately.

That’s why we didn’t get the other two elements of the list being sent to the output stream.

Example: while loop break statement

num = 10 

while num <100
    num += 1
    if num == 50
        break
    end 
end 
puts "The final value of the num variable is: #{num}"

Output:

The final value of the num variable is: 50

Ruby break Statement in Nested Loop

As mentioned in the for loop section, we can use any necessary instruction within the body of a loop! That includes using another loop (AKA nested loop) within the body of the enclosing loop.

Now, if we used a nested loop and called the `break` statement within the body of that loop, be aware that only the nested loop will terminate and not the parent loop! Basically, the `break` statement is only capable of terminating its closing loop.

Example: nested loop and break statement

for i in 0..2 do 
    puts "The value of the i variable is: #{i}"
    for b in 10...15 do 
        puts "The value of the b variable is: #{b}"
        if b == 12
            break
        end 
    end 
end 

Output:

The value of the i variable is: 0

The value of the b variable is: 10

The value of the b variable is: 11

The value of the b variable is: 12

The value of the i variable is: 1

The value of the b variable is: 10

The value of the b variable is: 11

The value of the b variable is: 12

The value of the i variable is: 2

The value of the b variable is: 10

The value of the b variable is: 11

The value of the b variable is: 12

Note that within the body of the inner for loop, we’ve defined a condition to trigger the break statement every time the value of the `b` variable reached to the number 12.

As you can see, the call to the `break` statement only affected the inner for loop and not the parent for loop in this example.

Difference between break and next (break vs next in Ruby)

Other than the break statement, there’s another statement called `next`. The main difference between these two statements is that:

The break statement ends a loop completely (it will cause your program to jump out from the target loop and run the instructions after the loop) while the `next` statement only ends the current iteration of the loop and starts the next iteration! This means the program is still in the loop and just skipped the current iteration not the complete loop!

More to Read:

Ruby next Statement

Leave a Reply