- Relational Operators in R
- Logical Operators in R
- Conditional Statements in R
- For Loop in R Programming
- While and Repeat Loop in R Programming
- Functions in R Programming
- Creating Functions in R
- Apply Functions in R
- Importing Data from External Data Sources in R
- Importing Data Using read.csv in R
- Import Data using read.table in R
- Importing Data Using data.table – fread in R
- Importing Data from Excel in R
- Using XLConnect in R Programming
- Importing Data from a Database in R
- SQL Queries from R
- Importing Data from Web in R
While and Repeat Loop in R Programming
The general format of the while statement is:
while (condition) {
statement
}
Note that a while loop may never execute the statement. The statement is executed repeatedly until condition becomes false.
In contrast, a repeat loop has the general format as below:
repeat {
print('test')
}
repeat
statement will execute at least once, and continue until it is explicitly interrupted with a break statement. In fact, break will immediately exit from any loop structure.
Unlike for
loop which iterates over a vector list, for using while
loop you need to have an indicator variable i
and change its value within each iteration. Otherwise you will have an infinite loop.
Example: While Loop
The following example shows how to calculate factorial of 10 using the while loop.
This content is for paid members only.
Join our membership for lifelong unlimited access to all our data science learning content and resources.