Updated April 1, 2023
Introduction to Ruby break statement
We have used loops in the Ruby and we know that loops are very useful in Ruby as they provide a convenient way to execute same piece of code for the many times, but what will happen if any instance we wanted to stop the execution and come out the loop, so in such type of situation we have a very useful attribute available in Ruby, which is break, break provide a way to stop execution of the loop at any given time, for example if you are searching for any data and once you got the data it is useless to search further so with the help of the break we can exit the loop once we get our required output.
Syntax:
Below is the syntax for the break statement in Ruby, we can explain the below syntax for the Ruby break in the following steps.
- In the example first things we are going, we are using a while loop and expression, here expression is the condition, and on successful expression(truee) output code block will be executed.
- Next we have a comment section where we have mentioned about our logic to search or perform any operation.
- Once we get our required output from the code , executing code blocks again and again is useless, so we have the code break which will exit the loop.
- From the syntax we can understand that at any time if we find that we have got our output we can skip the execution for the next instructions.
#We can consider any loop , i have taken an example of the while loop.
while expression
#write your code for searching your attribute from the each value
# Use the break once you get the value for which you're doing this search.
break
end
end
How Break Statement Works in Ruby?
With the help of the break statement we will be able to save unnecessary code execution and we will be able to save our costly resources and processes. Let us understand the working of the break in the Ruby with the help of the flowchart below.
- First it will execute the code block as it is a do while loop.
- Next time comes for the execution of the condition block , each time it will check for the conditions, if the condition gets success in that case again it will execute the code block.
- Here there is one way of halting the execution is the failure of the condition block, another way for halting the execution is use of a break statement.
- In the below diagram we can see if the condition block fails or break comes in the play then only the loop gets halted.
- We have taken examples of the do while loop, but can do same thing with other loops.
- Few important points about the break, because the break will only work with while and for loop we can face some challenges while using it.
Please see the flowchart for the break statement in the Ruby.
Examples of Break Statement in Ruby
Below are the examples of Break in Ruby:
Example #1
Below is the example where we are printing numbers. Here we are printing till the 5. We can explain the below code in the following steps.
- Here we are running a while loop which is checking for the numbers, the while conditional statement will only check if the number is from 0 to 10.
- If the number will be more than the 10 the loop will be halted.
- But here our goal is to print only till 5 , so break will play an important role here.
- So here we have used an if condition which is checking if the number which is going to print is greater than 5, if the number is greater than 5 it will break the statement and the loop will be halted.
Please follow the below example:
Code:
#We are writing code where we are going to halt the execution of the code till 5
for number in 0..10
if number > 5 then
break
end
puts "The number is #{number}"
end
Output:
Example #2
Here in the below example we have taken some array of students and running the loop on them, the goal of our program once we found the user “sujit” we are going to run the command break which will halt the complete execution of the program. We can also explain the below code in the following steps.
- First we have taken an array of the student variables .
- We are running a loop which can go infinite, because we have not used any specific condition, we have simply used true which will never change and execution will continue.
- Finally inside the code block we have written a check with if condition, it will check if the student name is “sujit” , if it matches the break statement will be executed and code will stop execution, which means the loop will be halted and remaining student print will not happen.
Please follow the below example:
Code:
student = ['ajay','ranjan','vijay','sujit','akash']
i=0
while true do
puts student[i]
i += 1
break if student[i] == 'sujit'
end
Output:
Example #3
Here the goal of this code is to keep adding 3 into a number which will start from 0, and we want to print till 9 only, which sum should be less than 12. Please follow the below example:
Code:
number = 0
while true
number += 3
puts "The sum of the two number is #{number}"
if number + 3 >= 12
break
end
end
Output:
Conclusion
From these tutorials we learned some important uses and basic concepts of break statement in Ruby with the help of some useful examples, we also learned the working of break with flowchart, we also focused on the uses of break in real world cases.
Recommended Article
We hope that this EDUCBA information on “Break in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.