While Loop in R - r - learn r - r programming
- In R programming, while loops are used to loop until a specific condition is satisfied.
- These is similar to for loop, but the iterations are controlled by a conditional statement.
Syntax:
Syntax Explanation
- test_expression is evaluated and the body of the loop is entered if the condition is TRUE. This process is repeated until test_expression evaluates to FALSE, in which case, the loop exits.
Sample Code:
Code Explanation
- x is specifying to string variable. Here x is initialized to value as 5.
- The test_expression is x < 10 which evaluates to TRUE since 5 is less than 10.
- In the next iteration, the value of incrementing x is 5 and the loop continues. This will continue until x takes the value 10. The condition 10 < 10 will give FALSE, and then loop is exits.
- Here print() is used to print the value, that value stored in x variable.
Output
- Here in this output, the while loop will continue to run as long as x=5 is less than (x < 10) which will increase by 1 each time the loop runs (x+1) till 10.