logo

LOOP STATEMENTS


A loop statement allows us to execute a statement or group of statements multiple times and the following is the general form of a loop statement in most of the programming languages −




The basic syntax for a loop statement in R is

R programming language provides the following kinds of loop to handle looping requirements.

Repeat loops

The Repeat loop executes the same code again and again until a stop condition is met.

The basic syntax for creating a repeat loop in R is −

repeat {
  commands
    if(condition) {
              break
                  }
      }

The flow diagram for repeat loop is:

An example of repeat loop is

v = c("Cotton","University")
count = 1

repeat {
   print(v)
   count = count+1
   
   if(count > 5) {
      break
   }
}
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"

The break statement is use to stop a loop when a certain condition is fulfilled.

While loop

The basic syntax for creating a while loop in R is −

 while (test_expression) {
    statement 
  }

The flow diagram for while loop is:

Here key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

An example of While loop

v = c("Cotton","University")
count = 1

while (count < 7) {
   print(v)
   count = count + 1
}
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"

The above while loop is repeated until count is less than 7, hence it is repeated 6 times.

However if the while condition is false at the very first step then the loop is not executed at all. For example:

v = c("Cotton","University")
count = 8

while (count < 7) {
   print(v)
   count = count + 1
}

The above while loop print nothing as the test condition is false.

For loops

A For loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

The basic structure of a for loop is

for (value in vector) { 
                 statements
                }

The flow diagram of for loop is:

An example of for loop

v=c("Cotton", "University")
for (i in 1:10) {
  print(v)
}
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"
[1] "Cotton"     "University"

The above for loop print the variable v 10 times.

a_vector=rep(10,10)     # We have created a vector of size 10 with all values = 10
for (i in 1:10) {   
  a_vector[i]=a_vector[i]+i
}
a_vector
 [1] 11 12 13 14 15 16 17 18 19 20

We can use multiple for loops in the same program. Also for loops can also be nested.

An example of nested for loops is given below:

a_matrix=mat.or.vec(3,3) #Creating an empty matrix of size 3x3
a_matrix
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
for (i in 1:3) {
  for (j in 1:3) {
    a_matrix[i,j]=a_matrix[i,j]+(i*j)
  }
}
a_matrix
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9

In the above example we have nested two for loops. At first we have created an empty matrix with all elements equal to 0. The loops assign the product of their position to each element of the empty matrix.

for loops are particularly flexible in that they are not limited to integers, or even numbers in the input. We can pass character vectors, logical vectors, lists or expressions.

v <- LETTERS[1:6]
for ( i in v) {
   print(i)
}
[1] "A"
[1] "B"
[1] "C"
[1] "D"
[1] "E"
[1] "F"

Note: the function mat.or.vec (row number, column number) is use to generate an empty matrix or vector.

Try Yourself