Command syntax can be seen in ?Control :
while(cond) expr repeat expr
This makes it clear that while checks the condition to determine when to end the loop, but repeat requires you to explicitly specify the break loop yourself and can be anywhere in the body of the loop. Depending on where you place your break statement, repeat can do other parts of the iteration compared to while .
Consider these 2 forms of the same cycle:
i <- 0 repeat {if (i==2) break;print(i);i<-i+1} [1] 0 [1] 1 i [1] 2 i <- 0 while (i!=2) {print(i);i<-i+1} [1] 0 [1] 1 i [1] 2
James
source share