R: Break Statement

In the R language, the break statement is used to break the execution and terminate from the loop at a particular iteration. In the case of a nested loop, breaks exist from the innermost loop only and control the program execution flow. 

Break Statement has two usages which are as follows:

  1. When the break statement is inside the loop, the loop is terminated immediately and program control goes to the next statement after the loop.
  2. It is also used in the switch statements for the purpose of termination of the case.

Syntax

if (test_expression)
{
    break
}

Example

a<-10    

while (a>5)
{    
    print(a)    
    
    if(a==5)    
        break    
    a = a - 1    
}

Output:

[1] 10
[1] 9
[1] 8
[1] 7
[1] 6