Loop control statements

There are control statements that can change the normal sequence of execution. break and next are loop control statements, and we will briefly discuss these control statements here.

break

break terminates the loop and gives control to the next following statement of the loop; for example:

>Vec <- c("Hello") 
>counter <- 5 
>repeat { 
>+   print(Vec) 
>+   counter <- counter + 1 
>+   if(counter > 8) { 
>+      break 
>+   } 
>+} 

As a result of the break statement, when the preceding statement gets executed, it prints Hello four times and then leaves the loop. repeat is another loop construct that keeps executing unless a stop condition is specified.

next

next does not terminate the loop, but skips the current iteration of the flow and goes to ...

Get Learning Quantitative Finance with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.