Introduction to While Loop in R
A while loop concept in R programming which has its own syntax and starts with the keyword “while”, much like in most other programming languages, having a block structure inside which statements to be executed are specified and which continue to execute, operating with requisite variables or constants are specified 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 not sure how many times we need to repeat an action or expression to be executed. In such cases, we make use of 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
- In the above example, We have initialized a to 1 and b to 2.
- 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 in resultant variable C.
- Print c.
Infinite Loop Sequence
While using the while statement, we need to 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)
}
If we remove the statement (B <- 0) from the program then, it 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 –
13 Online Courses | 20 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses