Break & Next in R programming
We already discussed the basics of conditional statements and statements in the previous tutorials. In this tutorial, we focus on learning loop control statements in the R programming language. Let us move forward with each type of loop control statement.
Break statement in R
In R programming language break
is a reserved word to represent one of the basic control flow structures.
The break statement in a loop allows the sudden termination from the current iteration which interrupts the further execution of the iteration. The control gets transferred to immediate instruction after the loop statement to resume the program execution.
- In the case of nested for loops, the break statement exit from the inner loop by transferring control to the outer loop.
- Break statements are used inside if..else statement, in the switch statement to terminate the execution
Syntax
break
Flow chart to depict the execution of a program when it encounters with a break statement.

Consider the program to iterate over a block of codes given in for loop.A vector v is created using c()
function which holds a list of elements such as "Learn","eTutorials","for","R","Program”.
When the R code starts execution the iterator variable i stores the elements of the input vector v, checks the condition whether iterator variable i is equal to 3 given as an if statement, when the condition meets wrong the element in i gets displayed. On the other hand when the if statement satisfies it exits from iterating
Iterator variable i | If statement | OUTPUT v[i] |
---|---|---|
i=1 | i==3 FALSE | V[1] = "Learn" |
i=2 | i==3 FALSE | V[2] = "eTutorials" |
i=3 | i==3 TRUE break | Exit from execution |
over the loop when a break statement is followed inside if statement. And jump to the next proceeding instruction in the program. In our given program when the iterator variable i becomes equal to 3(i==3), the break statement is provided to exit from the for a loop.
#break statement in R
#vector v with 5 character values
v < -c("Learn", "eTutorials", "for", "R", "Program")
for (i in 1: 5) {
if (i == 3) {
break
}
print(v[i])
}
The output generated by the above R source code shows that the iteration exit by interrupting the further execution of the sequence in the for-loop when the if control structure is satisfied and is followed by a break
statement.
[1] "Learn" [1] "eTutorials"
Next statement in R
In R programming language the next statement allows continuing the execution of a program by skipping any of the iterations inside a for loop or while loop without affecting the remaining iteration.
When the next
a reserved word is evaluated the current executing iteration immediately halts and switches to resume from the next immediate iteration or statement.
Syntax
next
The flow chart for the next
control statement helps to understand the concept easily.

Example
Consider the same example we discussed for break
statement. Here the only difference you can see in the code is instead of break
statement another type of control structure (next statement) is used to control the flow of the R program.
During the for loop iteration, it checks the iterator variable or any given condition is satisfied or not. When a certain condition is satisfied inside the if statement, then the codes inside the if statement begins executing.
If a next statement is determined the iterator variable skips executing that sequence and continues with the next sequence in the for loop iteration.
In the given example when i becomes equal to 3(i==3), the condition is satisfied and executes the next statement by moving to next iteration ie i becomes 4 (i=4) by skipping the further action when i was equal to 3.
The table summarizes the working of for loop when there is the next statement.
Iterator variable i | If statement | OUTPUT v[i] |
---|---|---|
i=1 | i==3 FALSE | V[1] = "Learn" |
i=2 | i==3 FALSE | V[2] = "eTutorials" |
i=3 | i==3 TRUE next | Moves to next iteration |
i=4 | i==3 FALSE | V[4] = "R" |
i=5 | i==3 FALSE | V[5] = "Program") |
#next statement in R
#vector v with 5 character values
v < -c("Learn", "eTutorials", "for", "R", "Program")
for (i in 1: 5) {
if (i == 3) {
next
}
print(v[i])
}
The output generated is
[1] "Learn" [1] "eTutorials" [1] "R" [1] "Program"S
Example 1: break statement in for loop
for ( x in 1:10) {
if ( x == 5) {
break
}
print (x)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
In this example we have created a for loop which iterates from 1 to 10. However the condition is if the value is 5 then break or terminate loop. After if condition with break there is a single statement of print. When this program is run it prints from 1 to 4 and when the value is 5, the condition is True and the break statement is executed which moves the flow control out of this loop and exits the program as there is no more code to execute.
Example 2: break statement in while loop
a <- 1
while( a < 7){
print(a)
a = a + 1
if ( a > 3) {
break
}
}
Output:
[1] 1
[1] 2
[1] 3
Here a while loop initializes with the variable a as 1. Even when the condition of while loop is True the loop stops or terminates when the value of a is more than three. Hence it prints only 1 2 and 3.
Example 3: break statement in if else structure
for ( i in 1:11) {
if ( i < 5) {
print (i)
} else {
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
break can also be used in else part of if else structure. For loop condition is iteration from 1 to 11. If condition is print all values if they are less than 5 and the else structure is if value is not less than five then break out of the loop.
next statement
next statement is used to skip the current iteration of the loop without terminating it and starts the next iteration of the loop. When R interpreter finds the next statement it stops further evaluation of current iteration of loop and starts next iteration.
Syntax
The syntax of next statement in R is very easy as we use a condition and put next in its block of code in case if condition is true next is executed.
...
if (condtion) {
next
}
...



Comments
Post a Comment