Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Ruby is a general-purpose, dynamic, reflective, object-oriented programming language. Everything in Ruby is an object. Ruby's development aimed to create a user interface between human programmers and the underlying computational machinery.
In programming languages, looping is a feature that allows a sequence of instructions or functions to be executed repeatedly when some of the conditions evaluate true or false.
Let’s learn about looping in Ruby in-depth.
Loops In Ruby
In Ruby, looping is a feature that allows a sequence of instructions or functions to be executed repeatedly when some of the conditions evaluate true or false.
Ruby provides a variety of loops to handle conditional situations in programs, making the programmers' jobs more manageable.
The loops in Ruby are:-
1.) While Loop
2.) For Loop
3.) Do-While Loop
4.) Until Loop
In this blog, we will cover the While and Until loops in-depth.
While Loop
In the While loop, the test condition is specified at the start of the loop, and all statements are run until the boolean condition is satisfied. The control will exit the while loop when the condition turns false. Because the condition to be tested is present at the beginning of the loop body, it is also known as an Entry Controlled Loop. Generally, a while loop is used when the number of iterations in a program is not fixed.
Syntax:
while conditional [do]
# code to be executed
end
You can also try this code with Online Ruby Compiler
Let’s see the working of the while loop with the help of a Code Example.
# Ruby program to see the working of 'while' loop
# variable count
count = 5
# using while loop
# here conditional is count i.e. 5
while count >= 1
# statements to be executed
puts "Coding Ninjas"
count = count - 1
# while loop ends here
end
You can also try this code with Online Ruby Compiler
Since we have run the While loop for count=5 number of times, it prints the Coding Ninjas five times.
Until Loop
The statements or code in a Ruby Until loop are executed till the provided condition evaluates to true. It's basically the opposite of the while loop, which runs until the specified condition evaluates to false. The conditional in an until the statement is separated from the code by the reserved word do, a new line, or a semicolon.
Syntax:
until conditional [do]
# code to be executed
end
You can also try this code with Online Ruby Compiler
Let’s see the working of until loop with the help of a Code Example.
# Ruby program to see the working of 'until' loop
count = 10
# using until loop
# here do is optional
until count==5 do
# code to be executed
puts "Coding Ninjas"
count = count - 1
# here loop ends
end
You can also try this code with Online Ruby Compiler
Since we have run the Until loop till the count is equal to 5, and we started with count=10, it will execute a total of 5 number of times the statement inside the Until loop; therefore, it prints the Coding Ninjas five times.
Frequently Asked Questions
What are Objects in Ruby?
In Ruby, everything is an object. All objects have a unique identification; they can also maintain a state and exhibit behavior in response to messages. Usually, these messages are sent out via method calls. A string is an example of a Ruby object.
What is Looping in Ruby?
In Ruby, looping is a feature that allows a sequence of instructions or functions to be executed repeatedly when some of the conditions evaluate true or false.
What are the main types of loops provided by Ruby?
There are mainly four loops in Ruby:-
1.) While Loop
2.) For Loop
3.) Do-While Loop
4.) Until Loop
How does the While loop work in Ruby?
In the While loop, the test condition is specified at the start of the loop, and all statements are run until the boolean condition is satisfied. The control will exit the while loop when the condition turns false.
How does the Until loop work in Ruby?
The statements or code in a Ruby Until loop are executed till the provided condition evaluates to true. It's basically the opposite of the while loop, which runs until the specified condition evaluates to false.
Conclusion
In this article, we have extensively discussed Looping in Ruby and how the while and until loop works in Ruby with the help of code examples.