Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Break and Next statements in R

  • Last Updated : 04 May, 2020

In R programming, we require a control structure to run a block of code multiple times. Loops come in the class of the most fundamental and strong programming concepts. A loop is a control statement that allows multiple executions of a statement or a set of statements. The word ‘looping’ means cycling or iterating.

Jump statements are used in loops to terminate the loop at a particular iteration or to skip a particular iteration in the loop. The two most commonly used jump statements in loops are:

  • Break Statement
  • Next Statement

Note: In R language continue statement is referred to as the next statement.

The basic Function of Break and Next statement is to alter the running loop in the program and flow the control outside of the loop. In R language, repeat, for and while loops are used to run the statement or get the desired output N number of times until the given condition to the loop becomes false.
Sometimes there will be such a condition where we need to terminate the loop to continue with the rest of the program. In such cases R Break statement is used.
Sometimes there will be such condition where we don’t want loop to perform the program for specific condition inside the loop. In such cases, R next statement is used.

Break Statement

The break keyword is a jump statement that is used to terminate the loop at a particular iteration.
Syntax:



if (test_expression) {
break
}

Example 1: Using break in For-loop




# R program for break statement in For-loop
  
no <- 1:10
  
for (val in no)
{
    if (val == 5
    {
        print(paste("Coming out from for loop Where i = ", val))
        break
    }
    print(paste("Values are: ", val))
}

Output:

[1] "Values are:  1"
[1] "Values are:  2"
[1] "Values are:  3"
[1] "Values are:  4"
[1] "Coming out from for loop Where i =  5"

Example 2: Using break statement in While-loop




# R Break Statement Example
a<-1    
while (a < 10)
{    
    print(a)    
    if(a==5)    
        break    
    a = a + 1    
}    

Output:

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

Next Statement

The next statement is used to skip the current iteration in the loop and move to the next iteration without exiting from the loop itself.
Syntax:

if (test_condition) 
{
    next
}

Example 1: Using next statement in For-loop




# R Next Statement Example
  
no <- 1:10
  
for (val in no)  
{
    if (val == 6)  
    {
        print(paste("Skipping for loop Where i =  ", val))
        next
    }
    print(paste("Values are:  ", val))
}

Output:

[1] "Values are:   1"
[1] "Values are:   2"
[1] "Values are:   3"
[1] "Values are:   4"
[1] "Values are:   5"
[1] "Skipping for loop Where i =   6"
[1] "Values are:   7"
[1] "Values are:   8"
[1] "Values are:   9"
[1] "Values are:   10"

Example 2: Using next statement in While-loop




# R Next Statement Example
x <- 1
while(x < 5
{
    x <- x + 1
    if (x == 3
        next
    print(x);

Output:

[1] 2
[1] 4
[1] 5



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!