The next
statement is used to skip a particular iteration and jump to the next one if a certain condition is met. The break
statement is used to stop and exit the loop at a particular iteration if a certain condition is met. When used in one of the inner loops of a nested loop, this statement exits only that inner loop.
Both next
and break
statements can be used in any type of loops in R: for loops, while loops, and repeat loops. They can also be used in the same loop, e.g.:
for(i in 1:10) {
if(i < 5)
next
if(i == 8)
break
print(i)}
Powered By
Output:
[1] 5
[1] 6
[1] 7
Leave a Reply