x <- 1
while (x <= 5) {
print(x)
x <- x + 1
}[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Not all loops in R have a fixed number of repetitions. Sometimes you want your code to keep running until a certain condition is met. In these cases, you can use conditional loops, which rely on logical tests rather than predefined sequences. R provides two main forms of conditional looping: the while loop and the repeat loop.
A while loop checks a condition before each iteration and keeps running as long as that condition is TRUE. For example, the following loop prints numbers from 1 to 5:
Here, the variable x starts at 1. Before every iteration, R checks whether x <= 5. If the condition is true, the code inside the braces runs. Once the condition becomes false, the loop stops automatically.
A repeat loop, on the other hand, does not check a condition at the start—it runs indefinitely until you explicitly tell it to stop using break().
This produces the same output as the previous example. The repeat loop is useful when the stopping condition depends on a computation that happens inside the loop, rather than something known in advance.
You can also control the flow inside loops using two special commands: break and next. break, as we saw, immediately stops the loop, even if the condition is still true, while next skips the rest of the current iteration and moves on to the next one.
Here’s a simple demonstration:
[1] 1
[1] 2
[1] 4
This prints 1, 2, and 4 — it skips 3 because of next, and stops entirely before reaching 5 because of break.
Conditional loops and flow control give you flexibility: you can make loops respond to data dynamically, handle exceptions, or stop automatically when a specific goal is reached.
Write a conditional loop that print all the possible combination between the numbers 1 to 10 in where the first number is bigger than the second number
i <- 1
while(i <= 10){
j <- 1
while(j <= 10){
if(i < j) break
print(paste(i, j))
j <- j + 1
}
i <- i + 1
}[1] "1 1"
[1] "2 1"
[1] "2 2"
[1] "3 1"
[1] "3 2"
[1] "3 3"
[1] "4 1"
[1] "4 2"
[1] "4 3"
[1] "4 4"
[1] "5 1"
[1] "5 2"
[1] "5 3"
[1] "5 4"
[1] "5 5"
[1] "6 1"
[1] "6 2"
[1] "6 3"
[1] "6 4"
[1] "6 5"
[1] "6 6"
[1] "7 1"
[1] "7 2"
[1] "7 3"
[1] "7 4"
[1] "7 5"
[1] "7 6"
[1] "7 7"
[1] "8 1"
[1] "8 2"
[1] "8 3"
[1] "8 4"
[1] "8 5"
[1] "8 6"
[1] "8 7"
[1] "8 8"
[1] "9 1"
[1] "9 2"
[1] "9 3"
[1] "9 4"
[1] "9 5"
[1] "9 6"
[1] "9 7"
[1] "9 8"
[1] "9 9"
[1] "10 1"
[1] "10 2"
[1] "10 3"
[1] "10 4"
[1] "10 5"
[1] "10 6"
[1] "10 7"
[1] "10 8"
[1] "10 9"
[1] "10 10"