While Loop in R with Examples

The while loop in R is used to execute a set of statements in a loop as long as a condition is TRUE. It is a control statement.

while loop is a basic statement that exists in any programming language when you want to repeat a task as long as the condition is TRUE. There are three types of loops in R Programming, the for loop, the while loop and the repeat loop. In this article, you will learn all different things about while loop with several examples.

1. Syntax of while Loop

Following is the syntax of the while control statement.


# while statement syntax
while ( condition ) 
{
  # one or more statements
  # to execute
}

2. Flow of while Loop

The below chart demonstrates the flow of a while loop. Here, the condition is tested at the start of the while execution, when the condition is TRUE, the control enters into the while block to execute the statement and again goes back to the condition to check, it continues this process iteratively as long as the condition is TUE. when condition FALSE, it exits the while loop.

r while loop

3. while Loop in R Example

Let’s learn the while loop concept by going through a very simple example. The following example executes the while block as long as the condition i<=n becomes false. Here, first, n is assigned a value 5 and i is assigned a value 1. within the loop we increment i value by 1 and the loop executes until i reaches n.


# while example
i <- 1
n <- 5
while (i <= n) {
  print(i)
  i = i + 1
}

# Output
#[1] 1
#[1] 2
#[1] 3
#[1] 4
#[1] 5

4. while Break & Next Statements

The break and next statements are Jump statements in R that are used to interrupt the looping statements.

The break is used within the scope of the while loop to stop and exit the iteration without waiting for the condition to become false. Below is an example.

The next is similar to continue from other programming languages that is used to skip the rest of the block and jump to the condition to process the next iteration. Below is an example.


# Using break & next
i <- 1
n <- 5
while (i <= n) {
  if(i == 4){
    break
  }
  if(i == 3){
    i = i + 1
    next
  }
  print(i)
  i = i + 1
}

5. Nested while Loop

A while loop within another while loop is called a nested loop. When you used jump statements like break or next in an inside loop, it just interrupts the inner loop, not the outer loop.


# nested while loop
i=1
while (i <= 4) {
  j = 1
  while(j < 2) {
    print(i + j)
    j = j + 1
  }
  i = i + 1
}
# Output
# [1] 3
# [1] 4
# [1] 5

5. Conclusion

In this article, you have learned the syntax of the while loop in R and how to use it to execute the block of code until the condition becomes false. while Loop is a basic statement that exists in any programming language when you want to repeat a task a defined number of times. There are three types of loops in R the for loop, the while loop, and the repeat loop.

Related Articles

References

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing While Loop in R with Examples