R Next and Break Statement with Example

The next statement in R is useful when we need to skip the current iteration of a loop without eliminating it. Like with while and repeat loops, you can break the for loop completely by using a break statement. Furthermore, if you want to skip the current iteration and continue the loop, use the next statement. It is beneficial if your loop finds an error, but you don’t want it to break everything.

You don’t need to use both break and next simultaneously, which clearly shows the general structure of using them. The reason for using next at the beginning of the program is that before the code runs, it checks for a problem before it happens.

Let’s see both next and break statements in detail.

R Next Statement

The next is the built-in R statement that is useful when we want to skip the current repetition without ending it.  On encountering next, the R parser hops further evaluation and starts the next iteration of the loop.

Next in the R is used with loop statements to provide additional control instructions to the loop processing logic in the runtime.

It provides a feature to skip any specific loop iteration of the statement or logical step while looping through the R statements. 

Syntax

if (condition) {
   next
}

The next statement can also be used inside the else branch of the if…else statement.

How does Next Statement Work in R

  1. The next advances the looping index in R.
  2. The next is a reserved keyword, which is used to restrict the current loop iteration based upon the return value of the condition.
  3. The next statement in R is used for flow control.
  4. R runtime evaluates the loop code and checks the associated loop conditions.
  5. If the loop condition returns a TRUE value, then the flow control goes inside the loop block.
  6. If the loop condition returns a FALSE value, it treats the loop condition as invalid and immediately ends the execution and exits from the loop.

Example

Define a vector using the colon( : ) operator and use the for loop to print the sequence except 4, which we will pass.

dt <- 1:6

for (val in dt) {
 if (val == 4) {
    next
 }
 print(val)
}

Output

[1] 1
[1] 2
[1] 3
[1] 5
[1] 6

In the above example, we use the next statement inside a condition to check if the value is equal to 4.

If the value is equal to 4, the current evaluation stops (value is not printed), but the loop continues with the next iteration.

Let’s use the LETTERS constant. To create a sequential uppercase alphabet in R, use the LETTERS constant.

dt <- LETTERS[1:6]

for (k in dt) {
 if (k == "C") {
 next
 }
 print(k)
}

Output

[1] "A"
[1] "B"
[1] "D"
[1] "E"
[1] "F"

In the above example, we use the next statement inside a condition to check if the value is equal to C.

If the value is equal to C, the current evaluation stops (value is not printed), but the loop continues with the next iteration.

R Break Statement

A break is an inbuilt R statement used inside a loop (repeat, for, while) to stop the iterations and flow the control outside of the loop. When a break statement is found inside the loop, the loop is instantly terminated, and program control resumes at the next statement following the loop.

Syntax

if (expression) {
     break
}

The break statement can also be used inside the else branch of the if…else statement.

Example

dt <- LETTERS[1:6]

for (k in dt) {
 if (k == "C") {
    break
 }
 print(k)
}

Output

[1] "A"
[1] "B"

In this example, we iterate over the LETTERS constant, in which we are using up to 6 characters. Inside the for loop, we have used an if condition to break if the current value is equal to C.

As we can see from the output, the loop terminates when it encounters the break statement. At that time, it will print up to that value, which is and B.

R While Loop Break Statement

We can use the break statement inside the While loop to exit from the loop iteration.

num <- 10

while (num > 0) {
 if (num == 5) {
  break
 }
 print(paste("Values are :", num))
 num <- num - 1
}

Output

[1] "Values are : 10"
[1] "Values are : 9"
[1] "Values are : 8"
[1] "Values are : 7"
[1] "Values are : 6"

That is it for the R next and break statements.

See also

R rep()

R seq()

R map

Leave a Comment