← Back to R Course | Chapter 4: Control Flow | Lesson 6 of 6

break and next

In this page:

  1. break and next

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

markup
for (i in 1:10) {
  if (i %% 2 == 0) {
    next  # skip even numbers
  }
  if (i > 7) {
    break  # stop once past 7
  }
  cat(i, "\n")
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.