Ruby do-while

Ruby do-while
Written by Sagar RabidasDecember 30, 2021
10 min read
Sagar Rabidas

Software Developer

Today we will discuss in this blog, Ruby do-while.


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.


Ruby do-while:-

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


  • while: This is the ruby predefined keyword that 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 are 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 the ‘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: 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.

the do-while loop works in Ruby

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.

Examples to Implement Ruby do-while

Below is the example is mentioned:

Example #1


$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

Example #2

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"
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.

ruby on rails
ruby do while
Was this blog helpful?
You must be Logged in to comment
Code Block
Sagar Rabidas
Software Developer
162 Blog Posts
15 Discussion Threads
Trending Categories
Frontend Development24
Backend Development19
Server Administration17
Linux Administration24
Data Center24
Penetration Testing14

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:


Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright @2020
made with by Abhishek & Priyanka Jalan