5 min read•august 14, 2024
Loops are the backbone of in R programming. They allow you to execute code multiple times, saving time and effort. This section covers three types of loops: for, while, and repeat, each serving different purposes in your code.
Understanding loops is crucial for efficient programming. You'll learn how to iterate over sequences, perform condition-based execution, and control loop behavior. These skills will help you tackle complex problems and automate repetitive tasks in your R projects.
numbers <- c(4, 9, 16, 25) for (num in numbers) { sqrt_num <- sqrt(num) print(sqrt_num) }
1:n
or seq_along(vector)
for (variable in sequence) { code block }
n <- 5 for (i in 1:n) { print(paste("Iteration", i)) }
TRUE
while (condition) { code block }
TRUE
, the code block is executed; this process continues until the condition becomes FALSE
FALSE
to avoid an infinite loop; this is usually achieved by modifying the variables involved in the condition within the code blockcount <- 0 while (count < 5) { print(count) count <- count + 1 }
<
, >
, ==
, !=
, &
, or |
user_input <- "" while (user_input != "quit") { user_input <- readline("Enter a value (or 'quit' to exit): ") print(user_input) }
repeat { code block }
[break](https://www.fiveableKeyTerm:break)
statement must be used within the code block; the break
statement is typically placed inside an if
statement that checks for the desired conditionrepeat { print("Menu:") print("1. Option 1") print("2. Option 2") print("3. Exit") choice <- readline("Enter your choice: ") if (choice == "1") { print("You selected Option 1") } else if (choice == "2") { print("You selected Option 2") } else if (choice == "3") { print("Exiting...") break } else { print("Invalid choice. Please try again.") } }
break
statement is reachable and that the condition for breaking the loop will eventually be satisfied to prevent an infinite loopvalid_input <- FALSE repeat { user_input <- readline("Enter a positive number: ") if (user_input > 0) { valid_input <- TRUE break } print("Invalid input. Please enter a positive number.") } print(paste("You entered:", user_input))
break
statement is used to exit a loop prematurely, immediately terminating the loop's execution and moving the program flow to the statement after the loopbreak
statement causes the loop to terminate, regardless of the remaining iterations or the loop conditionbreak
statement is commonly used in combination with conditional statements (e.g., if
) to exit a loop when a specific condition is metnumbers <- c(3, 7, 2, 9, 4) target <- 9 for (num in numbers) { if (num == target) { print("Target found!") break } }
next
statement is used to skip the remainder of the current iteration and move to the next iteration of the loopnext
statement immediately moves the program flow to the next iteration, skipping any remaining code in the current iterationnext
statement is often used to handle special cases or to avoid unnecessary computations for certain elements within a loopbreak
and next
statements provide control over the loop execution and allow for more flexible and efficient processing of elements within loopsbreak
and next
statements judiciously and ensure that they are placed in the appropriate locations within the loop to achieve the desired behaviornumbers <- c(1, 2, 3, 4, 5) for (num in numbers) { if (num %% 2 == 0) { next } print(num) }