R Break Statement

The Break and Next in R Programming are the two essential statements used to alter the flow of a program. In R Programming, Loops are used to execute a particular block of code for N number of times until the test expression is false. There will be some situations where we have to terminate the loop without executing all the lines. In these situations, we can use the R Break and Next statements.

R Break Statement

The R Break statement is very useful to exit from any loop such as For, While, and Repeat. While executing these, if it finds the break statement inside them, it will stop executing the code and immediately exit from the loop.

For example, we have 15 statements inside the loop and want to exit from the loop when a certain condition is True; otherwise, it has to execute all of them. In this situation, we can use the If condition to check for the expression and place the R Break statement inside the If block. If the condition is True, it will execute the break statement, and the break will completely exit the controller from the loop. Otherwise, it will execute all of them.

R Break statement Syntax

The syntax of the Break Statement in R Programming language is

break

In this article, We would like to share 2 examples to display the working functionality of the R Break statement in both the While and For loops. Please refer Repeat article to understand the R Programming Repeat loop example

R For Loop Break Statement

This program uses the R break statement inside the for loop to exit from the iteration.

# Example

number <- 1:10

for (val in number)  {
  if (val == 7)  {
    print(paste("Coming out from for loop Where i =  ", val))
    break
  }
  print(paste("Values are :  ", val))
}
R Break Statement 1

Within the For loop, we placed the If Statement to test whether i is equal to 7.

  • If the expression is false, then it will skip the Break and print that number as output (In Our case 1, 2, 3, 4, 5, 6).
  • If the expression is True, then the Break is executed, and the iteration will stop at that number without printing the line: print(paste(“Values are : “, val)).

R While Loop Break Statement

This program uses the break statement inside the While loop to exit from the iteration.

# Example

number <- 10

while (number > 0)  {
  if (number == 3)  {
    print(paste("Coming out from While loop Where number =  ", number))
    break
  }
  print(paste("Values are :  ", number))
  number = number - 1
}
R Break Statement 2

First, We initialized the value of the number to 10 at the beginning of the code. Within the While loop, we check for the condition of whether the number is greater than 0 or not.

while (number > 0)  {

Inside the While loop, we placed the If Statement to test whether i is equal to 3.

  • If the condition is false, then it will skip and prints that number as output (In Our case, 10, 9, 8, 7, 6, 5, 4).
  • If this condition is True, then the Break statement is executed, and the iteration will stop at that number without printing the print(paste(“Values are : “, number)).