6.6 break
/next
to exit and skip interations
The break
argument is used to exit a loop immediately, regardless of what iteration the loop may be on. break
arguments are typically embedded in an if
statement in which a condition is assessed, if TRUE break
out of the loop, if FALSE continue on with the loop. In a nested looping situation, where there is a loop inside another loop, this statement exits from the innermost loop that is being evaluated.
In this example, the for
loop will iterate for each element in x
; however, when it gets to the element that equals 3 it will break out and end the for
loop process.
The next
argument is useful when we want to skip the current iteration of a loop without terminating it. On encountering next
, the R parser skips further evaluation and starts the next iteration of the loop. In this example, the for
loop will iterate for each element in x
; however, when it gets to the element that equals 3 it will skip the for
loop execution of printing the element and simply jump to the next iteration.