How to Create Repeat Loop in R Programming

In this tutorial, we will learn to make a repeat loop with the use of R programming.

How to Create Repeat Loop in R Programming

A repeat loop is used to iterate over a block of code over several number of times.

In case of a repeat loop, there is no condition to check in for exiting repeat loop.

Hence, we must ourselves put a condition explicitly within a repeat loop body and make use of the break statement to exit the loop. Failing to do so will result into an infinite loop.

 Syntax of repeat loop

repeat {
   statement
}

When in the statement block, we must use the statement ‘break’ to exit the loop.

 r-repeat-loop-flowchart-120

Example: 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

Note that in the example above, we have only made use of a condition to check and exit the loop when x equals the value of 6.

That is why we see in our output that only values from 1 to 5 get printed.

Why not pull the strings of your career by enrolling for an intensive R programming certification course in Delhi!  DexLab Analytics, being a premier R programming training institute can help you on your endeavour.


This post originally appeared onwww.datamentor.io/r-programming/repeat-loop

Interested in a career in Data Analyst?

To learn more about Machine Learning Using Python and Spark – click here.
To learn more about Data Analyst with Advanced excel course – click here.
To learn more about Data Analyst with SAS Course – click here.
To learn more about Data Analyst with R Course – click here.
To learn more about Big Data Course – click here.

April 10, 2017 6:02 am Published by , , , , ,

, , ,

Comments are closed here.

...

Call us to know more

×