Instead of skipping when a condition is met, you should not skip when a condition is not met--for(n in 1:5){if(n!=3){cat(n)}}
– MichaelChirico
2 Answers
183
votes
for(n in 1:5) {
if(n==3) next # skip 3rd iteration and go to next iteration
cat(n)
}
-1
votes
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) }
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
for(n in 1:5){if(n!=3){cat(n)}}
– MichaelChirico