break and next
In this page:
break and next
break immediately exits the nearest enclosing loop, while next skips the rest of the current iteration and moves to the next one -- R's equivalent of continue in other languages. Both work inside for, while, and repeat loops. They give finer control over loop execution beyond the loop's own condition.
Note: R uses next where many other languages use continue -- remember this naming difference.
Example: break and next
for (i in 1:10) {
if (i %% 2 == 0) {
next # skip even numbers
}
if (i > 7) {
break # stop once past 7
}
cat(i, "\n")
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: