R: Next Statement

In the R programming language, the next statement is used when we want to skip any remaining statements in the loop and continue the execution of the loop. In other words, it is a statement that skips the current iteration without loop termination.

The next Statement works opposite to that of break statements, instead of terminating the loop, it forces to execute the next iteration of the loop.

  • It is used to skip any remaining statements in the loop and continue executing.
  • It is mostly used with for loop and while loop.

Syntax

if (test_condition)
{
    next
}

Example

a<-1    

while (a < 10)
{    
    if(a==5)    
        next    
    
    print(a)    
    a = a + 1    
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4