Ruby is an open-source object-oriented scripting language invented in the mid-90s by Yukihiro Matsumoto.
Unlike languages such as C and C++, a scripting language doesn’t talk directly to hardware. It’s written to a text file and then parsed by an interpreter and turned into code. These programs are generally procedural, meaning they are read from top to bottom.
Object-oriented languages, on the other hand, break out pieces of code into objects that can be created and used as needed. You can reuse these objects in other parts of the program or even other applications.
The announcement does at the same time as a loop in ruby lets in builders to runs the equal piece of code the numerous time and execute code at least once for the primary time, or in a more clean way while loop in ruby is a manner to run the identical piece of code for as many time as wanted for the state of affairs at the side of executing as soon as on the begin of execution, in any do even as a loop it includes a circumstance statement(the circumstance can be an aggregate of multiple situations and might be an expression of conditions) and if the situation is actual loop will execute the code block else loop will wreck and for the first time code block will execute due to 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
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: At the starting of the condition block, it will execute the code block for once for sure. This means the 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 is about checking if the conditions written inside the do-while loop are true or false. In the 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.
Working of the do-while loops in ruby can be explained in the below steps:
The principal crucial statistics of the ruby do-at the same time as the loop are it will execute the loop at least once for the primary time after which it will move for checking of the conditions. At the same time as loops in ruby are primarily based on the boolean cost which means it works on the real and fake price of the conditions. Each time loop checks for the situation and if the circumstance written for the while loop is proper it'll execute the code block and if the circumstance is false the while loop will spoil and the quit takes place.
In extra technical words, if the circumstance receives failed(circumstance==fake) then the loop may be broken. Here the condition can be a combination of many extra situations. If the situation is it'll input into the loop and execute the code block and it's going to hold doing this till the situation is real. With the assist of doing the keyword in the ruby whilst loop, it will execute altealt once even the conditions get failed for the primary time, which means that even circumstance is false once the code block will execute on the beginning of the execution.
Below is the example is mentioned:
$studentsLists = ["ranjan","ajay","vijay","suresh"] $a=0
while $studentsLists.length()>$a do
@s=$studentsLists[$a] puts("Welcome to the programming world mr. #@s" )
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
puts "The number #$oddeven is a odd number"
puts "The number #$oddeven is a even number"
$oddeven += 1
From these tutorials, we learned the working of the “do while” loop along with its important uses in the real-life world we also learned how the 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.
Subscribe to get latest updates