In this tutorial, we will learn to make a repeat loop with the use of 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.
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 on – www.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.
R Programming, R programming certification, R Programming Training, R Programming Tutorial