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. R While loop starts with the expression, and if the expression is True, then statements inside it will be executed.

If the specified expression is false, it won’t be executed at least once. It means that while loop in R programming may execute zero or more times, the syntax of it is:

R While Loop Syntax

The syntax of the R While loop is

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

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

R While loop Flow Chart

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

WHILE LOOP FLOW CHART

The R While loop will check for the expression at the beginning.

  1. If the expression inside the While is True, it executes the statements inside the loop.
  2. Next, we must increment or decrement the value inside the R while loop.
  3. After the value increments, again, it will check for the expression. The statements inside the while loop are executed as long as the condition is True.
  4. If the condition is False, it will exit from the iteration.

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

While Loop in R Programming example

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

# Example

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

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

print(paste("The total Sum of Numbers is:  ", total))

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

While Loop in R Programming 1

Within the following statements of this 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 Value below 10:  "))

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

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

Analysis

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

First Iteration

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

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

Second Iteration

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

total = 6 + 7 ==> 13

Next, the number will increment 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 = 13 + 8 ==> 21

Next, the number incremented by 1.

Fourth Iteration

Within the third Iteration of R While loop, the values of number = 9 and total = 21

total = 21 + 9 ==> 30

Next, the number incremented by 1

Fifth Iteration

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

total = 30 + 10 ==> 40

Next, the number incremented by 1

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

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

print(paste("The total Sum of Numbers is:  ", total))

Infinite While Loop in R Programming

If you forget to increment or decrement the value inside the while loop, the code executes infinite times, also called an infinite loop.

# Infinite While Loop Example

number <- 1

while (number < 10)  {
  print(paste("Value from it is:  ", number))
}
Infinite While 2

In this R while loop example, the number will always be 1, and the number is always less than 10, so it executes infinite times. Let us add the + operator (number= number + 1) inside the while loop to the above example.

# Infinite While Loop Example

number <- 1

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

When it reaches 10, the condition (number < 10) will fail. Let us see the output of the example.

Infinite While Loop in R Programming 3