Break R - r - learn r - r programming

  • A break statement is used inside a loop to stop the iterations and flow the control external of the loop.
  • In a nested looping situation, where there is a loop inside another loop, this statement exits from the innermost loop that is being evaluated.
  • It can be used to terminate a case in the switch statement.
   r break statement



Sample Code

v <- 1:10
for (val in v) 
    if (val == 5)

Code Explanation

 R break statement structure
  1. Here we create a vector v, which has consecutive numbers from 1 to 10 (1:10).
  2. The for loop condition we used val value, it takes on the value of corresponding element of v.
  3. If condition is specifying to, if the current value is equal to 5. The loop terminates when it encounters the break statement. Here print() is used to print the value, that value stored in val variable.


 R break statement structure output
  1. Here in this output we display the 1 2 3 4 values which specifies condition to break if the current value is equal to 5.

Related Searches to Break R