R repeat Loop

Introduction

In this chapter, you will learn about the repeat loop in R. The repeat loop is a control structure that allows you to execute a block of code repeatedly until a specified condition is met. Unlike the for and while loops, the repeat loop does not have a built-in condition to stop the loop; instead, you must use the break statement to exit the loop.

The repeat Loop

Basic Usage

The repeat loop continues to execute a block of code indefinitely until it encounters a break statement. It is useful when you need a loop with a condition to be checked inside the loop body.

Syntax

repeat {
  # Code to execute
  if (condition) {
    break  # Exit the loop
  }
}
  • condition: A logical expression that, when TRUE, causes the loop to stop.

Examples

Example 1: Basic repeat Loop

In this example, the repeat loop prints numbers from 1 to 5.

Example:

# Basic repeat loop
i <- 1

repeat {
  print(i)
  i <- i + 1
  if (i > 5) {
    break  # Exit the loop
  }
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5

Example 2: Summing Numbers Using repeat Loop

In this example, the repeat loop calculates the sum of numbers from 1 to 10.

Example:

# Summing numbers using repeat loop
sum <- 0
i <- 1

repeat {
  sum <- sum + i
  i <- i + 1
  if (i > 10) {
    break  # Exit the loop
  }
}

print(paste("The sum is:", sum))
# Output: [1] "The sum is: 55"

Example 3: Using break in a repeat Loop

In this example, the break statement is used to exit the loop when a specific condition is met.

Example:

# Using break in a repeat loop
i <- 1

repeat {
  if (i == 5) {
    break  # Exit the loop when i equals 5
  }
  print(i)
  i <- i + 1
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4

Example 4: Using next in a repeat Loop

In this example, the next statement is used to skip the current iteration when a specific condition is met.

Example:

# Using next in a repeat loop
i <- 1

repeat {
  i <- i + 1
  if (i %% 2 == 0) {
    next  # Skip even numbers
  }
  print(i)
  if (i > 10) {
    break  # Exit the loop
  }
}
# Output:
# [1] 3
# [1] 5
# [1] 7
# [1] 9
# [1] 11

Example Program with repeat Loop

Here is an example program that demonstrates the use of the repeat loop in R:

# R Program to Demonstrate repeat Loop

# Using repeat loop to calculate factorial of a number
factorial <- function(n) {
  result <- 1
  i <- 1

  repeat {
    result <- result * i
    i <- i + 1
    if (i > n) {
      break  # Exit the loop
    }
  }

  return(result)
}

# Calculate factorial of 5
n <- 5
fact <- factorial(n)

# Print the factorial
print(paste("The factorial of", n, "is:", fact))
# Output: [1] "The factorial of 5 is: 120"

# Using repeat loop to find the first number greater than 100 that is divisible by 7
num <- 1

repeat {
  if (num > 100 && num %% 7 == 0) {
    print(paste("The first number greater than 100 that is divisible by 7 is:", num))
    break  # Exit the loop
  }
  num <- num + 1
}
# Output: [1] "The first number greater than 100 that is divisible by 7 is: 105"

Conclusion

In this chapter, you learned about the repeat loop in R, including how to use it for basic iteration, summing numbers, and more complex tasks like calculating factorials and finding specific numbers. You also learned how to incorporate the break and next statements within a repeat loop. The repeat loop is used for performing tasks that require repeated execution with a condition to be checked inside the loop. By mastering the repeat loop, you can write more flexible and efficient R programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top