Explain While loops in R?

It is a type of control statement which will run a statement or a set of statements repeatedly unless the given condition becomes false. It is also an entry-controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false.

Syntax:

while ( condition ) 
{
  statement
}

Example: Program to display numbers from 1 to 5 using while loop in R.

# R program to demonstrate the use of while loop
 
val = 1
 
# using while loop
while (val <= 5)
{
    # statements
    print(val)
    val = val + 1
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5