![]() |
VOOZH | about |
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:
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.
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(;).
Output:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
"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.
for variable_name[, variable...] in expression [do]
# code to be executed
end
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 loopOutput:
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).
Output:
GFG
G4G
Geeks
Sudo
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.
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:
Output:
GeeksforGeeksRuby 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.
until conditional [do]
# code to be executed
end
Example:
Output:
70
80
90
100
| 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 |
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