Ruby | Loops (for, while, do..while, until) (original) (raw)
Last Updated : 24 Sep, 2024
Looping is a fundamental concept in programming that allows for the repeated execution of a block of code based on a condition. Ruby, being a flexible and dynamic language, provides various types of loops that can be used to handle condition-based iterations. These loops simplify tasks that require repetitive actions in a program.
**The main types of loops in Ruby are:
1. while Loop
The condition that is to be tested, is given at the beginning of the loop and all statements are executed until the given boolean condition satisfies. When the condition becomes false, the control will be out of the while loop. It is also known as an **Entry Controlled Loop because the condition to be tested is present at the beginning of the loop body. So basically, _while loop is used when the number of iterations is not fixed in a program.
**Syntax:
**while conditional [**do]
code to be executed
**end
**Note: A while loop's conditional is separated from code by the reserved word _do, _a newline, _backslash(\), or a _semicolon(;).
**Flowchart:
**Example:
Ruby `
Ruby program to illustrate 'while' loop
variable x
x = 4
using while loop
here conditional is x i.e. 4
while x >= 1
statements to be executed
puts "GeeksforGeeks" x = x - 1
while loop ends here
end
`
**Output:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeksÂ
2. for Loop
"_for" loop has similar functionality as while loop but with different syntax. _for loop is preferred when the number of times loop statements are to be executed is known beforehand. It iterates over a specific range of numbers. It is also known as _Entry Controlled Loop because the condition to be tested is present at the beginning of the loop body.
**Syntax:
for variable_name[, variable...] in expression [do]
code to be executed
end
Components:
for
: A keyword that begins the loop.variable_name
: The loop variable that iterates over values.in
: A keyword used to specify the range or array to iterate over.expression
: Defines the range, array, or collection.do
: (optional) Indicates the beginning of the loop body.end
: Marks the end of the loop
**Example 1: Iterating over a range
Ruby `
Ruby program to illustrate 'for'
loop using range as expression
i = "Sudo Placements"
using for loop with the range
for a in 1..5 do
puts i
end
`
**Output:
Sudo Placements
Sudo Placements
Sudo Placements
Sudo Placements
Sudo Placements
**Output:
1
2
3
4
5
Explanation: Here, we have defined the range 1..5. Range Operators create a range of successive values consisting of a start, end, and range of values in between. The (..) creates a range including the last term. The statement **for a in 1..5 will allow _a to take values in the range from 1 to 5 (including 5).
**Example 2: Iterating over an array
Ruby `
Ruby program to illustrate 'for'
loop using array as expression
array
arr = ["GFG", "G4G", "Geeks", "Sudo"]
using for loop
for i in arr do
puts i
end
`
**Output:
GFG
G4G
Geeks
Sudo
3. do..while Loop
_do while loop is similar to while loop with the only difference that it checks the condition after executing the statements, i.e it will execute the loop body one time for sure. It is a **Exit-Controlled loop because it tests the condition which presents at the end of the loop body.
**Syntax:
loop do
code to be executed
break if Boolean_Expression
end
Here, Boolean_Expression will result in either a true or false output which is created using comparing operators (>, =, <=, !=, ==). You can also use multiple boolean expressions within the parentheses (Boolean_Expressions) which will be connected through logical operators (&&, ||, !).
**Example:
Ruby `
Ruby program to illustrate 'do..while'loop
starting of do..while loop
loop do
puts "GeeksforGeeks"
val = '7'
using boolean expressions
if val == '7' break end
ending of ruby do..while loop
end
`
**Output:
GeeksforGeeks
4. until Loop
Ruby _until loop will executes the statements or code till the given condition evaluates to true. Basically it's just opposite to the while loop which executes until the given condition evaluates to false. An _until statement's conditional is separated from code by the reserved word _do, a _newline, or a _semicolon.
**Syntax:
until conditional [do]
code to be executed
end
**Example:
Ruby `
Ruby program to illustrate 'until' loop
var = 7
using until loop
here do is optional
until var == 11 do
code to be executed
puts var * 10 var = var + 1
here loop ends
end
`
**Output:
70
80
90
100
Summary of Loop Types:
Loop Type | Control Type | Condition Evaluated | Use Case |
---|---|---|---|
while | Entry-controlled | Before loop starts | When the number of iterations is unknown. |
for | Entry-controlled | Before loop starts | When the number of iterations is known (iterating over a range or collection). |
do..while | Exit-controlled | After loop execution | When you need the loop to execute at least once. |
until | Entry-controlled | Before loop starts | When you want to loop until a condition is true |
Conclusion
Ruby offers a variety of looping constructs to handle repetitive tasks in different scenarios. The while
and for
loops are **entry-controlled, meaning the condition is evaluated before executing the loop body. On the other hand, the do..while
loop is **exit-controlled, ensuring the loop body is executed at least once. The until
loop provides an alternative way to execute a loop until a condition is met