R Next Statement

The Next statement is one of the most useful statements that control the flow of R Programming language loops. We generally use this Next statement inside the For Loop and While Loop. While executing these loops, if the compiler finds the R Next statement inside them, it will stop the current loop iteration and starts the new iteration from the beginning.

For example, If we have 10 statements inside the loop, and we want to skip executing the second 5 lines (statement6 — statement10) when a certain condition is True. Otherwise, it has to execute all 10 statements inside the loop. In this situation, we place the If condition with a certain condition and within that place the R Next statement. If the condition is True, it will stop executing statements 6 to 10. Otherwise, it will execute statements 1 to 10.

R Next Statement Syntax

The syntax of the R Next Statement is

Next

In this article, we would like to share two examples to display the working functionality of the R Next statement in both While Loop and For Loop.

R Next Statement in For Loop

In this program, we show how to use the R Next Statement inside the For Loop with an example. This program will display the Even and Odd numbers inside the given range.

# Example

number <- 1:20

for (val in number) {
  if (val %% 2 != 0)  {
    print(paste("ODD Number =  ", val, "(Skipped by Next Statement)"))
    next
  }
  print(paste("EVEN Number =  ", val))
}
R Next Statement 1

Inside the for loop, we placed the If Statement to test whether (val %% 2 != 0).

  • If this expression result is True, the Next statement is executed. And the iteration will stop at that number without printing the other: print(paste(“EVEN Number = “, val)).
  • If the expression result is false, then it will skip the Programming Next statement and print that number as output (In Our case, Even Number).

Next Statement in R While Loop Example

In this program, we show how to use R Next Statement inside the While Loop with an example. This program allows the user to enter any integer values. Then it will display all the values from 0 to a given number except 4 and 7.

number <- 0

while (number <= 10)  {
  if (number == 4 || number == 7)  {
    print(paste("Skipped by the Next Statement =  ", number))
    number = number + 1
    next
  }
  print(paste("Values are :  ", number))
  number = number + 1
}
R Next Statement 2

Inside the While loop, we placed the If Statement to test whether i is equal to 4 or 7.

  • If this condition is True, then the R Next statement executes, and the iteration will stop at that number without printing the other: print(paste(“Values are : “, number)). For better understanding, we placed print(paste(“Skipped by the Next Statement = “, number)) inside the If condition. So, whenever the iteration break, that value be printed from this statement.
  • If the condition is false, then it will skip the next statement and print that number as output (In Our case, 0,1,2,3,5,6,8,9,10).