Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
While and Until as Loops
2.1.
While Loop
2.1.1.
Code
2.1.2.
Output
2.1.3.
Code
2.1.4.
Output
2.2.
Until Loop
2.2.1.
Code
2.2.2.
Output
2.2.3.
Code
2.2.4.
Output
3.
While and Until as Modifiers
3.1.
Ruby While Modifier
3.1.1.
Code
3.1.2.
Output
3.2.
Ruby Until Modifier
3.2.1.
Code
3.2.2.
Output
4.
Frequently Asked Questions
4.1.
What is the use of break statements in Ruby?
4.2.
Can we use nested loops in Ruby?
4.3.
Is the foreach loop present in Ruby?
4.4.
When does a while loop become an infinite loop in Ruby?
4.5.
What are closures in Ruby?
5.
Conclusion
Last Updated: Mar 27, 2024

While and Until as Modifier in Ruby

Author Naman Kukreja
0 upvote

Introduction

For a programmer choosing a perfect programming language for a project is one of the most crucial tasks as some programming languages have different features and advantages. The same goes with ruby. It has various features that make it one of the top choices of programmers in today’s time.

Suppose you have to print a string 15 times in ruby. You can do so by using the print statement 15 times by writing them individually. Or you can use the loops feature provided by ruby to avoid repetitive tasks. You can take another example where you must repeatedly repeat a fixed task. The loops are the perfect replacement. It saves time for the user so they can focus on any other problem.

We will learn all about loops while moving further with this blog, so without wasting any time further, let’s get on with our topic.

While and Until as Loops

In any language, loops are used to perform repetitive code execution for a certain amount of time until the given conditions are met to reduce the time and effort. There are mainly four types of loops available in Ruby:

  • While
  • Do while
  • Until
  • For

In this blog section, we will learn about while, until loops as do while is almost similar to the while loop.

While Loop

While the loop is given as a parameter that evaluates the conditions as either true or false, the while loop will only continue to work until the condition is true. The moment the condition becomes false, the while loop stops working or executing, and the program after the while loop will start executing. You can write any logic inside the while loop that you want to execute. You will get the output according to the logic implemented inside the while loop.

The general syntax of the while loop is 

while(condition)
Statements
end

As we have discussed above, first, the condition will be checked if the statement is correct or true, then the statement inside the while loop is performed, and after each execution, the condition is checked again. When the condition becomes false, the loop terminates immediately.

Let’s understand this with an example. Below is the example of printing “Hello, World” 5 times.

Code

#while loop
count  = 1
while count<=5
           puts("Hello, World")
 end

Output

The output of the following code will be this:

But how we only want to print it 5 times, then why it is printing infinite times. The reason is that we have initialized count with 1 and have the condition to terminate the loop when the count becomes greater than 5. But in the while loop, no statement increments the value of the count. So the value of the count never becomes greater than 5, so it will stick for an indefinite time, known as an infinite loop.

Code

The correct code will look like this:

#while loop
count  = 1
while count<=5
           puts("Hello, World")
count+=1
 end 
 puts("End of while loop")

Now we are incrementing the count value after every time printing “Hello, World,” So when the value becomes 6, the loop terminates, and the puts statement will execute.

Output

The output will be like this:

Until Loop

While and until loops are pretty similar. The difference between the two is that the while loop executes the statements in the loop until the condition becomes false.

In contrast, the until loop executes the statements in the loop until the condition becomes true. You can refer until loop as vice versa for the while loop.

Syntax of Until Loop:

until (condition)
statements
end

The above case of the infinite loop also exists here, as if we do not change the variable's value inside the loop, then the loop will become an infinite loop. The example has been shown below:

Code

#Until loop
count  = 1
Until count>5
           puts("Hello, World")
 end

Output

Code

Now we will increment the value of the count inside the loop:

#Until loop
count  = 1
Until count>5
           puts("Hello, World")
count+=1
end
puts("End of Until")

Output

When the value of the count becomes 6, the loop terminates, and the output will be like this:

While and Until as Modifiers

We can use while and until as modifiers as they reduce the complexity and size of the code. We will learn how to use them as modifiers in the blog section.

Ruby While Modifier

There are two syntaxes to use while as a modifier. One does not involve benign and end keyword, whereas the other involves whereas and end keyword Both the syntaxes are shown below:

code while condition

 
OR

 
begin
code
end while conditional

In the first syntax, we write code first, followed by the while statement, whereas in the second syntax, we first write the begin keyword, which tells the compiler that the loop has been started, as shown below:

Code

#!/usr/bin/ruby

 
$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1
end while $i < $num

Output

The output will be like this:

If a while modifier follows a begin statement with no ensure or rescue code, clauses are executed once before the conditional is evaluated.

Ruby Until Modifier

Like While Modifier, the until modifier also has two syntaxes, one without the begin and end keywords and another with begin and end keywords.

code until conditional

 
OR

 
begin
code
end until conditional

It executes the loop while the condition is false. In the first syntax, we write code first, followed by until statement, whereas in the second syntax, we first write the begin keyword, which tells the compiler that the loop has been started, as shown below:

Code

#!/usr/bin/ruby

 
$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1;
end until $i > $num

Output

The output will be like this:

If an until modifier follows a begin statement with no ensure or rescue code, clauses are executed once before the conditional is evaluated.

Remember that they must appear on the same line as the loop body they modify when using while and until as loop modifiers. The Ruby interpreter will regard the loop body as an array if there is a newline between the loop body and the while or till keyword. While or till the start of a normal loop with an unchanged expression.

When the modifiers while and until are applied to a single Ruby expression, the loop is created. Despite being written after the loop body, the condition is tested first. The body of the loop is if it were formatted as a conventional while or until loop, it would be executed zero or more times.

Frequently Asked Questions

What is the use of break statements in Ruby?

The break statement is used to exit the current loop in execution. We can use this loop when a certain condition is met inside the loop, and we do not need the loop to execute further. Then we can write inside the loop as it saves some iterations.

Can we use nested loops in Ruby?

Yes, we can use nested loops inside Ruby. We use nested loops in the case when we have to perform various repeated operations for each outer operation.

Is the foreach loop present in Ruby?

No, the foreach loop is not a looping statement in Ruby.

When does a while loop become an infinite loop in Ruby?

When the condition parameter is not changed in the while loop, the while condition never becomes false, so the loop continues till infinite, and the while loops become infinite.

What are closures in Ruby?

The closure is a variable that can be passed to any function as an argument or assigned to another variable.

Conclusion

In this article, we have extensively discussed while and until as loops with their syntaxes and examples and various cases where a loop can become an infinite loop and at last while and until as modifiers with their syntaxes and proper explanations with codes.

Suppose you are not much comfortable with Ruby and still wondering about whether you should proceed with Ruby or not. Don't worry. Coding ninja has you covered in this. There are the top 8 reasons why Ruby should be your first programming language!.

And for any further queries regarding Ruby, you can visit here. You will get answers to almost all of your queries.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, React, JavaScript, System Design, etc. Refer to the mock test and problems; If you are looking for practice questions to enter in Tech Giants like Amazon, Microsoft, Uber, etc., you must look at the interview experiences and interview bundle for placement preparations. 

Do upvote our blog to help other ninjas grow.

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

 “Happy Coding!”

Live masterclass