Next R - r - learn r - r programming



  • A next statement is useful when we want to skip the current iteration of a loop without terminating it.
  • On come across next, the R parser skips additional evaluation and starts next iteration of the loop.
 r Next Statement

Syntax

next

Sample Code

v <- 5:10
for (val in v) 
{
    if (val == 7)
{
        next
    }
    print(val)
}

Code Explanation

 r next statement structure
  1. Here we create a vector v, which has consecutive numbers from 5 to 10 (5:10).
  2. The for loop condition we used val value, it takes on the value of corresponding element of v.
  3. If the value is equal to 7, the current evaluation stops which means value is not printed, but the loop continues with the next iteration. Here we use the next statement inside a condition to check if the value is equal to 7.
  4. print() is used to print the value, that value stored in val variable.

Output

 r next statement output
  1. Here in this output we display the 5 6 8 9 10 values which specifies to, if the value is equal to 7, the current evaluation stops (7 value is not printed), but loop continues with the next iteration 8 9 10 display on the output.

Related Searches to Next R