R while loop

R While loop executes a set of statements repeatedly in a loop as long as the condition is satisfied. We shall learn about the syntax, execution flow of while loop with R example scripts.

Syntax

while (expression) {
    statements
}

Execution Flow

R While Loop

As long as the boolean expression evaluates to TRUE, the statements inside the while block are executed. The point at which the boolean expression results FALSE, the execution flow is out of the while loop statement.

Example

 

Break statement inside while loop

Not only the boolean expression next to while keyword could stop the while loop, but also a break statement kept intentionally inside the while loop. An example is provided below :

The inclusion of break statement might look similar to R Repeat Loop Statement.

 

Conclusion :

In this R Tutorial, we have learnt about the syntax and execution flow of while loop with R example scripts.