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.
- Here we create a vector v, which has consecutive numbers from 1 to 10 (1:10).
- The for loop condition we used val value, it takes on the value of corresponding element of v.
- 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.
- 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.