Updated July 5, 2023
Introduction to While Loop in R
A while loop concept in R programming, which has its syntax and starts with the keyword “while,” much like in most other programming languages, has a block structure inside which statements to be executed are specified and which continue to execute, operating with requisite variables or constants are set by the user, based on the context, until a certain condition is met, after which control of execution passes out of the block that acts a loop that has a conditional end.
Control Flow
While Loop in R
Below is an example of using While loop statements.
Let’s say we are unsure how many times we must repeat an action or expression to be executed. In such cases, we use the While statement with the logical condition.
With FOR loop, we use curly brackets to wrap the expressions. If it is a single expression, curly brackets aren’t required.
Control Flow
Syntax:
While(condition)
expression
while(condition){
expression 1
expression 2
…
}
Example
a <- 1
b <- 2
while (b > 1){
c <- a + b
b <- 0
print(c)
}
Output:
[1] 3Steps
- We have initialized a to 1 and b to 2 in the above example.
- In the while statement: We have a condition to check if b is greater than 1.
- We then enter the loop as the condition (b>1) is true.
- We add both a and b and store them in the resultant variable C.
- Print c.
Infinite Loop Sequence
While using the while statement, we must be cautious in defining the condition/statements. Otherwise, we may end up in an infinite loop.
Example
while (b > 1)
{
c <- a + b
b <- 0
print(c)
}
Removing the statement (B <- 0) from the program will lead to an infinite loop because b is defined as 2 at the start and never changes its value through the program. Unless we change its value in the loop. (b <- 0) .This allows the program to print C only once and not infinite times.
Remember, all the statements in FOR and WHILE Loop are executed sequentially.
Loop Control Statements
There are two different types of loop control statements in R.
- Break
- Next
Break statement
The function of the break statement is to bring the execution out of the loop and execute the statements outside the loop, if any.
Syntax:
While (condition)
{
Expression 1
Break
}
Example
a <- 1
b <- 2
while (b > 1)
{
c <- a + b
b <- 0
print(c)
break
}
print(b)
Output:
[1] 3 [1] 0Steps
- Using the same while example program, we have added a break statement after print statements.
- We have used the break to come out of the loop and print b.
Next Statement
We use the NEXT statement to skip a statement in the loop.
Syntax:
While (condition)
{
Expression 1
next
skip statement
}
Example
a <- 1
b <- 2
while (b > 1){
c <- a + b
b <- 0
next
print(c)
}
print(b)
Output:
[1] 0Steps
- We used the NEXT statement after b <- 0
- NEXT statement skips the statement (print(c)) and prints b.
Recommended Articles
This is a guide to While Loop In R. Here, we discuss the introduction to While Loop In R and different types of loops in R, along with some examples and steps. You may also have a look at the following articles to learn more –