Introduction to Loops

In R, loops are used to repeat a block of code multiple times. The most common type of loop is the for loop.

# A for loop that prints the numbers 1 to 5
for (i in 1:5) {
  print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5

Exercises

  1. Write a for loop that prints the first 5 even numbers.
# Your code here
for(i in 1:5){
  print(i*2)
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
  1. Create a numeric vector named my_vector with the numbers 1 to 10. Write a for loop that iterates over my_vector and prints the square of each number.
# Your code here
my_vector <- 1:10
for (num in my_vector) {
  print(num^2)
}
## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
## [1] 36
## [1] 49
## [1] 64
## [1] 81
## [1] 100
  1. Create a character vector named my_colors with your three favorite colors. Write a for loop that iterates over my_colors and prints each color.
# Your code here
my_colors <- c("red", "green", "blue")
for (color in my_colors) {
  print(color)
}
## [1] "red"
## [1] "green"
## [1] "blue"
  1. Write a for loop that prints the numbers from 1 to 10.
# Your code here
for (num in 1:10) {
  print(num)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
  1. Write a for loop that prints the even numbers from 1 to 10.
# Your code here
for (i in 1:10) {
  if(i%%2 == 0){
    print(i)
  }
}
## [1] 2
## [1] 4
## [1] 6
## [1] 8
## [1] 10
  1. Write a program that calculates the factorial of a given number (e.g., 5!) using a for loop.
# Your code here
calculate_factorial <- function(n) {
  factorial_result <- 1
  if (n < 0) {
    print("Factorial is not defined for negative numbers.")
  } else if (n == 0) {
    print("The factorial of 0 is 1.")
  } else {
    for (i in 1:n) {
      factorial_result <- factorial_result * i
    }
    print(paste("The factorial of", n, "is", factorial_result))
  }
}
calculate_factorial(5)
## [1] "The factorial of 5 is 120"
calculate_factorial(4)
## [1] "The factorial of 4 is 24"
  1. Write a program that prints the Fibonacci sequence up to a given number of terms (e.g., 10 terms) using a for loop.
# Your code here
fibonacci <- function(n_terms) {
  a <- 0
  b <- 1
  if (n_terms <= 0) {
    print("Please enter a positive integer.")
  } else if (n_terms == 1) {
    print(a)
  } else {
    print(a)
    print(b)
    for (i in 3:n_terms) {
      next_term <- a + b
      print(next_term)
      a <- b
      b <- next_term
    }
  }
}
fibonacci(10)
## [1] 0
## [1] 1
## [1] 1
## [1] 2
## [1] 3
## [1] 5
## [1] 8
## [1] 13
## [1] 21
## [1] 34
  1. Write a program that prints the multiplication table of a given number (e.g., 7) using a for loop. The output should be in a human-readable format (e.g., “7 x 1 = 7”).
# Your code here
multiplication_table <- function(num){
  for (i in 1:num) {
    print(paste(num,"x",i,"=",num*i))
  }
}
multiplication_table(7)
## [1] "7 x 1 = 7"
## [1] "7 x 2 = 14"
## [1] "7 x 3 = 21"
## [1] "7 x 4 = 28"
## [1] "7 x 5 = 35"
## [1] "7 x 6 = 42"
## [1] "7 x 7 = 49"
  1. Write a program that prints the prime numbers up to a given number (e.g., 20) using a for loop and conditional statements.
# Your code here
is_prime_num <- function(num){
  if(num<=1){
    return(FALSE)
  }
  if(num==2){
    return(TRUE)
  }
  for (i in 2:(num - 1)) {
    if (num %% i == 0) {
      return(FALSE)
    }
  }
  return(TRUE)
}
print_primes <- function(limit){
  for (i in 1:limit) {
    if(is_prime_num(i)){
      print(i)
    }
  }
}
print_primes(20)
## [1] 2
## [1] 3
## [1] 5
## [1] 7
## [1] 11
## [1] 13
## [1] 17
## [1] 19
  1. Write a program that prints the sum of the digits of a given number (e.g., 123) using a for loop.
# Your code here
sum_digits <- function(num) {
  num_str <- as.character(num)
  digits <- as.numeric(strsplit(num_str, "")[[1]])
  total_sum <- 0
  for (digit in digits) {
    total_sum <- total_sum + digit
  }
  print(paste("The sum of digits is:", total_sum))
}
sum_digits(1234567999)
## [1] "The sum of digits is: 55"

Solutions

Click here for the solutions