Next statement
Most of the languages provide functionality using which we can skip the current iteration at the moment and go for the next iteration if it exists. For this purpose, we have the “next” statement in R.
Example 1: In the below program we are skipping the inner for-loop if the value of number1 is equal to one.
R
# R program to illustrate the working # of next statement in nested for-loop # Iterating over outer loop for (number1 in 1:5) { if (number1 == 1) next # Iterating over outer loop for (number2 in 1:5) { # Print the sum of number1 and number2 print ( paste (number1, "+" , number2, "=" , number1 + number2)); } } |
Output:
Example 2: In the below program we are skipping the print statement if the value of number2 is equal to one in the inner for-loop.
R
# R program to illustrate the working # of next statement in nested for-loop # Iterating over outer loop for (number1 in 1:5) { # Iterating over outer loop for (number2 in 1:5) { # If number1 is equal to 1 # Then skip the print statement below # and move to the next iteration if (number2 == 1) next # Print the sum of number1 and number2 print ( paste (number1, "+" , number2, "=" , number1 + number2)); } } |
Output:
How to Create a Nested For Loop in R?
A loop in a programming language is a sequence of instructions executed one after the other unless a final condition is met. Using loops is quite frequent in a program.
Contact Us