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:

if (condition) {
  # Do something
} else {
  # Do something different
}

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.

if (condition1) {
  # Do statement 1
} else if (condition2) {
  # Do statement 2
} else if (condition3) {
  # Do statement 3
} else {
  # Do statement 4
}

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

Q1

Without directly utilizing the abs() function, create a custom function to compute the absolute value of any given number: it should return the number unchanged if it is positive, and its positive counterpart if it is negative. Verify the functionality by calculating the absolute values of 7 and -7.

Q2

Without using the max() function, create a custom function that identifies the larger of two numbers with an if statement. Test its accuracy by determining the maximum between 376 and 924.

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 (variable in sequence) {
  # Do something
}

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

Q1

Write a for loop to print the mean of each column in the mtcars dataset. Additionally, how would you modify the loop to store all the mean values in a vector?

Q2

Write a for loop that adds numbers from 1 to 10.

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.

while (condition) {
  # Do something
}

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:

# CAUTION: THE FOLLOWING IS AN INFINITE LOOP.
# TERMINATE IT USING Ctrl + C IF IT RUNS.

# Initialize the variable 'i'
i <- 1
 
while (i < 5) {
  print(i)  
  i <- i - 1  # Mistakenly decremented i, which means it will never exceed 5
}

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 R in a terminal or console, you can usually stop the process by pressing Ctrl + 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.

Exercise D

Q1

Use a while loop to print the square roots of integers from 10 to 0.

Q2 (Optional)

Employ a while loop to determine the smallest integer \(n\) for which the sum \(\sum_{i=1}^n i^2\) exceeds 1000.