R Repeat - r - learn r - r programming
- In general, the Repeat loop executes the same condition many times until a stop condition is satisfied.
- A repeat loop is used to iterate over a block of code multiple number of times.
![repeate loop](https://cdn.wikitechy.com/tutorials/r-programming/repeate-loop.png)
Syntax
repeat
{
commands
if(condition)
{
break
}
}
Sample Code
x <- 1
repeat
{
print(x)
x = x+1
if (x == 10)
{
break
}
}
Code Explanation
![repeate loop structure](https://cdn.wikitechy.com/tutorials/r-programming/repeate-loop-structure.png)
- x specifies to string variable. Here x is initialized to value as 1.
- Here print() is used to print the value and that value stored in x variable.
- x = x+1, which means the x = 1 is incremented by 1 and becomes 9. If the condition to check and exit the loop when x takes the value of 10. Here break statement is used to exit the loop.
Output
![repeate loop output](https://cdn.wikitechy.com/tutorials/r-programming/repeate-loop-output.png)
- Here we have used a condition to check and exit the loop when x takes the value of 10. Hence, we have displayed the output that only values from 1 to 9 get printed.