Break and Next statements in R [With Easy Examples]

Filed Under: R Programming
Break & Next Statements In R

Hello, readers! In this article, we will be focusing on the Break and Next statements in R in detail.

So, let us begin.

With iterative statements in any programming language, we need the use of control statements to take control of certain customized processes framed along with it.

These statements enable us to set a limit to the loops depending upon certain set of conditions.

Considering R as a programming language, it offers us with the below statements to set limits with customized conditions:

  • Break Statement
  • Next Statement

Let us have a look at this one by one in the upcoming sections.


1. Break statement in R

With Break statement, we can easily limit the loop or condition in iteration upon reaching a certain situation. That is, it terminates the loop as soon as it encounters a particular condition within the execution of the loop.

By this, as soon as it encounters the condition, the loop terminates.

Syntax:

if (condition)
{
 break
}

Example 1: Break statement with for loop

In this example, we have created a vector ‘data’. We have iterated the contents of data using a for loop and the loop terminates i.e breaks as soon as the pointer encounters the value ‘2’ in the vector.

rm(list = ls())
getwd()

data <- 1:6

for (x in data) 
{ 
  if (x==2) 
  { 
    print("Terminating for loop")
    break
  } 
  
} 
print(x)

Output:

[1] "Terminating for loop"
[1] 2

Example 2: Break statement with while loop

Now, we have implemented while loop over the data and as soon as the pointer encounters 13, it breaks out of the execution and terminates the loop.

rm(list = ls())
getwd()

data <- 10

while (data < 15) 
{	 
  print(data)	 
  if(data == 13)	 
    break	
  data = data + 1	
}	 

Output:

[1] 10
[1] 11
[1] 12
[1] 13

2. Next statement in R

R Next statement works completely opposite to Break statements. With next statement, we tend to ignore a particular state or condition upon its occurrence at a particular situation.

That is, as soon as the particular condition occurs, the “next keyword” ignores the upcoming statements within the loop and moves to the next iteration of the loop.

It does not terminate the loop, but rather considers the current iteration as complete and moves further.

Syntax:

if(condition)
{
next;
}

Example 1: Next Statement in R with the While Loop

In this example, we have set a while loop and as soon as the pointer encounters value 13, it skips the condition and forwards the control to the next condition.

rm(list = ls())
getwd()

data <- 10

while (data < 15) 
{	 data = data +1
  	 
  if(data == 13)	 
    next;
print(data)
  	
}	 

Output:

[1] 11
[1] 12
[1] 14
[1] 15

Example 2: Next statement with the For Loop

In this example, we have created a for loop and as soon as the pointer encounters 2, it skips the preceding action to the condition and forwards the control to the next iteration.

rm(list = ls())
getwd()

data <- 1:6

for (x in data) 
{ 
  if (x==2) 
  { 
    print(paste("Skip the for loop for x = ",x))
  
    next;
  } 
  print(x)
} 

Output:

[1] 1
[1] "Skip the for loop for x =  2"
[1] 3
[1] 4
[1] 5
[1] 6

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content