Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
We use the R break
and next
statements to alter the flow of a program. These are also known as jump statements in programming:
break
– terminate a looping statementnext
– skips an iteration of the loop
R break Statement
You can use a break
statement inside a loop (for
, while
, repeat
) to terminate the execution of the loop. This will stop any further iterations.
The syntax of the break
statement is:
if (test_expression) { break }
The break
statement is often used inside a conditional (if...else
) statement in a loop. If the condition inside the test_expression
returns True
, then the break
statement is executed. For example,
# vector to be iterated over x = c(1, 2, 3, 4, 5, 6, 7) # for loop with break statement for(i in x) { # if condition with break if(i == 4) { break } print(i) }
Output
[1] 1 [1] 2 [1] 3
Here, we have defined a vector of numbers from 1
to 7
. Inside the for
loop, we check if the current number is 4
using an if
statement.
If yes, then the break
statement is executed and no further iterations are carried out. Hence, only numbers from 1
to 3
are printed.
break Statement in Nested Loop
If you have a nested loop and the break
statement is inside the inner loop, then the execution of only the inner loop will be terminated.
Let's check out a program to use break statements in a nested loop.
# vector to be iterated over x = c(1, 2, 3) y = c(1, 2, 3) # nested for loop with break statement for(i in x) { for (j in y) { if (i == 2 & j == 2) { break } print(paste(i, j)) } }
Output
[1] "1 1" [1] "1 2" [1] "1 3" [1] "2 1" [1] "3 1" [1] "3 2" [1] "3 3"
Here, we have a break
statement inside the inner loop.
We have used it inside a conditional statement such that if both the numbers are equal to 2
, the inner loop gets terminated.
The flow then moves to the outer loop. Hence, the combination (2, 2)
is never printed.
R next Statement
In R, the next
statement skips the current iteration of the loop and starts the loop from the next iteration.
The syntax of the next
statement is:
if (test_condition) { next }
If the program encounters the next
statement, any further execution of code from the current iteration is skipped, and the next iteration begins.
Let's check out a program to print only even numbers from a vector of numbers.
# vector to be iterated over x = c(1, 2, 3, 4, 5, 6, 7, 8) # for loop with next statement for(i in x) { # if condition with next if(i %% 2 != 0) { next } print(i) }
Output
[1] 2 [1] 4 [1] 6 [1] 8
Here, we have used an if
statement to check whether the current number in the loop is odd or not.
If yes, the next
statement inside the if block is executed, and the current iteration is skipped.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.