fastread homefastrread library fastread menu

R Programming : repeat loop

Tutorial by:Maria Ghoste      Date: 2016-06-10 00:32:43

❰ Previous Next ❱

A repeat loop is used to iterate over a block of code multiple number of times. There is no condition check in repeat loop to exit the loop. We must ourselves put a condition explicitly inside the body of the loop and use the break statement to exit the loop. Failing to do so will result into an infinite loop.

Syntax of repeat loop

repeat {
   statement
}

In the statement block, we must use the break statement to exit the loop.

Flowchart of repeat loop

R programming repeat loop flowchart

Example of repeat loop

x <- 1

repeat {
   print(x)
   x = x+1
   if (x == 6){
       break
   }
}

 

 
 

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

In the above example, we have used a condition to check and exit the loop when x takes the value of 6. Hence, we see in our output that only values from 1 to 5 get printed.

❰ Previous Next ❱


R Programming

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website