Back

Suppose you have a for loop like so

for(n in 1:5) { #if(n=3) # skip 3rd iteration and go to next iteration cat(n)}

for(n in 1:5) {

#if(n=3) # skip 3rd iteration and go to next iteration

cat(n)

}

How would one skip to the next iteration if a certain condition is met?

R has the next statement that is used to skip the current iteration of the loop without terminating the loop.

For example:

for(n in 1:5) { if(n==3) next #skip 3rd iteration and go to next iteration cat(n)}

if(n==3)

next #skip 3rd iteration and go to next iteration

Output:

1245

31k questions

32.4k answers

501 comments

693 users