Table of contents
1.
Introduction
2.
Loops In Ruby
3.
While Loop
4.
Until Loop
5.
Frequently Asked Questions
5.1.
What are Objects in Ruby?
5.2.
What is Looping in Ruby?
5.3.
What are the main types of loops provided by Ruby?
5.4.
How does the While loop work in Ruby?
5.5.
How does the Until loop work in Ruby?
6.
Conclusion
Last Updated: Mar 27, 2024

While and Until in Ruby

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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
Run Code

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
Run Code

Output:

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
Run Code

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
Run Code

Output:

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.

After reading about the While and Until in Ruby, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Object Marshalling in RubyTainting Objects in RubyCopying Objects In RubyHow To Invoke Global Functions In Ruby?, and Object References in Ruby.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Coding!

Live masterclass