R – Next Statement
Next statement in R is used to skip any remaining statements in the loop and continue the execution of the program. In other words, it is a statement that skips the current iteration without loop termination.
‘next’ is a loop control statement just like the break statement. But ‘next’ statement works opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
Next Statement in R Programming Language Syntax
next
Next Statement in R Programming Flowchart
Next Statement in R Language Example
Example 1: Using next in the for loop
R
# R program to illustrate next in for loop val <- 6:11 # Loop for (i in val) { if (i == 8) { # test expression next } print (i) } |
Output:
[1] 6 [1] 7 [1] 9 [1] 10 [1] 11
The next statement can be used with any other loop also like ‘while’ or ‘repeat’ loop in a similar way as it is used with for loop above.
Example 2: Using next in the while loop
R
# R program to illustrate next in while loop val <- 6 i = 11 # Loop while (i>val) { if (i == 8) { # test expression next } print (i) i = i - 1 } |
Output:
[1] 11 [1] 10 [1] 9
Example 3: Using next in the repeat loop
R
# R program to illustrate next in repeat loop i = 0 # Loop repeat { if (i == 10) break if (i == 5) { next } print (i) i = i + 1 } |
Output:
[1] 0 [1] 1 [1] 2 [1] 3 [1] 4
Please Login to comment...