Break Statement in R – How To Control Loops & Execution Flow

Sometimes in programming, your program can get stuck in a loop, this is where the break statement comes into play. The line break in r programming is a simple statement with no arguments but it can serve as an emergency rescue for your program in situations where it can get stuck in an infinite loop.

Description of the Break Statement in R

While loops are wonderful, sometimes you need to call an immediate halt and escape the loop condition. Meet R’s break statement, which gives you the ability to interrupt the current loop (for, repeat, while). If you’re in a nested loop (loops within loops), it will break out of the current loop and return execution to the next outer loop in the sequence. Be wary of reusing the current value of any global variables as part of the outer loop.

The break statement provides you with a way to escape an infinite loop condition. This also works if you’re iteration through multiple data cleanup rules and identify a reason to stop the process for that record. The break statement allows you to end the current step in the innermost loop and resume processing with the next row.

Alternatives to using a break statement include developing switch statement logic that is capable of handling all possible cases of a situation. While this is slightly more desirable from a flow control perspective (you defined all cases, including a default), using the break statement in R gives you an option to secure an equivalent result.

Explanation of the Break Statement in R

The break statement is one of the simplest commands in R. It involves no arguments, no real way of creating a code error. It is hard, but not impossible, to mess up the typing of this command. Its function is even simpler. It just kicks the program out of a loop, function, or other programming elements to prevent the program from getting stuck and possibly freezing up your computer.

Examples of R Break Statement in action.

There are many things that can cause a need to use the break in R programming. They include processing an extremely long vector such as x=1:1000000000000000. They can also be created by a nested loop which could get extremely long and even infinite. The following examples use repeat and for loops in your R code.

# break in r
> y = 0
> repeat {
+ print(y)
+ y = y + 1
+ if(y > 5) break
+ }
[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

In this example answer, we have a repeat loop. Now without the break statement, this current loop would go on forever requiring you to use the “Esc” button on the keyboard to switch out of the R program text sequence. However, the break statement prevents this from happening in your current iteration output expression.

# how to use break statement in r programming
> x = 1:10
> for (y in x) {
+ print(y)
+ if (y == 4) break
+ }
[1] 1
[1] 2
[1] 3
[1] 4

This example uses a for loop, the break statement kicks the program out of the current loop in the R program sequence structure early and this could be needed for numerous reasons.

Application of this statement.

The most common application of the break statement in an R command file or character vector is kicking your program out of an otherwise infinite loop output structure.

# break in r example
> a = Sys.time()
> c=0
> while(T){
+ b = Sys.time()
+ c = b-a
+ if (c > 10) break
+ }
> c
Time difference of 10.00019 secs

This syntax example uses a while loop and because T is always true it would go on forever without the break statement. Under normal circumstances, this would be bad variable programming but it makes for an easy to follow example. In this case, it goes on for about 10 seconds and then the brake command character vector stops it. Such an infinite loop could occur in practice by the data syntax never turning the condition false. In such a situation you can create an independent condition such as time to prevent an infinite loop.

The primary purpose for the break statement is preventing infinite loops, however, it can be used for other purposes. One such purpose would be kicking out of a for loop after getting what you want but before reaching natural the end of the loop. It is a simple command with lots of power and one that will keep your program running as intended.

Looking for more great R programming content? Check out the rest of our site and these great articles:

Scroll to top
Privacy Policy