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 statements 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 statements. In these situations, we can use this R Break statement and Next statements.

R Break Statement

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

For example, we have 15 statements inside the loop, and we 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 If Statement to check for the expression and place the Break statement inside the If block. If the condition is True, then it will execute the break statement, and the break will exit the controller from the loop completely. Otherwise, it will execute all the statements.

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 Break statement in both While Loop and For Loop. Please refer R Repeat Loop article to understand the R Programming Repeat loop example

R For Loop Break Statement

In this R program, we use the break statement inside the for loop to exit from the loop iteration.

# R Break Statement 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 If Statement to test whether i is equal to 7.

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

R While Loop Break Statement

In this program, we use the R break statement inside the While loop to exit from the loop iteration.

# R Break Statement 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 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 the Break statement and prints that number as output (In Our case 10, 9, 8, 7, 6, 5, 4).
  • If this condition is True, then the R Break statement executed, and the iteration will stop at that number without printing the print(paste(“Values are : “, number)) statement.