R while Loop

In this tutorial, you will learn about while loops in R with the help of examples.

In programming, loops are used to repeat a block of code as long as the specified condition is satisfied. Loops help you to save time, avoid repeatable blocks of code, and write cleaner code.

In R, there are three types of loops:


R while Loop

while loops are used when you don't know the exact number of times a block of code is to be repeated. The basic syntax of while loop in R is:

while (test_expression) {
  # block of code
}
  • Here, the test_expression is first evaluated.
  • If the result is TRUE, then the block of code inside the while loop gets executed.
  • Once the execution is completed, the test_expression is evaluated again and the same process is repeated until the test_expression evaluates to FALSE.
  • The while loop will terminate when the boolean expression returns FALSE.

Example 1: R While Loop

Let's look at a program to calculate the sum of the first ten natural numbers.

# variable to store current number
number = 1

# variable to store current sum
sum = 0

# while loop to calculate sum
while(number <= 10) { # calculate sum sum = sum + number # increment number by 1 number = number + 1 }
print(sum)

Output

[1] 55

Here, we have declared two variables: number and sum. The test_condition inside the while statement is number <= 10.

This means that the while loop will continue to execute and calculate the sum as long as the value of number is less than or equal to 10.


Example 2: while Loop With break Statement

The break statement in R can be used to stop the execution of a while loop even when the test expression is TRUE. For example,

number = 1

# while loop to print numbers from 1 to 5
while(number <= 10) {
print(number) # increment number by 1 number = number + 1 # break if number is 6
if (number == 6) { break }
}

Output

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

In this program, we have used a break statement inside the while loop, which breaks the loop as soon as the condition inside the if statement is evaluated to TRUE.

if (number == 6) {
  break
}

Hence, the loop terminates when the number variable equals to 6. Therefore, only the numbers 1 to 5 are printed.


Example 3: while Loop With next Statement

You can use the next statement in a while loop to skip an iteration even if the test condition is TRUE. For example,

number = 1

# while loop to print odd number between 1 to 10
while(number <= 10) { # skip iteration if number is even if (number %% 2 == 0) { number = number + 1 next }
# print number if odd print(number) # increment number by 1 number = number + 1 }

Output

[1] 1
[1] 3
[1] 5
[1] 7
[1] 9

This program only prints the odd numbers in the range of 1 to 10. To do this, we have used an if statement inside the while loop to check if number is divisible by 2.

Here,

  • if number is divisible by 2, then its value is simply incremented by 1 and the iteration is skipped using the next statement.
  • if number is not divisible by 2, then the variable is printed and its value is incremented by 1.
Did you find this article helpful?