In the R language, the break statement is used to break the execution and for an immediate exit from the loop. In nested loops, break exits from the innermost loop only and control transfer to the outer loop.
It is useful to manage and control the program execution flow. We can use it to various loops like: for, repeat, etc.
There are basically two usages of break statement which are as follows:
- When the break statement is inside the loop, the loop terminates immediately and program control resumes on the next statement after the loop.
- It is also used to terminate a case in the switch statement.
Note: We can also use break statement inside the else
branch of if...else
statement.
Syntax
There is the following syntax for creating a break statement in R
Flowchart
Example 1: Break in repeat loop
Output:
Example 2
Output:
Example 3: Break in while loop
Output:
Example 4: Break in for loop
Output:
Example 5
Output:
No comments:
Post a Comment