Updated April 3, 2023
Introduction to Ruby next 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 skip the execution for certain element instead of completely halting the loop, so in such type of situation we have a very useful attribute available in Ruby , which is next. next provide a way to skip the execution of the loop and start from other element at any given time , for example if you are searching for any data and you do not want to perform code block execution for some element then you can use next to skip the execution, in the very simple term we can say it allow us to go to any specific execution element .
Syntax:
Below is the syntax for the next statement in Ruby, we can explain the below syntax for the Ruby next in the following steps.
- In the example first things we are going to run a while loop, we are using a while loop and expression, here expression is the condition, and on successful expression(true) output code block will be executed .
- Next we have written an if condition where we are checking if the condition if failed or success, in case if the condition is true and the next statement will be called.
- Here next statement will skip the execution and go to again while loop condition check and if again if condition is true next will call and command will go to while.
- Once the, if condition fails or simply condition, will be false, it will go to code block for the execution.
#We can consider any loop, i have taken an example of the while loop.
while expression
if(condition)
next
end
#here we will write the code which will execute
end
How next Statements Works in Ruby?
With the help of the next 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 “next” in Ruby with the help of the flowchart below.
- First it will start the while loop condition checking.
- If condition is success or true then it will go for if statement checking, else the loop will be halted or exit from the loop.
- In case if the if condition is true the next statement will be called, and again the next statement will call the “while” statement block to check for the condition with the new attribute. Once the if condition is false it will go to the code block and execute it.
- We have taken examples of the while loop, but can do the same thing with other loops.
Please see the flowchart for the next statement in the Ruby.
Examples to Implement next in Ruby
Below are the examples of next statement in Ruby:
Example #1
Below is the example where we are printing numbers. Here we are printing the numbers from 4 to the 10. 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 or less then 0 the loop will be halted.
- But here our goal is to print only the number from 4 to 10, so next 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 4, if the number is less than 4 it will call to the next statement and loop will again run for the next number.
Code:
#Here in this example we are printing number start from 4 to 10 out of number from 0 to 10
for number in 0..10
if number < 4 then
next
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 “ranjan” we are going to print the remaining users, which means keep skipping the loop till the user “ranjan” arrived in the loop. 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 and there we have used a condition to check if the variable i is less than 4 , which means i should be less than the length of the array of the students.
- Inside the if block we are checking for the user “ranjan” , till it will not get the user “ranjan” it will not print anything.
- It means we are able to jump to a particular condition without halting the whole loop.
Code:
#Here we printed all the students after ranjan.
student = ['ajay','ranjan','vijay','sujit','akash']
i=0
while i<4 do
i += 1
next if student[i] == 'ranjan'
puts "The student name is #{student[i]}"
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 will not print anything till the number is less than 12. Once the sum of the numbers will reach to the 12 it will start printing till the 30.
Code:
number =0
while number<30
number += 3
if number + 3 <= 12
next
end
puts "The sum of the two number is #{number}"
end
Output:
Conclusion
From these tutorials we learned some important uses and basic concepts of the next statement in Ruby with the help of some useful examples, we also learned the working of next with flowchart, we also focused on the uses of next in real-world cases.
Recommended Article
This is a guide to next in Ruby. Here we discuss a few important uses and basic concepts of next statement in Ruby with the help of some useful examples and its flowchart. You can also go through our other suggested articles to learn more –