Repeat Loop in R Programming Language: Syntax and Examples

Rumman Ansari   Software Engineer   2024-07-05 06:47:28   5665  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

The Repeat loop executes the same code again and again until a stop condition is met.

Syntax

The basic syntax for creating a repeat loop in R is −

repeat { 
   commands 
   if(condition) {
      break
   }
}

Example

 Live Demo

v <- c("Hello","loop")
cnt <- 2

repeat {
   print(v)
   cnt <- cnt+1
   
   if(cnt > 5) {
      break
   }
}

When the above code is compiled and executed, it produces the following result −

[1] "Hello" "loop" 
[1] "Hello" "loop" 
[1] "Hello" "loop" 
[1] "Hello" "loop" 
MCQ Available

There are 2 MCQs available for this topic.

2 MCQ