Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

R for loop skip to next iteration ifelse

Writer Olivia Zamora

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)
}

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

1

2 Answers

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

If you want jump out of the loop, like that:

for(n in 1:5) { if(n==3) break # jump out of loop, not iterating cat(n) }

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy