Break and Next in R


April 8, 2022, Learn eTutorial
1575

We already discussed the basics of conditional statements and loop 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.

Break Flowchart

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.

Next Flowchart

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"