### R Fundamentals

##### The break statement

The break statement breaks out of the control loops like for and while. When a break is encountered, control is transferred to the first statement outside of the enclosing loop. When combined with if condition, the break can be effectively used for the conditional termination of the for or while loops. Example below illustrates this concept.


nevent = 100

for(i in 1:nevent)
{
if(i*12.0 > 200)
break;

print(i)

}

print("Now the control is outside of the for loop")


The value of the iterator i varies from 1 to nevent = 100 inside the for loop. There is an if condition inside the for loop that tests whether i*12 > 200 for every iterative value of i. When this test is true, the break statement transfers the control outside the first enclosing for loop. Since 17*12 > 200, the for loop should run for first 16 iterations when i varies from 1 to 16. This code prints out the following lines as expected:


[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
[1] 13
[1] 14
[1] 15
[1] 16
[1] "Now control is outside the for loop"