Open In App

repeat loop to print the elements of a vector.

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to print the elements of a vector with its working example in the R Programming Language using R repeat loop. In R programming, loops are essential constructs that allow us to repeat a set of instructions multiple times. The repeat loop is one such construct that repeatedly executes a block of code until a certain condition is met. This loop is particularly useful when you want to iterate over elements without knowing the exact number of iterations in advance. In this article, we will explore how to use the repeat loop to print the elements of a vector and understand its step-by-step implementation.

Syntax:

vector <- c(...)  # Replace ... with the vector elements
i <- 1
repeat {
# Code to execute
}

Example 1:

R




my_vector <- c(1, 2, 3, 4, 5)
 
# Using repeat loop to print vector elements
i <- 1
repeat {
  print(my_vector[i])
  i <- i + 1
  if (i > length(my_vector)) {
    break
  }
}


Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
  • Create a vector named vector with the given elements.
  • Initialize a variable i with a value of 1.
  • Start a repeat loop, which will run indefinitely until explicitly broken.
  • Inside the loop, to print the element.
  • Increment the value of i by 1.
  • Use an if statement to check if the value of i has exceeded the length of the vector (length(my_vector)). If it has, the break statement is executed, which exits the repeat loop.

Example 2:

R




fruits <- c("Apple", "Banana", "Orange", "Grapes")
 
# Using repeat loop to print vector elements
i <- 1
repeat {
  cat("Fruit:", fruits[i], "\n")
  i <- i + 1
  if (i > length(fruits)) {
    break
  }
}


Output:

Fruit: Apple 
Fruit: Banana
Fruit: Orange
Fruit: Grapes
  • Create a vector named “fruits” containing different fruit names.
  • Initialize the index variable “i” to 1, which will help in accessing elements of the vector.
  • Start the repeat loop. Inside the loop:
  • Use the cat function to print the current fruit element along with the “Fruit:” label.
  • Use the “\n” character to move to the next line after each print.
  • Increment the index variable “i” using i <- i + 1 to move to the next element.
  • Check if the value of “i” is greater than the length of the “fruits” vector.
  • If the condition is met, break the loop using the break statement.
  • The loop continues until all elements of the “fruits” vector are printed.

Example 3:

R




cat("\nUsing a repeat loop:\n")
index <- 1
repeat {
  if (index > length(my_vector)) {
    break
  }
  print(my_vector[index])
  index <- index + 1
}


Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
  • cat("\nUsing a repeat loop:\n"): This line prints a message indicating that a repeat loop is being used to print the vector elements. The \n characters are used to add line breaks for better formatting.
  • index <- 1: Initialize the index variable index to 1. This variable will keep track of the current position in the vector.
  • repeat { ... }: This initiates a repeat loop, which will continue indefinitely until the break statement is executed.
  • if (index > length(my_vector)) { break }: This if statement checks if the current index is greater than the length of the vector. If it is, the loop is exited using the break statement. This prevents accessing elements beyond the vector’s length.
  • print(my_vector[index]): This line prints the element of the vector at the current index using the print function.
  • index <- index + 1: The index is incremented by 1 in each iteration to move to the next element of the vector.
  • The loop continues to iterate through the vector until all elements have been printed.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads