Introduction to Loops

Learn the loops for a fixed number of iterations in Ruby.

Repetition

The repetition of an instruction or a group of instructions is a commonly encountered phenomenon in programming called a loop. As an example, say we want our program to perform the following tasks:

  • To read a document or data file line by line repeatedly until the end of the file.
  • To draw an image pixel by pixel repeatedly using color values that show it on the screen.
  • To iterate through a collection of variables one by one to process its values.
  • To compute a result by applying a formula to several values.

The for loop

Let’s say we want to display integers from 00 to 44. We can simply display the numbers 00 to 44 one by one using the print() statement. We can also use one variable and print its value repeatedly by changing the value of the variable, as shown below:

Press + to interact
# Multiple print statements with help of variable a
a = 0
print(a,"\n")
a = 1
print(a,"\n")
a = 2
print(a,"\n")
a = 3
print(a,"\n")
a = 4
print(a,"\n")

The code above clearly shows the repetitive use of print(a). The benefit of using a single variable is that we can convert this repetition into a for loop. In Ruby, we use a for loop to perform a process repeatedly for a fixed number of iterations. Let’s demonstrate this in the following program:

Press + to interact
# Repeated print statement with for loop
for a in [0,1,2,3,4]
print(a,"\n")
end

The for loop causes lines 1 and 2 to be repeated five times. So, we can say the loop has five iterations.

In the code above:

  • We use a as a variable that takes the values provided by the operator in.
  • The in operator picks the values from [0,1,2,3,4] one by one at each iteration.
  • The body of the loop starts after specifying the values in the for statement.
  • The body of the loop contains only one statement, print(a), caused by the indentation.

Let’s understand the for loop by unfolding it with the help of an execution sheet.

There are only two repeatable lines in the loop, but when we unfold the loop and write out each step as it happens, there are ten execution steps. The loop iterates all values enclosed in the range to execute each statement in the body of the loop.

Let’s take an example of generating the first five non-negative even numbers—0, 2, 4, 6, and 8.

Press + to interact
for a in [0,1,2,3,4] # Accessing each value
e = a * 2 # Multiplying the value by 2
print(e,"\n")
end

Let's understand the flow of the above program with the help of the following execution sheet that documents a loop unfolding:

The end statement after lines 2 and 3 shows that these lines are the body of the loop.

Simple loops practice

The following are a few example programs to practice writing for loops in Ruby. The “Show Solution” button will provide one possible program that solves the respective problem. You can copy and paste the given solution into the code widget to execute it. It helps to verify that your solution’s output matches the given solution. There might be several ways of writing correct solutions in programming.

Odd numbers

Write a program that shows the first five odd numbers 11,33,55,77, and 99.

Press + to interact
# Write your code here

Arithmetic sequence

An arithmetic sequence is an ordered set of numbers that have a common difference between each consecutive term.

Write a program that shows the first five terms of the arithmetic sequence 00, 55, 1010, 1515, and 2020.

Press + to interact
# Write your code here

Harmonic sequence

A harmonic sequence is an ordered set of numbers that have a common difference between the reciprocals of each consecutive term.

Write a program that shows the first five terms of the harmonic sequence 1/21/2, 1/41/4, 1/61/6, 1/81/8, and 1/101/10.

Press + to interact
# Write your code here

Geometric sequence

A geometric sequence is an ordered set of numbers that have a common multiplier between each consecutive term.

Write a program that shows the first five terms of the geometric sequence 11, 33, 99, 2727, and 8181.

Press + to interact
# Write your code here