Loops are used to execute a block of code repeatedly until a specified condition is met. This helps us avoid writing the same code again and again.
Ruby provides many looping techniques. The most common loops in Ruby are:
- while loop
- until loop
- for loop
- times loop
- each loop
- loop do...end
- begin...end while
Let's start with the while
loop.
Ruby while Loop
The while
loop executes a block of code as long as the given condition is true
. Its syntax is:
while condition
# Code to execute
end
Here's how this loop works:
- The
while
loop first checks thecondition
. - If the
condition
istrue
, the code inside the loop is executed. - The loop continues until the
condition
becomesfalse
. - The loop is never executed if the
condition
isfalse
from the outset.
Example: Ruby while Loop
num = 1
# Use while loop to print numbers from 1 to 3
while num <= 3
puts num
num += 1
end
puts "Loop finished!"
Output
1 2 3 Loop finished!
Here, the loop runs three times because of the condition num <= 3
. Here's how the program works:
- The
num
variable is initialized to 1. - The
while
loop starts because the conditionnum <= 3
istrue
(since 1 is less than 3). - Inside the loop, the value of
num
is printed. Thus, we get 1 as an output. - Then, the value of
num
is increased by 1 with the codenum += 1
. - Thus, the first iteration of the loop is over.
- The second iteration begins because
num <= 3
istrue
again (since 2 is less than 3). - The whole process repeats itself until
num
is greater than 3. Then, the loop terminates. - As a result, all numbers from 1 to 3 are printed.
- Finally, the code outside of the loop is executed. Thus,
Loop finished!
is printed.
To learn more, visit Ruby while Loop.
Ruby until Loop
The until
loop is the opposite of the while
loop. It runs a block of code until the given condition becomes true
. Its syntax is:
until condition
# Code to execute
end
Example: Ruby until Loop
x = 1
until x > 3
puts x
x += 1
end
Output
1 2 3
In this example, the loop runs until x > 3
becomes true
. In other words, it stops when x
becomes 4.
Ruby for Loop
The for
loop in Ruby is used to iterate over a range or a collection (like arrays or hashes). Its syntax is:
for variable in collection
# Code to execute
end
Here,
collection
- A range of values, or a collection of values like arrays or hashes.variable
- A placeholder that takes the value of each element in thecollection
during each iteration.
Note: Many Ruby programmers prefer the each
loop over the for
loop when iterating over collections.
Example: Ruby for Loop with Range
for i in 4..6
puts i
end
Output
4 5 6
Here, the for
loop iterates over the range 4..6
. As a result, the loop runs three times over the numbers 4, 5, and 6.
In each iteration of the loop, the variable i
takes values from 4 to 6, which is then printed to the screen. Here's how it works:
Loop Iteration | Value of i |
---|---|
First | 4 |
Second | 5 |
Third | 6 |
Example: Ruby for Loop with Array
people = ["Bob", "Alex", "Sharon", "Cassie"]
for person in people
puts person
end
Output
Bob Alex Sharon Cassie
Here, the for
loop iterates over the people
array. In each iteration of the loop, the variable person
takes the corresponding element of the array. Here's how this works:
Loop Iteration | Value of person |
---|---|
1 | "Bob" |
2 | "Alex" |
3 | "Sharon" |
4 | "Cassie" |
To learn more, visit Ruby for and each Loops.
Ruby times Loop
The times
loop is a concise way to repeat something a fixed number of times.
The syntax of the times
loop is:
n.times do
# Code to execute
end
Here, the loop is repeated n
number of times.
Example: Ruby times Loop
3.times do
puts "Knock!"
end
Output
Knock! Knock! Knock!
The above loop runs three times. In each iteration, we print the string "Knock!"
. So, Knock!
is printed three times.
Note: We often use the times
loop for simple repetitions.
Ruby each Loop
The each
loop is used to iterate over each element in a collection like an array. Its syntax is:
collection.each do |item|
# Code using item
end
Here,
collection
- A collection of values like arrays or hashes.item
- A placeholder that takes the value of each element in thecollection
during each iteration.
Example: Ruby each Loop
people = ["Bob", "Alex", "Sharon", "Cassie"]
people.each do |person|
puts person
end
Output
Bob Alex Sharon Cassie
Here, the each
loop runs once for each item in the people
array.
In each iteration of the loop, the person
variable takes a single element from the people
array, which is similar to how our for
loop example works:
Loop Iteration | Value of person |
---|---|
1 | "Bob" |
2 | "Alex" |
3 | "Sharon" |
4 | "Cassie" |
To learn more, visit Ruby for and each Loops.
Ruby loop do...end
The loop do...end
runs infinitely unless we explicitly use a control statement like break
to exit the loop. Its syntax is:
loop do
# Code to execute
end
Example: Ruby loop with break
i = 1
loop do
puts i
i += 1
# Terminate the loop if i is greater than 3
break if i > 3
end
Output
1 2 3
Here, the loop runs infinitely, but the break
statement stops it when i
becomes greater than 3.
Notice that this loop behaves similarly to a while
loop, but we need to manage the break
condition ourselves.
To learn more, visit Ruby loop do.
Ruby begin...end while Loop
Ruby also provides the begin...end while
loop, which is similar to a do...while
loop in other languages.
In this loop, the code is executed at least once before the condition is checked. Its syntax is:
begin
# Code to execute
end while condition
Here, the begin
loop first executes the code, and only then checks the condition
:
- If the
condition
istrue
, the loop runs again. - If the
condition
isfalse
, the loop ends.
Example: Ruby begin...end while Loop
x = 1
begin
puts x
x += 1
end while x <= 3
Output
1 2 3
Here, the loop prints the value of x
starting from 1 and runs until x
becomes 4.
As you can see, this loop acts just like a while
loop, but the first iteration always happens regardless of whether the condition is true
or false
.
Note: The begin...end while
loop is discouraged by many Ruby developers because it can reduce readability and make code harder to maintain. Instead, they prefer loop do...end
with a break
statement, which is clearer and more flexible.