Break Statement in R

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 statement.

The line break statement is not a function it does not accept any value as an argument it simply kicks the program out of an iteration of an element sequence. This is an important tool because a program can create problems if it gets stuck. This can happen for any number of reasons from code errors to problems with the data being analyzed. It can even result from an incompatibility between the data and the program. Whatever the cause having a break command allows building into a program the ability to kick out of infinite loops.

Explanation of the statement.

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 the 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
> T=TRUE
> 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: