|
Home |
---|
In this tutorial we will discuss break and next statement in R Programming. You will learn function, syntax, flowchart and applications of break and next with the help of examples.
In R programming break and next are used in a loop where normal loop sequence is altered with the help of these statements as per requirement.
A break statement is used to stop the iteration of a loop i.e. to terminate it based on a condition and move the flow control to the next statement just after the loop. It is used in for loop, while loop and repeat loop.
Now if there are nested loops then this statement immaturely terminates the iteration of loop in which this statement is used, however normal flow of nesting is carried out as designed.
The syntax of break statement is very simple as we use a condition and put break keyword in its block of code.
...
if (condtion) {
break
}
...
for
( x in 1:10) {
if ( x == 5) {
break
}
print (x)
}
Output:
[1]
1
[1]
2
[1]
3
[1]
4
In
this example we have created a for loop which iterates from 1 to 10.
However the condition is if the value is 5 then break or terminate
loop. After if condition with break there is a single statement of
print. When this program is run it prints from 1 to 4 and when the
value is 5, the condition is True and the break statement is executed
which moves the flow control out of this loop and exits the program as
there is no more code to execute.
a
<- 1
while( a < 7){
print(a)
a = a + 1
if ( a > 3) {
break
}
}
Output:
[1] 1
[1] 2
[1] 3
Here
a while loop initializes with the variable a as 1. Even when
the
condition of while loop is True the loop stops or terminates when the
value of a is more than three. Hence it prints only 1 2 and 3.
for
( i in 1:11) {
if ( i < 5) {
print (i)
} else {
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
break can also be used in else part of if else structure. For loop condition is iteration from 1 to 11. If condition is print all values if they are less than 5 and the else structure is if value is not less than five then break out of the loop.
next statement is used to skip the current iteration of the loop without terminating it and starts the next iteration of the loop. When R interpreter finds the next statement it stops further evaluation of current iteration of loop and starts next iteration.
The syntax of next statement in R is very easy as we use a condition and put next in its block of code in case if condition is true next is executed.
...
if (condtion) {
next
}
...
for
( x in 1:5) {
if ( x == 4) {
next
}
print (x)
}
[1] 1
[1] 2
[1] 3
[1] 5
A for loop is set to run five iterations from 1 to 5 using variable x. The condition used is if x equals 4 then next. It means when the value of x is 4 the remaining part of loop is skipped i.e. it will not print 4 as the print statement will not be executed for 4th iteration of loop.
for
( j in 1:12) {
if ( j %%2 == 0) {
next
}
print (j)
}
Output:
[1] 1
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
In this example we have used next statement inside a condition which checks if a number is divisible by 2. In case a number is divisible by 2 next statement is executed which means the print statement in for loop is skipped and the loop continues the next iteration.