R for loop skip to next iteration ifelse

alki picture alki · Aug 18, 2015 · Viewed 111k times · Source

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?

Answer

Alexey Ferapontov picture Alexey Ferapontov · Aug 18, 2015
for(n in 1:5) {
  if(n==3) next # skip 3rd iteration and go to next iteration
  cat(n)
}