Updated April 3, 2023
Introduction to Ruby do-while
The statement does while Loop in Ruby allows developers to runs the same piece of code for the various time and executing code at least once for the first time, or in a more clear way while loop in ruby is a way to run the same piece of code for as many time as needed for the situation along with executing once on the start of execution, in any do while loop it consists of a condition statement(the condition can be a combination of multiple conditions and will be an expression of conditions) and if the condition is true loop will execute the code block else loop will break and for the first time code block will execute because of the do.
Below are the syntax and explanation of doing while loops in the Ruby :
while cond1 cond 2 .. [do] //(code block)here we are going to write code end
- 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 which 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 for the first time.
- end: This keyword represents the ending of the ‘while’ loop block which started from ‘do‘ keyword
Flowchart of Ruby do-while
Below is the flow chart for the do-while loop in the Ruby, we can explain the below flow chart in the following steps:
Explanation: On the starting of the condition block, it will execute the code block for once for sure. This means code block will execute for the first time then it will start the checking of the condition. Second, the 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.
This condition about checking if the conditions written inside the do-while loop are 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 do-while loop.
How to do while loop works in Ruby?
Working of the do-while loops in ruby can be explained in the below steps:
The main important facts of the ruby do-while loop are it will execute the loop at least once for the first time and then it will go for checking of the conditions. 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==false) 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 for the first time, which means even condition is false once the code block will execute on the starting of the execution.
Examples to Implement Ruby do-while
Below is the example are mentioned:
$studentsLists = ["ranjan","ajay","vijay","suresh"] $a=0 while $studentsLists.length()>$a do @s=$studentsLists[$a] puts("Welcome to the programming world mr. #@s" ) $a +=1 end
Explanation: In the above example we are welcoming with greetings to the array of students. First we have defined an array of students list 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. Here will play its role in executing it at least once on starting.
Below is the example for the while loop in the Ruby, we can explain the below example in the following steps:
$oddeven = 0 while !($oddeven > 20) do if $oddeven.odd? puts "The number #$oddeven is a odd number" else puts "The number #$oddeven is a even number" end $oddeven += 1 end
Explanation: In the below example we are checking if the given number is an odd number or even number and printing the output accordingly. We have defined a global variable $oddeven and assigned the value 20, which means up to 20 we are checking for even and odd numbers. We have used the ruby method odd for checking the number which will tell us if the number is odd or even.
From these tutorials, we learned the working of “do while” loop along with it’s important uses in the real-life world we also learned how do-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 fail.
We hope that this EDUCBA information on “Ruby do while” was beneficial to you. You can view EDUCBA’s recommended articles for more information.