repeat loop to print the elements of a vector. Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like 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 elementsi <- 1repeat { # 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] 5Create 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] 5cat("\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. Comment More infoAdvertise with us Next Article repeat loop to print the elements of a vector. A anjugaeu01 Follow Improve Article Tags : R Language Similar Reads Print the Elements of a Vector using Loop in R A while loop is a fundamental control structure in programming that allows us to repeatedly execute a block of code as long as a specified condition remains true. It's often used for tasks like iterating over elements in a data structure, such as printing the elements of a vector. The loop continues 3 min read Replicate elements of vector in R programming - rep() Method In the R programming language, A very useful function for creating a vector by repeating a given numbervector with the specified number of times is the rep(). The general structure of rep() : rep(v1,n1). Here, v1 is repeated n1 times. R - replicate elements of vector The forms of rep() functions : r 1 min read How to Create, Access, and Modify Vector Elements in R ? In this article, we are going how to create, modify, and access vectors in vector elements in the R Programming Language. Vector is a one-dimensional data structure that holds multiple data type elements. Creating a vectorIt can be done in these ways: Using c() function.Using: operator.Using the seq 5 min read Creating a Vector of sequenced elements in R Programming - seq() Function In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function. What are sequenced elements?Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields. seq() Funct 2 min read Convert Set to Vector in C++ In C++, std::vectors stores data in the contiguous memory location while std::set stores data in non-contiguous memory location but in some specified order. In this article, we will learn different methods to convert the set to vector in C++.Table of ContentUsing Range Constructor of std::vectorUsin 3 min read Like