r loops

R Loops

Posted on

Loops in R

The R programming language offers three types of looping statements:

  1. R repeat loop – This involves repeating a block of statements indefinitely. A condition to break out of the loop must be included within the repeat block.
  2. R while loop – This executes a block of statements repeatedly as long as the condition specified in the while statement evaluates to TRUE.
  3. R for loop – This runs a block of statements for each item in a list provided to the for loop.

The R programming language also allows for the abrupt termination of a loop.

The R break statement allows for the insertion of an extra condition within any of the previously discussed R loops. If this condition is met, incorporating a break statement within the conditional code will terminate the current loop instantly.

R repeat loop

The R Repeat Loop performs the repetition of a group of statements within a loop until a termination condition is fulfilled.

repeat {
      statements
      if(stop_condition) {
          break
      }
  }

While it’s not obligatory, omitting a termination condition in the repeat loop statement will cause the statements within the repeat block to run endlessly, resulting in an infinite loop. It’s important that the breaking condition yields a boolean value, either TRUE or FALSE. The Repeat Loop executes until the breaking condition becomes TRUE.

Example – Repeat Loop:

b = 1
repeat {
  # starting of repeat statements block
  print(b)
  b = b+1
  # ending of repeat statements block
  if(b>8){ # breaking condition
    break
  }
}

#output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8

R While Loop

The While Loop repeatedly executes a group of statements within its body within a loop, continuing as long as the condition remains TRUE.

The syntax:

while (expression) {
     statement(s)
}

When the boolean expression evaluates to FALSE, the program flow exits the while loop statement.

Example – R while loop:

m <- 1
while(m<4) {
  print(m)
  m = m+1
}

#Output:
[1] 1
[1] 2
[1] 3

Break statement in while loop

The while loop can be terminated not only by the boolean expression following the while keyword but also by a deliberately placed break statement within the loop itself.

Example – Break inside while loop:

m <- 1
n <- 4
while(m<6) {
  if (m == n) {
    break
  }
  print(m)
  m = m+1
}

#Output:
[1] 1
[1] 2
[1] 3

R For Loop

The R For Loop runs a group of statements for every element within a vector that is given to it.

The Syntax:

for (x in vector) {
     statement(s)
}

The statements within the for loop are performed for every element within the vector. Once there are no more elements in the vector, control exits the for loop and proceeds to execute statements following it.

Example – For Loop:

m = c(1, 31, 5, 17)
for(i in m) {
  print(i)
}

#output:
[1] 1
[1] 31
[1] 5
[1] 17

Example – Break statement inside For Loop:

m = c(1, 31, 5, 17)
for(i in m) {
  if(i==5){
    break
  }
  print(i)
}

#output:
[1] 1
[1] 31

Congratulations! You have passed R Loops tutorials.