Updated April 10, 2023
Introduction to For loop in Ruby
For Loop in Ruby allow developers to reuse the same piece of code for the various time, or in more clear way loop in ruby is a way to run the same peace of code for as many time as needed for the situation, in Ruby it supports a predefined way to achieve the goal of loops, for loop is the most common for the Ruby programming languages, in any for loop it consists of in operator which will help to iterate over given data in the expression section from start to end of the array([“a”,”b”,”c”]) or numbers(0…10).
Syntax
Below is the syntax for loops in Ruby,
for name_of_variable[, variable1...] in expr [do]
# here will do the code for executions
end
The explanation for the below syntax is given below,
- for: this is the ruby predefined keyword which will be used for the execution of the same piece of code, again and again, it indicates the start of any loop.
- name_of_variable: For the iteration of the loop these attributes are important. They pay the role for the reference of the iteration of the current loop.
- in: This keyword is made specially in the ruby for the “for” loops which play a vital role in breaking the array of the data.
- expr: Expr is the expression that holds the array of the data and code will go through each attribute of the expr.
- do: This is the optional attribute that will be used to specify the starting of the code which will repeatedly go to executes.
- end: This keyword represents the ending of the ‘for‘ loop block which started from ‘do‘ keyword.
Flowchart of For loop in Ruby
Below is the flow chart for the loop in the Ruby, we can explain the below loop in the following steps,
- The first execution will start with passing the value for the loop in the form of expression(0..n).
- In the loop, there is a condition block which will check for the true or false condition. Basically this condition about checking if any data inside the array is available which has not been traversed.
- In case of a condition success(still, data is there) code block will execute and if the condition is false the loop will break. Here fail will happen if the array gets empty after looping on the array or reached to the last number.
- It will continue until the condition is true, which means the array of data will empty.
- The expression can be an array of data or numbers. In the case of numbers, it will iterate till the last number and in case of an array of data, it will iterate for all array data.
How for loop works in Ruby?
Working of the for loops in ruby can be explained in the below steps,
- For Loops in ruby are based on the array of data or some hash value.
- Each time loops checks for the condition and available data inside the array or hash value.
- If the condition gets failed(condition==false) then the loop will be broken. Here the condition is for a non-empty array.
- If the condition is true it will enter into the loop and execute the code block and it will continue doing this till the condition is true.
- Execution of a loop also depends on the data available inside the array or hash value(especially for each loop).
- Loop will run for the numeric value of the length of the array of data.
Examples of for loop in Ruby
Here are the following examples mention below
Example #1
Below is the first example for the Ruby for loop and we can explain the below code in the following steps.
- First, we created a variable “name” and assigned the value “ranjan” to this variable.
- In the next step, we started the loop which will run the code till 8. As we have written 0..8 in place of expression.
- At the start of code, it will start printing greetings for the ranja0, to ranjan8 users.
- Once 8the number is reached it will execute code for last and break the loops.
Please follow the below code along with the screen of output.
Code:
name ="ranjan"
for b in 0..8 do
puts "Hello mr #{name}#{b} How are you"
end
Output:
Example #2
This is the second example of the for loop and this example we can explain in the following ways,
- The first “for” loop will begin and it will check for the number from 0 to 10.
- For each number, it will compare it with the condition written inside the if blocks
- Inside the if block we will only execute if the value of “b” is greater than 5.
- If the value of “b” is less than 5, the loop will go to next which means the number value of “b” will change and a new number will come. For example, if the value was 4 it will change to 5 and again next time 6.
- Once the value of “b” is 5 it will start going inside the if block and start printing the output for us.
- Once the value of “b” reaches the 10 which is the last value of the expression section, it will print and halt the for loop here.
Please follow the below code along with the screen of output.
Code:
for b in 0..10
if b < 5 then
next
end
puts "value is still greater than 5 and it is #{b}"
end
Output:
This is the third example for the “for” loop we can explain this example in the below steps,
- First, we defined variable students and assigned some array of student’s data to it.
- In the next step, the for loop will begin and start printing each student’s names.
- Here “b” is the string containing the names of each student inside the student’s variables.
- In this loop, each time the operator on for loop will run and check for the data.
- Once the student’s array gets empty the loop will halt.
Please follow the below code along with the screen of output.
Code:
students = ["Ranjan", "Ajay", "Vijay", "Sujoy"]
for b in students do
puts "Student name is #{b}"
end
Output:
Conclusion
From this tutorial we learned uses and working principles of for loop in Ruby, we learned about the flow and some real case uses of the for loops in Ruby, We learned how we can handle arrays of data as well as numbers from start to end. We have also seen the flow chart for the for loop in Ruby for “for loop”.
Recommended Articles
This is a guide to for loop in Ruby. Here we discuss the Examples of for loop in Ruby and How does it work in ruby along with the Flowchart. You may also have a look at the following articles to learn more –