while
Repeats as long as the condition is true.
Syntax
while (condition) { ... }Description
while is used when the number of iterations is not known in advance. R doesn't have i++; you write i <- i + 1.
Example
Loading editor…
Expected output
0 1 2
When to use?
Conditional repetition.
Common mistakes
- ⚠️ Forgetting to increment (n <- n + 1) creates an infinite loop.