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

repeat

In this page:

  1. repeat

repeat

repeat runs its body indefinitely with no built-in condition, relying entirely on a break statement inside to stop. This is R's equivalent of an unconditional infinite loop found in other languages (like while (true) in C-family languages). It's most useful when the exit condition is checked partway through the loop body rather than at the very start.

Note: repeat is the right choice when you need to check the exit condition somewhere in the middle of the loop body, not just at the top.

Example: repeat

markup
count <- 1

repeat {
  cat("Count:", count, "\n")
  count <- count + 1
  if (count > 3) {
    break
  }
}
🔒

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.