Loops

A loop statement allows us to execute a statement or group of statements multiple times.

There are three types of loops in R programming:

  • For Loop

  • While Loop

  • Repeat Loop

for loop in R

A for loop is used to iterate over a list, vector, or any other object of elements.

for (value in sequence) {
  # block of code
}

Here, sequence is an object of elements and value takes in each of those elements. In each iteration, the block of code is executed.

while loop in R

A while loop in R is a type of loop that executes a block of code repeatedly while a certain condition is met.

The basic structure of a while loop in R is as follows:

while (condition) {
  # Code to be executed
}

In this structure, the condition is evaluated at the beginning of each iteration of the loop. If the condition is TRUE, the code inside the loop is executed, and the process is repeated. If the condition is FALSE, the loop terminates and control is returned to the rest of the program.

While loops can be very useful in R, but it's important to be careful with their usage to avoid infinite loops and slow performance. Whenever possible, it's recommended to use a for loop or a repeat loop instead of a while loop, as they provide a more concise and straightforward way to handle looping in R.

while loop with break Statement in R

A break statement can be used in a while loop to prematurely terminate the loop and exit the rest of the program.

The basic structure of a while loop in R with a break statement is as follows:

while (condition) {
  # Code to be executed

  if (exit_condition) {
    break
  }
} 

In this structure, the condition is evaluated at the beginning of each iteration of the loop. If the condition is TRUE, the code inside the loop is executed. If the exit_condition is TRUE, the break statement is executed, and the loop terminates. If the exit_condition is FALSE, the loop continues.

while loop with next Statement in R

Here's an example of a while loop in R that uses the next statement:

# Initialize a counter
i <- 1

# Start the while loop
while (i <= 10) {
  # Check if the current value of i is odd
  if (i %% 2 != 0) {
    # If i is odd, go to the next iteration of the loop
    i <- i + 1
    next
  }
  
  # If i is even, print its value
  print(i)
  
  # Increment the counter
  i <- i + 1
}

In this example, the while loop will run as long as i is less than or equal to 10. At each iteration of the loop, the current value of i is checked to see if it is odd. If i is odd, the next statement is executed, which causes the loop to go to the next iteration without executing the rest of the loop body. If i is even, its value is printed to the console. The counter i is incremented at the end of each iteration, which eventually causes the loop to terminate when i becomes greater than 10.

Output:

[1] 2
[1] 4
[1] 6
[1] 8
[1] 10

repeat loop in R

We use the R repeat loop to execute a code block multiple times.

  • However, the repeat loop doesn't have any condition to terminate the loop.

  • You need to put an exit condition implicitly with a break statement inside the loop.

repeat {
      # statements
      if(stop_condition) {
          break
      }
  }

Here, we have used the repeat keyword to create a repeat loop. It is different from the for and while loop because it does not use a predefined condition to exit from the loop.

Sources:

Sources of contents on this page:

Last updated