You are currently viewing Break and Next (Continue) Statements in R

The Break and Next are jump statements in R that are used in control statements to terminate the loop at a particular iteration or to skip a particular iteration.

The looping in R is used to repeatedly execute a set of statements or block of code a specified number of times or until a specified condition is satisfied. There are three types of loops in R Programming, the for loop, the while loop and the repeat loop. You can use these jump statements with any of these loop statements.

Let’s understand the jump statements with examples.

In simple words, the break is used within the scope of the control statements to stop and exit the iteration without looping through all the iterations. And the next is similar to continue from other programming languages that is used to skip the current iterator and jump to the next iteration.

The next is similar to continue from other programming languages that is used to skip the rest of the iterator and jump to the next item in the sequence.

1. Break Statement in R

The break statement in R is used to exit or terminate the loop and the control goes to the very next statement after the loop. When a break statement is used in the nested loops, it exits from the innermost loop, and control transfers to the outer loop.

for break r

1.1 Using Break Statement with For Loop


# Using break statement
numbers <- c('One','Two',"Three","Four","Five")
for(i in numbers) {
  if( i == "Four")
    break
  print(i)
}

Yields below output.

break in r

1.2 Using Break Statement in Nested For Loop

Quickly let’s see what is nested for loop in R, If a for loop (inner loop) exists inside the body of the outer loop is called a nested loop. In each iteration of the outer loop, the inner loop will be re-started and the inner loop must finish all of its iterations before the outer loop can continue to its next iteration.

The break is used within the scope of either the outer or inner loop to exit the iteration without looping through all the items in sequence. When we use a break statement inside the inner loop, it just terminates the inner loop but not the outer loop. The below example demonstrates using the break statement in the outer as well as the inner loop.


# nested for loop using break
# outer loop & inner loop
for (i in c('A','B','C')){
  if( i == 'C')
    break
  # inner loop
  for (j in c('X','Y')){
    # print i & j
    if( j == 'Y')
      break
    print(c(i,j))
  }
}

# Output
#[1] "A" "X"
#[1] "B" "X"

1.3 Using Break Statement in While Loop

The following example demonstrates using a break statement in a while loop. Here, when i value becomes 4 we are exiting from the while loop.


# Using break & next
i <- 1
n <- 5
while (i <= n) {
  if(i == 4){
    break
  }
  print(i)
  i = i + 1
}

Similarly, you can also use the break statement with a nested while loop.

2. Next Statement in R

The next is similar to continue from other programming languages that is used to skip the rest of the iterator and jump to the next item in the sequence.

for next r

2.1 Using Break & Next Statement in R

The below example demonstrated using break and next statement in R.


# Continue or next
numbers <- c('One','Two',"Three","Four","Five")
for(i in numbers) {
  if(i == "Two")
    next
  if( i == "Four")
    break
  print(i)
}

Yields below output.

for loop syntax

2.2 Using Break & Next in Nested For Loop


# nested for loop using next
# outer loop
for (i in c('A','B','C')){
  # inner loop
  for (j in c('X','Y')){
    # print multiplication
    if( i == 'B' && j == 'X')
      next
    print(c(i,j))
  }
}

# output
#[1] "A" "X"
#[1] "A" "Y"
#[1] "B" "Y"
#[1] "C" "X"
#[1] "C" "Y"

2.3 Using Break & Next in While Loop

The below example uses both break and next statements in R with a while loop.


# Using 
i <- 1
n <- 5
while (i <= n) {
  if(i == 4){
    break
  }
  if(i == 3){
    i = i + 1
    next
  }
  print(i)
  i = i + 1
}

3. Conclusion

In this article, you have learned the break and next jumping statements in R and learned how to use them with for, and while statements. In simple words, the break is used within the scope of the control statements to stop and exit the iteration without looping through all the iterations. And the next is similar to continue from other programming languages that are used to skip the current iterator and jump to the next iteration.

Related Articles

References