Updated March 28, 2023
Definition of While Loop in Ruby
While Loop in Ruby allows developers to runs the same piece of code for the various time, or in a more clear way while 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, while loop is the one of the most used for looping in Ruby, in any while loop it consists of a condition statement(the condition can be a combination of multiple conditions) and if the condition is true loop will execute the code block else loop will breaks.
Syntax:
Below are the syntax while loops in the Ruby,
while cond1 cond 2 .. [do]
//(code block)here we are going to write code
end
Explanation:
An explanation for the above syntax is given below,
- while: 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.
- Cond1,cond2..: These are the conditions that will be checked before the execution of the code block. Example (a>9 || a<100) here may be any integer value and two conditions are the cond1 and cond2, if any of the conditions is true then the code block will execute.
- do: With the help of doing block it will be able to execute the code at least once even if the conditions get failed or simply conditions are false.
- end: This keyword represents the ending of the ‘while’ loop block which started from ‘do‘ keyword.
Please follow the given below code syntax for the while loop.
Flowchart
Below is the flow chart for the loop in the Ruby, we can explain the below flow chart in the following steps,
- The first execution will start with passing the value for the loop in the form of cond1 and cond2, which means the required data for cond1 and cond2.
- In the loop, there is a condition block that will check for the true or false condition. Basically this condition about checking if the conditions written inside the while loop is true or false.
- In case of a condition(cond1 and cond2), the success code block will execute and if the condition is false the loop will break.
- It will continue until the condition is true, which combined conditions for the cond2 and cond2 or maybe more should be true.
- If the condition is false, the loop will be halted and the end happens to the while loop.
How While Loop Works in Ruby?
Functions of the while loops in ruby can be explained in the below steps.
- While Loops in ruby are based on the boolean value which means it works on the true and false value of the conditions.
- Each time loop checks for the condition and if the condition written for the while loop is true it will execute the code block and if the condition is false the while loop will break and the end happens.
- In more technical words, If the condition gets failed(condition==fale) then the loop will be broken. Here the condition can be a combination of many more conditions.
- 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.
- With the help of doing keyword in Ruby while loop it will execute altealt once even the conditions get failed which means even condition is false once the code block will execute.
Examples of While Loop in Ruby
Examples of while loop in ruby are given below:
Example #1
Below is the first example for the while loop in the Ruby, we can explain the below example in the following steps,
- Here the goal of the program is to print all the numbers upto 10.
- First, we have defined a global variable with $ like $a and $number.
- We have initialized the value for the $a and $number as 0 and 10 respectively.
- The “While loop” starts with the condition, which will check if the $number which is going to print is greater than the $a.
- If the $number is greater than $a it will print the number, once the value of $a reaches the 10 it will fail(false).
Code:
$a = 0
$number = 10
while $a < $number do
puts("number is still greater than a = #$a" )
$a +=1
end
Output:
Example #2
Below is the second example for the while loop in the Ruby, we can explain the below example in the following steps.
- In the below example we are welcoming with greetings to the array of students.
- First, we have defined an array of students’ global variables.
- We defined a global variable $a which will use to check the length of the student’s array.
- Finally, in the condition, we are checking if the length of the array is greater then the $a variable. With this, we will be able to traverse all the array and print the greeting for each student.
Code:
$students = ["ranjan","ajay","vijay","suresh"]
$a=0
while $students.length()>$a do
@s=$students[$a]
puts("welcome to the programming world MR.#@s" )
$a +=1
end
Output:
Example #3
Below is the third example for the while loop in the Ruby, we can explain the below example in the following steps.
- In the below example we are checking if the given number is an odd number or even number.
- We have defined a global variable $number , and assigned the value 20, which means upto 220 we are checking for even and odd numbers.
- We have used the ruby method odd the number which will tell us if the number is odd or even.
Code:
$number = 0
while $number <= 20
if $number.odd?
puts "the number #$number is a odd number"
else
puts "the number #$number is a even number"
end
$number += 1
end
Output:
Conclusion
From these tutorials, we learned the working of while loop along with it’s important uses in the real-life world we also learned how while loop works in Ruby along with its common syntax and flowchart with the conditions (multiple conditions with combinations if true then success and false then fail).
Recommended Articles
We hope that this EDUCBA information on “While Loop in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.