Repeat loop in R is used to iterate over a block of code multiple number of times. And also it executes the same code again and again until a break statement is found.
Repeat loop, unlike other loops, doesn’t use a condition to exit the loop instead it looks for a break statement that executes if a condition within the loop body results to be true. An infinite loop in R can be created very easily with the help of the Repeat loop. The keyword used for the repeat loop is 'repeat'
.
Syntax:
repeat {
commands
if(condition) {
break
}
}
Flowchart:

Example 1:
result < - c( "Hello World" )
i < - 1
repeat {
print (result)
i < - i + 1
if (i > 5 ) {
break
}
}
|
Output:
[1] "Hello World"
[1] "Hello World"
[1] "Hello World"
[1] "Hello World"
[1] "Hello World"
Example 2:
result < - 1
i < - 1
repeat {
print (result)
i < - i + 1
result = result + 1
if (i > 5 ) {
break
}
}
|
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Apr, 2020
Like Article
Save Article