while loop
While Loop in R Programming
A While loop in R programming is a statement that keeps running until a condition after while block is satisfied.
While loop in R programming language is used when the exact number of iterations of loop is not known beforehand. It executes the same code again and again until a stop condition is met. While loop checks for the condition to be true or false n+1 times rather than n times. This is because the while loop checks for the condition before entering the body of the loop.
R- While loop Syntax:
while (test_expression) {
statement
update_expression
} 
How does a While loop execute?
- Control falls into the while loop.
- The flow jumps to Condition
- Condition is tested.
- If Condition yields true, the flow goes into the Body.
- If Condition yields false, the flow goes outside the loop
- The statements inside the body of the loop get executed.
- Updation takes place.
- Control flows back to Step 2.
- The while loop has ended and the flow has gone outside.
Important Points about while loop in R language:
- It seems to be that while loop will run forever but it is not true, condition is provided to stop it.
- When the condition is tested and the result is false then loop is terminated.
- And when the tested result is True, then loop will continue its execution.
R – while loop Flowchart:

While Loop in R Programming Examples
Example 1:
R
# R program to illustrate while loopresult <- c("Hello World")i <- 1# test expressionwhile (i < 6) { print(result) # update expression i = i + 1} |
Output:
[1] "Hello World" [1] "Hello World" [1] "Hello World" [1] "Hello World" [1] "Hello World"
Example 2:
R
# R program to illustrate while loopresult <- 1i <- 1# test expressionwhile (i < 6) { print(result) # update expression i = i + 1 result = result + 1} |
Output:
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5
R – while loop break
Here we will use the break statement in the R programming language. Break statement in R is used to bring the control out of the loop when some external
condition is triggered.
R
# R program to illustrate while loopresult <- c("Hello World")i <- 1# test expressionwhile (i < 6) { print(result) if( i == 3){ break} # update expression i = i + 1} |
Output:
[1] "Hello World" [1] "Hello World" [1] "Hello World
While Loop Syntax in R
Following is the syntax for While Loop in R programming:
while (condition) {
Exp
}
R While Loop Flowchart

Note: Remember to write a closing condition at some point otherwise the loop will go on indefinitely.
While Loop in R Programming Examples
Example 1:
Let’s go through a very simple example to understand the concept of while loop. You will create a loop and after each run add 1 to the stored variable. You need to close the loop, therefore we explicitely tells R to stop looping when the variable reached 10.
Note: If you want to see current loop value, you need to wrap the variable inside the function print().
#Create a variable with value 1
begin <- 1
#Create the loop
while (begin <= 10){
#See which we are
cat('This is loop number',begin)
#add 1 to the variable begin after each loop
begin <- begin+1
print(begin)
}Output:
## This is loop number 1[1] 2 ## This is loop number 2[1] 3 ## This is loop number 3[1] 4 ## This is loop number 4[1] 5 ## This is loop number 5[1] 6 ## This is loop number 6[1] 7 ## This is loop number 7[1] 8 ## This is loop number 8[1] 9 ## This is loop number 9[1] 10 ## This is loop number 10[1] 11Example 2:
You bought a stock at price of 50 dollars. If the price goes below 45, we want to short it. Otherwise, we keep it in our portfolio. The price can fluctuate between -10 to +10 around 50 after each loop. You can write the code as follow:
set.seed(123) # Set variable stock and price stock <- 50 price <- 50 # Loop variable counts the number of loops loop <- 1 # Set the while statement while (price > 45){ # Create a random price between 40 and 60 price <- stock + sample(-10:10, 1) # Count the number of loop loop = loop +1 # Print the number of loop print(loop) }Output:
## [1] 2 ## [1] 3 ## [1] 4 ## [1] 5 ## [1] 6 ## [1] 7cat('it took',loop,'loop before we short theprice. The lowest price is',price)Output:
## it took 7 loop before we short the price.The lowest price is 402. Example of while Loopi <- 1 while (i < 6) { print(i) i = i+1 }Output
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5In the above example,
iis initially initialized to 1.Here, the
test_expressionisi < 6which evaluates toTRUEsince 1 is less than 6. So, the body of the loop is entered andiis printed and incremented.Incrementing
iis important as this will eventually meet the exit condition. Failing to do so will result into an infinite loop.In the next iteration, the value of
iis 2 and the loop continues.This will continue until
itakes the value 6. The condition6 < 6will giveFALSEand the while loop finally exits.
Comments
Post a Comment