Open In App

Break and Next statements in R

Improve
Improve
Like Article
Like
Save
Share
Report

In R Programming Language, 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 the 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 a while loops are used to run the statement or get the desired output N a 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 in R

Break-Statement-in-R

Break statements in R

The break Statement in R is a jump statement that is used to terminate the loop at a particular iteration.

Syntax:

if (test_expression) {
break
}

Break Statement in R using For-loop

R




# 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"

Break statement in R using While-loop

R




# 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 in R

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

Next statement in R

Syntax:

if (test_condition) 
{
next
}

Next statement in R using For-loop

R




# 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"

Next statement in R using While-loop

R




# 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


Last Updated : 20 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads