Control flow

Control flow is the order in which a program executes operations. Sometimes we want to execute operations only if certain conditions are met. Control flow statements help govern this. This tutorial will cover if/else statements, and how they can be used to implement this.

if statements

if statements check if certain conditions are met. If they are, the code following the statement is executed. If not, the code is skipped, and the program continues on.

if statements have 3 parts. The if statement itself, the condition, and the code to be executed if the condition is met.

Let’s start with 2 variables.

x = 5
x
[1] 5
y = 15
y
[1] 15

We can write an if statement to check if the number is < 10. If it is, we will subtract 3 and store this in a new variable.

if(x < 10){
  x1 = x - 3
}
x1
[1] 2

In this if statement, R checks that the condition (x < 10) is met. Since it is, it executes the code between the {}. That is, it creates a new variable “x1” that is 3 less than x.

Let’s see what would happen if we were to apply this same if statement to “y”.

if(y < 10){
  y1 = y - 3
}
y1
Error in eval(expr, envir, enclos): object 'y1' not found

Here, R tells us that “y1” is not found. This is because the if statement checks whether y < 10, Since it is not, the condition is not met, and the code inside the {} is not executed. “y1” is never created, so when we ask R to print “y1” it gives us an error, because “y1” does not exist.

else statements

We can also add code to execute if the condition is not met. This is done via an else statement following the if statement.

if(y < 10){
  y1 = y - 3
} else{
  y1 = y + 3
}
y1
[1] 18

As before, the if statement checks whether the condition (y < 10) is true. Since it is not, it skips the code within the {} for the if statement, and looks for an else statement following it. It then sees our else statement and executes the code within the {}, in this case, increasing “y” by 3.

To make sure that the else statement only executes when intended, I like following an else statement with another if statement.

if(y < 10){
  y1 = y - 3
} else if(y >= 10){
  y1 = y + 3
}
y1
[1] 18

This produces the same result as before. However, by adding a second if statement after the else (aka an else if), I can explicitly state the condition under which the else statement is executed, to be doubly sure.

I like explicitly stating the mutually exclusive and exhaustive condition in the last if statement, so I can be absolutely sure I know when R will execute it. Else statements without this execute if the if statement preceding it is false. In this case it is pretty straightforward, but there are many reasons for an if statement to be false, and I don’t trust my ability to imagine all of them every time.

Of course, if the condition for the original if statement is met, the else statement is not executed, and the code within it is skipped.

if(x < 10){
  x1 = x - 3
} else if(x >= 10){
  x1 = x + 3  
}
x1
[1] 2

ifelse()

In R, the ifelse() function can be used to implement simple if and else statements. The first argument is the condition, the second is what should be returned if the statement is true, and the third is what should be returned if the statement is false.

ifelse(x < 10, x-3, x+3)
[1] 2
ifelse(y < 10, y-3, y+3)
[1] 18

These results can of course be saved into variables.

Chaining if statements

We can chain if statements together using else ifs to allow for the evaluation of multiple conditions. The conditions are evaluated in order until one is met, at which point the code for that if statement is executed and the rest are skipped.

if(y < 10){
  y1 = y - 3
} else if(y < 13){
  y1 = y + 3
} else if(y < 16){
  y1 = y + 7
} else if(y < 19){
  y1 = y + 12
}
y1
[1] 22

Since “y” is 15 which is not less than 10, the first if is false. The second if checks if y \(<\) 13. Since we know that y \(\geq\) 10 from the first if, this will only execute if 10 \(\leq\) y \(<\) 13. However, this too is false. The next if statement checks if 13 \(\leq\) y \(<\) 16. Since this is true, 7 is added to “y”. The final else if is never reached and is skipped.