While Loop in R

The While loop in R Programming is used to repeat a block of statements for a given number of times until the specified expression is False. While loop in R starts with the expression, and if the expression is True, then statements inside the while loop will be executed. If the specified expression is false, it won’t be executed at least once. It means, R while loop may execute zero or more time and the syntax of while loop is:

R While Loop Syntax

The syntax of the While loop in R Programming language is

While ( Expression )  {
    statement 1
    statement 2
    ………….
    statement N;
    # Increment or Decrements the Values
}
#This statement is from Outside the While Loop

First, it will check for the expression inside the While loop. If the expression result is True, then the statement or group of statements under the while loop block will be executed. If the expression return is False, then it will come out of the loop and execute other statements outside the while loop.

Flow Chart of a While loop in R

The following picture will show you the flow chart behind the R While Loop

FLOW CHART of While Loop in R Programming

While loop in R will check for the expression at the beginning of the loop.

  1. If the expression inside the While is True, then it executes the statements inside the loop.
  2. Next, we have to increment or decrements the value inside the R while loop.
  3. After the value incremented, again, it will check for the expression. As long as the condition is True, the statements inside the while loop executed.
  4. If the condition is False, then it will exit from the R While loop.

Let us see the example of a While loop in R programming for better understanding.

While Loop in R Programming example

This program for a while loop in r allows the user to enter an integer value. Using this value, it adds those values up to 10.

# R While Loop Example

total = 0
number <- as.integer(readline(prompt="Please Enter any integer Value below 10:  "))

while (number <= 10)  {
  total = total + number
  number = number + 1
}

print(paste("The total Sum of Numbers From the While Loop is:  ", total))

OUTPUT: For this R Programming example, we are going to enter number = 6. It means, total = 6 + 7 + 8 + 9 + 10 = 40

While Loop in R Programming 1

Within the following statements of this r while loop, First, we declared the total variable and assigned it to Zero. Next, it will ask the user to enter any integer value below 10, and we are assigning the user entered value to a number variable.

total = 0
number <- as.integer(readline(prompt="Please Enter any integer Value below 10:  "))

Next line, we used the While loop, and the expression inside the While loop will make sure that the given number is less than or equal to 10.

while (number <= 10)  {
  total = total + number
  number = number + 1
}

In this while loop in R example, the user Entered value: number = 6, and we initialized the total = 0.

First Iteration

total = total + number
total = 0 + 6 ==> 6

Next, the number will be incremented by 1 (number = number + 1). Please refer R Arithmetic Operators article to understand this + notation.

Second Iteration

Within the first Iteration of r while loop, the values of both number and total changed as number = 7 and total = 6

total = total + number
total = 6 + 7 ==> 13

Next, the number will be incremented by 1.

Third Iteration

Within the second Iteration, the values of both number and total have been changed as number = 8 and total = 13

total = total + number
total = 13 + 8 ==> 21

Next, the number incremented by 1.

Fourth Iteration

Within the third Iteration, the values of both number and total changed as number = 9 and total = 21

total = total + number
total = 21 + 9 ==> 30

Next, the number incremented by 1

Fifth Iteration

Within the fourth Iteration, the values of both number and total changed as number = 10 and total = 30

total = total + number
total = 30 + 10 ==> 40

Next, the number incremented by 1

Here Number = 11 so, the condition present inside the while loop (number <= 10) will fail

The last print statement will print the total sum of digits present in the given number as output.

print(paste("The total Sum of Numbers From the While Loop is:  ", total))

Infinite While Loop in R Programming

If you forgot to increment or decrements the value inside the while loop, then the while loop executes infinite times (also called as infinite while loop).

# Infinite While Loop in R Programming Example

number <- 1

while (number < 10)  {
  print(paste("Number from the While Loop is:  ", number))
}
While Loop in R Programming 2

Here, in this R while loop example, the number will always be 1, and the number is always less than 10. So, while loop executes infinite times. Now, let us add + operator (number = number + 1) inside the while loop to the above example.

# Infinite While Loop in R Programming Example

number <- 1

while (number < 10)  {
  print(paste("Number from the While Loop is:  ", number))
  number = number + 1
}

Now, when it reaches 10, the condition (number < 10) will fail. Let us see the output of while loop example

While Loop in R Programming 3