While loop in R Programming

Filed Under: R Programming

In addition to the for loop we discussed earlier, R also offers another kind of loop to perform iterative programming, namely the while loop.

In many scenarios, we don’t know exactly how many times we want our loop to run. In such cases, while is the most useful programming construct. A while loop runs as long as the condition we supply initially holds true.

Structure of while loop in R

while(anycondition){
#Do something
}

It is common for a while loop to keep making changes to the condition variable until a certain point. For example, we might want the loop to run while a variable value if TRUE.

When a certain condition gets met inside the while loop, it can change the variable value to FALSE and terminate the loop. This terminating condition should be handled with care so as to not run into infinite loops. We will see some examples shortly.

Simple while loop Example

a=TRUE
while(a==TRUE){
  print("Entering loop")
  print("A is true")
  a=FALSE
}
  • This simple piece of code first sets the value of a Boolean variable a to TRUE.
  • The while loop checks if the value of a is TRUE. Since it is true, it enters the loop and prints “A is true” first.
  • Then a gets assigned to FALSE.
  • In the next loop run, the while loop checks the a value, which is not FALSE and therefore exits the loop right away.
  • Let us look at the output.

Output:

[1] "Entering Loop"
[1] "A is true"

The loop sentinel statement here gets printed only once, as the loop doesn’t get to run for the second time for a value FALSE.

Using break statement in the while loop

Since while loops have a possibility of running into infinite loops and exhausting system memory, it is ideal to use break in several cases.

Let us look at an example.

The most common computing application of loops is the generation of sequences. Suppose you wish to print the Fibonacci sequence until a particular limit, say till the number is less than or equal to 300. You surely don’t know how many iterations it will take to reach 300.

The following is a naive way of displaying the Fibonacci sequence using a while loop.

f1=0
f2=1
sum=0
while(TRUE){
  sum <- f1+f2
  f1 <- f2
  f2 <- sum
  print(sum)
}

Chances are that you will only see exponential and Inf numbers on your console, which continue printing till you press the STOP button on the top left of the console.

The problem here lies with the while(TRUE) part of the loop. We are specifying the compiler to run the loop forever and generate all the Fibonacci numbers. Therefore, we need to handle this loop by placing our condition inside the loop. Let us see a modified piece of code.

f1=0
f2=1
sum=0
while(TRUE){
  sum <- f1+f2
  f1 <- f2
  f2 <- sum
  if(sum>300){
    break
  }
  print(sum)
  
}

We have now handled the condition of the sum being greater than 300 using an if statement block above. Once the condition is met, we simply break the loop and come out of it.

Let us see the output now:

[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 13
[1] 21
[1] 34
[1] 55
[1] 89
[1] 144
[1] 233

The next number in the sequence will be 377, which is greater than 300. But we have already checked for the sum being greater than 300 right before we print the sum and broke the loop because it is. Therefore we only get the sequence till 233.

Now try displaying the sum value. Interestingly, it is 377 and not 233.

> sum
[1] 377

The sum value 377 is calculated and stored in the sum, but not printed since we broke out of the loop.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages