R break Statement

Introduction

In this chapter, you will learn about the break statement in R. The break statement is used within loops to immediately exit the loop, regardless of the iteration count or the loop’s termination condition. This is useful when you need to stop the loop based on a specific condition.

The break Statement

Basic Usage

The break statement can be used within for, while, or repeat loops to exit the loop when a certain condition is met.

Syntax

for Loop:

for (variable in sequence) {
  if (condition) {
    break
  }
  # Code to execute if condition is FALSE
}

while Loop:

while (condition) {
  if (inner_condition) {
    break
  }
  # Code to execute if inner_condition is FALSE
}

repeat Loop:

repeat {
  if (condition) {
    break
  }
  # Code to execute if condition is FALSE
}

Examples

Example 1: Using break in a for Loop

In this example, the break statement is used to exit the loop when a specific condition is met.

Example:

# Using break in a for loop
for (i in 1:10) {
  if (i == 5) {
    break  # Exit the loop when i equals 5
  }
  print(i)
}
# Output: 1 2 3 4

Example 2: Using break in a while Loop

In this example, the break statement is used to exit the loop when a specific condition is met.

Example:

# Using break in a while loop
i <- 1
while (i <= 10) {
  if (i == 5) {
    break  # Exit the loop when i equals 5
  }
  print(i)
  i <- i + 1
}
# Output: 1 2 3 4

Example 3: Using break in a repeat Loop

In this example, the break statement is used to exit the loop when a specific condition is met.

Example:

# Using break in a repeat loop
i <- 1
repeat {
  if (i == 5) {
    break  # Exit the loop when i equals 5
  }
  print(i)
  i <- i + 1
}
# Output: 1 2 3 4

Example Program with break Statement

Here is an example program that demonstrates the use of the break statement in R:

# R Program to Demonstrate break Statement

# Using break to exit the loop when a number divisible by 7 is found
for (i in 1:20) {
  if (i %% 7 == 0) {
    print(paste("Found a number divisible by 7:", i))
    break  # Exit the loop
  }
  print(i)
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] "Found a number divisible by 7: 7"

# Using break in a while loop to exit when a random number greater than 0.9 is generated
set.seed(123)  # Set seed for reproducibility
while (TRUE) {
  x <- runif(1)  # Generate a random number between 0 and 1
  print(x)
  if (x > 0.9) {
    print("Generated a number greater than 0.9. Exiting the loop.")
    break  # Exit the loop
  }
}

# Using break in a repeat loop to exit when a certain condition is met
i <- 1
repeat {
  if (i^2 > 50) {
    print(paste("Square of", i, "is greater than 50. Exiting the loop."))
    break  # Exit the loop
  }
  print(i)
  i <- i + 1
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] 7
# [1] "Square of 8 is greater than 50. Exiting the loop."

Conclusion

In this chapter, you learned about the break statement in R, including how to use it within for, while, and repeat loops. The break statement allows you to exit a loop immediately when a certain condition is met, which can be useful for stopping the loop based on specific criteria. By mastering the break statement, you can write more efficient and controlled loop structures in your R programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top