Home » R Break Statement

R Break Statement

by Online Tutorials Library

R Break Statement

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:

  1. When the break statement is inside the loop, the loop terminates immediately and program control resumes on the next statement after the loop.
  2. 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

R Break statement

Example 1: Break in repeat loop

Output:

R Break statement

Example 2

Output:

R Break statement

Example 3: Break in while loop

Output:

R Break statement

Example 4: Break in for loop

Output:

R Break statement

Example 5

Output:

R Break statement


Next TopicR For Loop

You may also like