What is the use of the next and break statements in R?

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *