Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

2. Write an R program Illustrate with for loop and stop on condition, to print the error message. || R

 # Set a flag to indicate if an error occurred 

error_flag <- FALSE 

# Generate random numbers and stop on condition 

for (i in 1:10) {  # Loop 10 times (you can change this 

number) 

random_number <- runif(1)  # Generate a random number 

between 0 and 1 

# Check if the random number is greater than 0.9 

if (random_number > 0.9) { 

error_flag <- TRUE 

cat("Error: Random number exceeded the threshold (", 

random_number, ")\n") 

break  # Exit the loop when the condition is met 

# Perform other operations here if needed 

# Check if an error occurred 

if (!error_flag) { 

cat("No errors encountered.\n") 

}

Post a Comment

0 Comments