Updated April 1, 2023
Introduction to Ruby Until Loop
Until Loop in Ruby allow developers to runs the same piece of code for the various time ,or in more clear way until loop in ruby is a way to run the same peace of code for as many time as needed for the situation, in Ruby until is a keyword which consists of the condition block and flow of loop depends on the condition block, until loop is exactly opposite to the while loop, which means it waits for true in the condition, Once the condition become true loop will halted ,so we can use the until loop according to our condition.
Syntax:
Please follow the given below code syntax for the until loop.
until cond1 cond 2 .. [do]
//(code block)here we are going to write code
end
Explanation:
- until: 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 && b==8) here “a” and “b” are 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 do block it will be able to execute the code at least once even if the conditions get success or simply conditions are true of combination.
- end: This keyword represents the ending of the until loop block which started from ‘do’ keyword.
Flowchart of Ruby Until Loop
Below is the flow chart for the until loop in 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 which will check for the true or false condition. Basically this condition is about checking if the expressions written inside the until loop are returning true or false.
- In case of a condition(cond1 and cond2) failed(false in combination of the both conditions) code block will execute and if the condition is true the loop will break.
- It will continue till the condition is false, here conditions can be combined conditions for the cond2 and cond2 or may be more and their combination expression should be false to continue the execution of the code block.
- If the condition(combination of all) is true, the loop will be halted and the end happens to the until loop.
How Does Until Loop Works in Ruby?
Working of the until loops in ruby can be explained in the below steps:
- until 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 until loop is false it will execute the code block and if the condition is true the until loop will break and the end happens.
- In more technical words ,If the condition gets success(condition output =true) then the loop will be broken .Here the condition can be a combination of many more conditions.
- If the condition is false it will enter into the loop and execute the code block and it will continue doing this until the condition output becomes true.
- With the help of do keyword in Ruby until loop, it will execute altealt once even the conditions get success(true) which means even condition is true once the code block will execute .
Examples to Implement Ruby Until Loop
Example #1
We can explain the below example in the following steps:
- Here the goal of the program is to print all the numbers upto 8.
- First we have defined a global variable with $ like $temp and $number.
- We have initialized the value for the $temp and $number as 0 and 8 respectively.
- The “until 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 8 it will be true(!condition) and loop will be halted.
Code:
$temp = 0
$number = 8
until $temp > $number do
puts("We are under the = #$temp" )
$temp +=1;
end
Output:
Example #2
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 students 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.
- Once the condition is true the loop will halt.
Code:
$a=0
until !($students.length()>$a) do
@s=$students[$a]
puts("Welcome to the programming world mr. #@s" )
$a +=1
end
Output:
Example #3
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 with the help of until loop.
- 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 of the number which will tell us if the number is odd or even.
Code:
$number = 0
until !($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 until loop along with it’s important uses in the real life world, we learned opposite in nature to the while loops, we also learned how until loop works in Ruby along with it’s common syntax and flowchart with the conditions(multiple conditions with combinations if false then success and true then fails).
Recommended Article
We hope that this EDUCBA information on “Ruby Until Loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.