R repeat loop

repeat loops are even more basic than while loops. They will repeat a block of code until weu tell them to stop (by hitting Escape) or until they encounter the command break, which will stop the loop.

Syntax:

repeat { statements }

x <- 1 # Declare variable x and assigned value 1

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

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9

Powered by WordPress