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 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 while loop in R programming may execute zero or more times, and the syntax of it is:

## R While Loop Syntax

The syntax of the 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 While loop. If the expression result is True, then the statement or group of statements under the while loop block will execute. If the expression return is False, then it will come out of the loop and execute other statements outside the while block.

## Flow Chart of a While loop

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

While loop will check for the expression at the beginning.

- If the expression inside the While is True, then it executes the statements inside the loop.
- Next, we have to increment or decrement the value inside the while loop.
- 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.
- If the condition is False, then it will exit from the While loop.

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

## While Loop in R Programming example

This program for a 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 are going to enter the number = 6. It means, total = 6 + 7 + 8 + 9 + 10 = 40

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 }

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 increment by 1 (number= number + 1). Please refer R Arithmetic Operators article to understand this + notation.

Second Iteration

Within the first Iteration of 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 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 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, then 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)) }

Here, 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. Now, let us add + 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 }

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