24 Flow control and loops
Flow control in R is like choosing your path in a choose-your-own-adventure book. It allows your program to make decisions, repeat actions, and change its course based on certain conditions. Flow control can be tricky. Start with simple if-else structures and gradually build complexity by adding more conditions and integrating loops.
24.1 if statement
The if statement is a fundamental aspect of flow control in programming. It allows your program to execute a block of code only if a specified condition is true. Think of it as a decision-making junction, where your program can take different paths based on certain criteria. In R, the basic syntax of an if statement is straightforward, yet it holds the power to add significant logic and complexity to your code.
The structure of an if statement in R is as follows:
For example,
# Assign the value 3 to the variable 'i'
i <- 3
# Check if 'i' is greater than 3
if (i > 3) {
print("Yes") # If 'i' is greater than 3, print "Yes"
} else {
print("No") # If 'i' is not greater than 3, print "No"
}[1] "No"
Conditions are typically represented by logical statements such as a != b, a > b, a >= b, a < b, a <= b, or a %% b == c, among others. These can be combined using the logical operators & (AND) and | (OR).
# Assign the value 5 to the variable 'i'
i <- 5
# Check if 'i' is greater than 2 AND less than 4
if (i > 2 & i < 4) {
print("Yes") # If both conditions are true, print "Yes"
} else {
print("No") # If either condition is false, print "No"
}[1] "No"
# Check if 'i' is greater than 2 OR less than 4
if (i > 2 | i < 4) {
print("Yes") # If at least one condition is true, print "Yes"
} else {
print("No") # If both conditions are false, print "No"
}[1] "Yes"
In the first if statement, the condition uses the logical AND operator &, which requires both conditions to be true. In the second if statement, the condition uses the logical OR operator |, which requires at least one condition to be true.
If there are more than two alternatives, we can construct an if...else ladder using else if.
For example,
# Assign the value 3 to the variable 'i'
i <- 3
# Check if 'i' is greater than 3
if (i > 3) {
i <- i + 1 # If true, increase 'i' by 1
} else if (i < 3) { # If the first condition is false, check if 'i' is less than 3
i <- i - 1 # If true, decrease 'i' by 1
} else { # If none of the above conditions are true
print("Good guess") # Print "Good guess"
}[1] "Good guess"
Note the format here. Command statements always appear on a separate line and do not require any accompanying brackets.
Exercise B
24.2 Loops
Loops allow you to perform repetitive tasks efficiently. Instead of writing the same code over and over, a loop can iterate through data, performing the same action on each element. Always think about your exit condition. An infinite loop is like a conversation that never ends—not very productive!
R executes loops using the syntax shown below, with for and while being the most common types.
24.2.1 for loop
A for loop is a control flow statement that allows code to be executed repeatedly through specified iterations.
For example,
# Initialize a 'for' loop to iterate over a sequence from 1 to 4
for (i in 1:4) {
j <- i + 10 # Add 10 to the current value of 'i' and assign it to 'j'
print(j) # Print the value of 'j' for each iteration
}[1] 11
[1] 12
[1] 13
[1] 14
Exercise C
24.2.2 while loop
A while loop is a control flow statement that repeatedly executes code as long as a specified boolean condition is true. It can be considered a recurring if statement.
For example,
# Initialize the variable 'i'
i <- 1
# While loop: runs as long as 'i' is less than 5
while (i < 5) {
print(i) # Prints the current value of 'i'
i <- i + 1 # Increments 'i' by 1
}[1] 1
[1] 2
[1] 3
[1] 4
A common pitfall with while loops is creating an infinite loop, where the loop’s exit condition is never met. Here’s an example of a poorly constructed while loop that will run indefinitely:
In this example, i starts at 1 and is continually decreases. Since i will never exceed 5, the loop will run endlessly.
To kill an infinite loop in R:
- If you are using
Rin a terminal or console, you can usually stop the process by pressingCtrl + C. - If you are using an IDE like
RStudio, there is typically a “Stop” button (usually a red square) in the console or script pane that you can click to terminate the running script.
After stopping the infinite loop, you will want to debug your code to ensure that the exit condition can be met to avoid future infinite loops.