Learn R Online
 Home            

R While Loop

In this tutorial you will learn how to create a while loop in R programming. A Loop is an iterative structure used for repeating a specific block of code given number of times or when a certain condition is met.

In while loop a condition or test expression is given. First the program control enters the loop and the test expression or condition is checked, if the condition evaluates to True the body of loop is executed and the condition is checked again. If the condition evaluates to False the control exits loop.

Syntax of while Loop

In R programming the syntax is 

while (condition) {

    block of code

    }

First is the keyword while which is used to start the loop after it there are parenthesis which contain a test condition which is to be evaluated for each run of this loop. The condition is a logical and hence results in TRUE or FALSE. If test expression is TRUE the block of code is executed, in case of a FALSE the flow control exits the loop. In case the condition is FALSE for the very first run of loop, the block of code is never going to be executed. The block of code may contain a single line, a single expression or as many lines of code as desired. In addition to assigning values, you may also call functions in body of loop. 

Flowchart

Here is flow control of this loop in R

Flowchart of R While Loop

When the condition is True, code is executed, when condition is False body of loop is skipped and control moves on to code immediately after loop.

Example of while Loop

> x  <-  10
> while ( x >= 5 ) {
     print (x)
     x  <-  x-1
    }

And the output will be

[1] 10
[1] 9
[1] 8
[1] 7
[1] 6
[1] 5

To understand the idea lets first see a simple example. In this example there is only one variable named x which is initialised to value 10. The while loop has test condition which states that value of x is greater than or equal to 5. With in the body of loop there are two expressions, first is print statement which is printing the value of x and second statement decrements 1 from x for each run of loop. In simple words, when the body of loop is executed every time one is subtracted from x and new value is assigned to x.

Now when the flow control enters while the condition is checked for the first time, x >=5 where current value of x is 10, hence the test expression evaluates to TRUE which means body of loop is to be executed. The value of x is printed on console and 1 is subtracted from x, and value of x is 9 now and the control moves on to test condition again. As value of x is 9, the condition results in TRUE and body is executed printing 9 and subtracting 1 from x. This will continue printing 10,9,8,7,6,5 until the condition is FALSE which will happen when the value of x will become less than 5, at this point the body of loop will be skipped and control will move out of loop to the next line of code immediately after while.

Example 2: Factorial with while Loop

num <- 6
factorial <- 1

while(num >=1 ){
factorial <- factorial * num
num <- num - 1
}
print(factorial)

[1] 720

In this example we are finding the factorial of a given number num. The factorial of any positive integer is 1 hence we assign 1 to variable factorial at the start. And for each run of loop we are decrementing 1 from the variable num until it is greater than or equal to 1. At the same time factorial is previous factorial multiplied by num.

With the help of this example the idea becomes clear. Factorial can also be found with recursion or for loop.

Example of infinite while loop

The test condition has to be set very carefully otherwise the while loop may run infinitely. See the example 

i <- 10
while (i >=1){
print(i)
}

In this example the output will be a continuous string of 10 without stopping. But you don't worry, you can stop it by pressing Escape key(Top left corner of keyboard). Here variable i is initialized to 10 and the test condition is that i is greater than or equal to 1. With in the body of while value of i is not changed hence it will be always 10 and the condition will remain TRUE forever, and the loop will run infinite number of times. 

Learn to Shine

  • Programming made Easy for all
  • High School and College
  • University level
  • Professionals doctors, engineers, scientists
  • Data Analysts