The While loop in R Programming is used to repeat a block of statements for given number of times, until the specified expression is False. While loop start 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 basic syntax of the While loop in R Programming language is as shown below:
While ( Expression ) { statement 1 statement 2 …………. statement N; # Increment or Decrements the Values } #This statement is from Outside the While Loop
First compiler 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 compiler will come out of the loop and execute other statements outside the while loop.
Flow Chart of a While loop in R
Following picture will show you the flow chart behind the R While Loop
While loop in R will check for the expression at the beginning of the loop.
- If the expression inside the While is True then it will execute the statements inside the loop.
- Next we have to increment, or decrements the value inside the R while loop.
- After the value incremented, again it will check for the expression. As long as the condition is True, the statements inside the while loop will be executed.
- If the condition is False then it will exit from the R While loop
Let us see the example of While loop in R programming for better understanding
While Loop in R Programming example
This programmer for while loop in r allows the user to enter an integer value. Using this value, compiler will add those values up to 10.
R CODE
# 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: We are going to enter number = 6. It means, total = 6 + 7 + 8 + 9 + 10 = 40
ANALYSIS
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 number variable
total = 0 number <- as.integer(readline(prompt="Please Enter any integer Value below 10: "))
Next line, we used the R 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 example for while loop in r, User Entered value: number = 6 and we initialized the total = 0
First Iteration
total = total + number
total = 0 + 6 ==> 6
Next, 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 has been changed as: number = 7 and total = 6
total = total + number
total = 6 + 7 ==> 13
Next, number will be incremented by 1.
Third Iteration
Within the second Iteration the values of both number and total has been changed as: number = 8 and total = 13
total = total + number
total = 13 + 8 ==> 21
Next, number will be incremented by 1.
Fourth Iteration
Within the third Iteration the values of both number and total has been changed as: number = 9 and total = 21
total = total + number
total = 21 + 9 ==> 30
Next, number will be incremented by 1
Fifth Iteration
Within the fourth Iteration the values of both number and total has been changed as: number = 10 and total = 30
total = total + number
total = 30 + 10 ==> 40
Next, number will be incremented by 1
Here Number = 11 so, the condition present inside the R while loop (number <= 10) will fail
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, R while loop will execute infinite times (also called as infinite while loop).
R CODE
# Infinite While Loop in R Programming Example number <- 1 while (number < 10) { print(paste("Number from the While Loop is: ", number)) }
OUTPUT
ANALYSIS
Here, in this R while loop example, number will always be 1 and number is always less than 10 so while loop will go on execute infinite times. Now, let us add + operator (number = number + 1) inside the while loop to the above example.
R CODE
# 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 R while loop example
OUTPUT
Thank You for Visiting Our Blog