Updated March 31, 2023
Introduction to Ruby Loops
Loops in Ruby are used to execute the same block of code for a given amount of repetitions or until a certain condition is met, used when there is need to execute the same statement or tasks multiple times, for example, if you want to print the number from 1 to 10, instead of printing each number, we can use a loop to automate the task. Usage of the loop also reduce the number of the line used in the program, so it reduces the length of the code and automates the certain task.
Types of Loops in Ruby
There are four types of loops in Ruby which are as follows:
- for loop
- while loop
- do-while loop
- Until loop
Now we will discuss all these one by one in detail.
1. For Loop
for loop is entry controlled loop which is used to perform certain operations repeatedly based on the condition. For loop first checks the condition, if it is true then it will iterate the loop that is the reason it is called an entry controlled loop. In for loop the number of iterations is decided based on the condition, hence it is suitable for the program where the number of iterations to be performed is fixed.
Syntax:
for variable in expression do
statement to be executed
end
In the syntax mentioned above for is a keyword which is used in for loop, a variable is the name of the variable (variable name can be anything except reserved keywords). in is also the keyword which checks the condition following the expression, the expression is the condition used to evaluate the code or the program. do is the keyword which transfers the flow control inside the loop if the condition becomes true else it will transfer the flow control to end and will terminate the program. Inside the for loop, the statement that needs to be executed is declared. end is the keyword used to terminate the code.
Example to Implement for loop in ruby:
Code:
puts("Enter the maximum number: ")
num = gets.chomp.to_i
puts("The series: ")
for i in 1..num do
puts(i)
end
Output:
Explanation:
Here we have written a program to print the series according to the users’ wishes. The program first prompts a message by showing Enter the maximum number. variable num is used to Store the value which is entered by the user. gets.chomp will allow a user to enter their value.1..num condition is used to print the series from 1 to the maximum value specified by the user. The loop will iterates until the condition becomes false and print the result.
2. While Loop
Like for loop, while loop is also used to iterates the program repeatedly until the condition gives a false result. while the loop first checks the condition, if the condition is true it will transfer the flow control inside the loop and repeat the same procedure until the condition becomes false. while loop is suitable for the program where the number of iterations to be performed is not fixed.
Syntax:
while condition
statement to be executed
end
in the above-mentioned syntax, while it is the keyword used for a while loop, the condition is used to evaluate a program that is defined by the user. if the condition is true it will execute the statement defined in the loop, else will terminate the program and transfers flow control outside the loop. The end is the keyword that is used to terminate the program.
Example to Implement while loop in ruby:
Code:
x = 1
puts("Enter the maximum number: ")
num = gets.chomp.to_i
puts("The series: ")
while x <= num
puts(x)
x += 1
end
Output:
Explanation:
Here we have written a program to print the series of numbers the same as we have performed in for loop, the same is written for while loop. The only difference is here we have the while syntax and different conditions. Here we have written a program where variable x is initialized with 1 and variable num’s value will be defined by the user. Until the value of x is less than or equal to num it will print the value and increment the value of x by 1.
3. Do While Loop
Do While Loop is quite similar to the while loop, the only difference is the do-while loop executed at least one time because the condition which is used to evaluate the program is written at the end of the loop, hence loop executed at least one time.
Syntax:
loop do
Statement to be executed
break if condition
end
in the above-mentioned syntax do is a keyword used for a do-while loop, do loop will transfer the flow control inside the loop and statement will be executed. break if tare the keyword that will break the loop if the condition becomes true and transfer flow control to the end Statement which will terminate the program.
program to implement do while loop in ruby
Code:
loop do
puts "Checking for answer"
x = gets.chomp
if x != '10'
break
end
end
Output:
4. Until Loop
until loop is also used to execute the loop repeatedly. until loop will iterate the loop until the condition becomes true.
Syntax:
until condition
statement to be executed
end
in the above-mentioned syntax until is the keyword used for until loop, until keyword will check the condition and execute the loop until the condition mentioned does not become true. Once the condition becomes true, it will terminate the code.
Example to Implement until loop in ruby:
Code:
x = 1
puts("Enter the maximum number: ")
num = gets.chomp.to_i
puts("The series: ")
until x == num
puts(x)
x += 1
end
Output:
The same program is written which is written for all the above programs using the until keyword. The concept and the logic are the same as other examples.
Recommended Article
We hope that this EDUCBA information on “Loops in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.