R
Loops
Sometimes, we may need to execute the same block of code multiple times. Normally, statements are executed sequentially: the first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
Loop statements allow us to execute a statement or group of statements multiple times. Below is the flowchart for loop statements in most programming languages:
R provides the following types of loops:
- repeat loop
- while loop
- for loop
R provides the following loop control statements:
- break statement
- next statement
Loop control statements change the execution order of your code. They can be used to jump to a specific part of the code.
Loop Types
repeat
The repeat loop will execute the code repeatedly until a condition is met to exit the loop, which requires the use of the break statement.
The syntax is as follows:
repeat {
// relevant code
if(condition) {
break
}
}
The following example exits the loop when the variable cnt
is 5, where cnt
is a counter variable:
Example
v <- c("Google","tutorialpro")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
Executing the above code will produce the following output:
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
while
The while loop in R will repeatedly execute a target statement as long as the given condition is true.
The syntax is as follows:
while(condition)
{
statement(s);
}
Here, statement(s)
can be a single statement or several statements within a block.
condition
can be any expression, and true is any nonzero value. The loop executes while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop.
The following example outputs the content within the while block as long as the variable cnt
is less than 7, where cnt
is a counter variable:
Example
v <- c("Google","tutorialpro")
cnt <- 2
while (cnt < 7) {
print(v)
cnt = cnt + 1
}
Executing the above code will produce the following output:
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
for
The for loop in R can repeatedly execute a specified statement, with the number of repetitions controlled within the for statement.
The syntax is as follows:
for (value in vector) {
statements
}
R's for loop is particularly flexible and can iterate over integer variables as well as character vectors, logical vectors, lists, and other data types.
The following example outputs the first four letters of the alphabet:
Example
v <- LETTERS[1:4]
for ( i in v) {
print(i)
}
Executing the above code will produce the following output:
[1] "A"
[1] "B"
[1] "C"
[1] "D"
Loop Control
break
The break statement in R is inserted within the loop body and is used to exit the current loop or statement, proceeding to the statement immediately following.
If you use nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code in the outer loop.
break is also commonly used in switch statements.
The syntax is as follows:
break
The following example uses break to exit the loop when the variable cnt
is 5, where cnt
is a counter variable:
Example
v <- c("Google","tutorialpro")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
Executing the above code will produce the following output:
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
[1] "Google" "tutorialpro"
next
The next statement is used to skip the current iteration of the loop and proceed to the next iteration (similar to 'continue' in other languages).
The syntax is as follows:
next
The following example outputs the first six letters of the 26 alphabets, skipping the current loop when the letter is D and proceeding to the next loop:
Example
v <- LETTERS[1:6]
for (i in v) {
if (i == "D") { # D will not be output, skip this loop and proceed to the next
next
}
print(i)
}
Executing the above code, the output result is:
[1] "A"
[1] "B"
[1] "C"
[1] "E"
[1] "F"