R: Repeat Loop

Repeat loop is created with the help of repeat keyword in R.It is used to iterate over a block of code multiple numbers of times.

In a repeat loop, we do not check any condition to exit the loop. For exiting, we include a break statement that executes when the condition within the loop body results to be true.

This property of the repeat loop makes it different from other loops. In R language we can create an infinite loop very easily using the Repeat loop.

Syntax

repeat { 
   Statements 
   if(condition) {
      break
   }
}

Flowchart

R Repeat Loop

Example

a<- 1
i <- 1
repeat 
{
    print(a)
    i <- i + 1
    if(i > 5) 
    {
        break
    }
}

Output

[1] 1
[1] 1
[1] 1
[1] 1
[1] 1