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: