Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
A while loop in R repeats a block of code for as long as a condition is TRUE. The condition is tested before each pass, so the loop runs zero times if it starts out FALSE. Use it when you do not know in advance how many repetitions you need; when you do, a for loop is the usual choice (see for loops in R).
What a while loop is used for in research
Research scripts reach for while when the stopping point depends on a result rather than a count: keep refining an estimate until two successive values differ by less than a tolerance you choose, keep halving a value until it falls below a threshold, or keep retrying a step until it succeeds. If you already have a vector to walk through, a for loop or a vectorized expression is simpler and avoids a loop-control mistake (see vectorizing R code).
The R Language Definition gives the syntax as while ( statement1 ) statement2: “statement1 is evaluated and if its value is TRUE then statement2 is evaluated”, and this continues until statement1 evaluates to FALSE.
Install or access
while, repeat, break and next are part of the base R language, so no package is needed. Download R from the Comprehensive R Archive Network (https://cran.r-project.org/) and run the examples below in the R console, a script, or any editor that sends code to R.
Step-by-step: write a while loop
- Set up the variable the condition depends on. The condition can only change if something inside the loop changes it.
i <- 1 - Write the condition. Per the R manual page “Control Flow”, the condition must be “a length-one logical vector that is not NA”.
while (i <= 5) { - Write the body, including the line that moves the loop toward its end.
print(i^2) i <- i + 1 } - Run it. The complete loop prints the squares of 1 to 5, one per line:
i <- 1 while (i <= 5) { print(i^2) i <- i + 1 } #> [1] 1 #> [1] 4 #> [1] 9 #> [1] 16 #> [1] 25
Keep a result: store it inside the loop
A loop does not hand back a value you can use. In the R Language Definition, “the value returned by a loop statement is always NULL and is returned invisibly”, so anything you want to keep must be assigned to a variable inside the body. This example counts how many halvings it takes to bring 100 below 1:
x <- 100
steps <- 0
while (x >= 1) {
x <- x / 2
steps <- steps + 1
}
steps
#> [1] 7
x
#> [1] 0.78125
Leave early with break, skip a pass with next
The base R manual states that break breaks out of a for, while or repeat loop, and that next “halts the processing of the current iteration and advances the looping index”. In a while loop there is no looping index, so next returns control to the condition test. That is why the counter is updated before next below; putting the update after it would leave the counter unchanged and the loop would never end.
i <- 0
while (i < 6) {
i <- i + 1
if (i %% 2 == 0) next
print(i)
}
#> [1] 1
#> [1] 3
#> [1] 5
For conditional logic inside the body, see the if statement in R.
There is no do-while: use repeat with break
Because a while loop tests its condition first, it cannot guarantee one pass. R’s repeat “causes repeated evaluation of the body until a break is specifically requested” (R Language Definition), so put the test at the end of the body when the body must run at least once:
n <- 0
repeat {
n <- n + 1
if (n >= 3) break
}
n
#> [1] 3
Output and how to read it
A while loop prints nothing by itself. Output comes only from calls such as print() inside the body, or from the variables you inspect afterwards. The loop’s own value is NULL, returned invisibly, so result <- while (...) {...} leaves result as NULL. In nested loops, break and next apply only to the innermost loop, so a break in an inner loop does not end the outer one.
Troubleshooting
The loop never stops
The loop continues until the condition is FALSE, so a condition that nothing in the body can change runs forever. Check that the body updates the variable the condition tests, and that any next sits after that update. For loops that depend on a computed value, add a counter and a break after a maximum number of passes as a safeguard.
The condition has more than one element
The base R manual says conditions of length greater than one are an error. This happens when the condition is a vector comparison such as x < 3 with x holding several values. Reduce it to a single TRUE/FALSE first. Whether that means any(x < 3) (continue while at least one element qualifies) or all(x < 3) (continue only while every element qualifies) is a decision about your analysis, not something R can choose for you. To work through a vector element by element, see vectors in R and use a for loop or a vectorized function.
The condition is NA
The condition must be a logical value that is not NA. A comparison involving a missing value can produce NA, so handle missing values before the loop or inside the condition.
The body never runs
If the condition is FALSE on the first test, the body is skipped without any message. Print the condition’s value just before the loop to confirm the starting state is what you intended.
Free alternatives
All of these are part of base R and free to use.
| Approach | When it fits | What the documentation says |
|---|---|---|
repeat with break |
The body must run at least once, or the exit test sits mid-body | Repeats the body until a break is requested (R Language Definition) |
for loop |
The number of passes is known, such as one per element of a vector | Also returns NULL invisibly; break and next work the same way (base R manual) |
| Vectorized expression | The same operation applies to every element of a vector | No loop statement to control; see vectorizing R code |
FAQ
What does while do in R?
It evaluates a condition and, while the condition is TRUE, evaluates the body again and again. It stops as soon as the condition evaluates to FALSE, or when a break runs.
What is the difference between for, while and repeat in R?
for goes through a set of values, while tests a condition before each pass, and repeat has no condition and runs until a break. All three return NULL invisibly.
How do I exit a while loop in R?
Make the condition FALSE, or call break, which exits the innermost loop being executed.
Does R have a do-while loop?
There is no do-while keyword in the syntax documented for the base language. Use repeat with a break test at the end of the body.
Sources (official documentation)
- R Documentation, base package, “Control Flow” (
while,repeat,break,next): https://stat.ethz.ch/R-manual/R-devel/library/base/html/Control.html, checked 2026-10-29. - R Core Team, R Language Definition, sections 3.2.2 to 3.2.4: https://cran.r-project.org/doc/manuals/r-release/R-lang.html, checked 2026-10-29.
- The Comprehensive R Archive Network, checked 2026-10-29: https://cran.r-project.org/.







