Flow control—conditionals

Objectives

  • Know how to use the if statement to control the path of program execution.

In this lesson, we will look at how we can control the flow of our programs through the use of conditional statements. We’re going to do this using the family of if statements in Python.

We will approach conditionals in the context of a mock experiment. Say you are running an experiment with three conditions, represented by the numbers 1, 2, and 3. On a given trial, the stimulus shown to the participant depends on the condition number for that trial. For the purposes of this demonstration, our experiment will print “blank” if the condition number is 1, “A” if the condition number is 2, and “B” if the condition number is 3.

We can implement our experimental design using conditionals. For example:

cond_number = 2

if cond_number == 1:
    print "blank"

Let’s unpack this example:

  • First, we set a condition number for this trial (cond_number = 2).
  • Then comes the conditional aspect to the code. We start with the word if, which is a special Python ‘keyword’. We followed it by something that evaluates to a boolean; in this case, a test of equality (cond_number == 1). Then we have a colon character (:). You can read this line as “if the condition number is equal to 1”.
  • We then start a new line, indent by pressing the TAB key, and then specify a print statement.

Tip

This usage of indentation is very important to understand. Indentation is the method Python uses to indicate nested structure. It is not just for aesthetics—how the program operates is directly connected to the indentation structure of the code.

If we were to run this code, then it would produce no output. This is because the indented print statement is only executed if the if statement above it evaluates to True. Because the condition number is not equal to 1, the print statement is skipped and the program finishes.

To implement the design for our mock experiment, we have to include some additional conditional statements. We can do this using the elif (i.e. else if) command:

cond_number = 2

if cond_number == 1:
    print "blank"

elif cond_number == 2:
    print "A"
A

You can see that we’ve removed the indentation, because we don’t want our next conditional statement to be ‘under’ the previous if. We’ve used the command elif, followed by a test of whether the condition number is equal to 2. Because this evaluates to True, the program descends into the indented section and executes the print statement.

Let’s complete our experimental design by adding another elif statement:

cond_number = 2

if cond_number == 1:
    print "blank"

elif cond_number == 2:
    print "A"

elif cond_number == 3:
    print "B"
A

Finally, we will consider the last construct in the if family, the else command. Any statements that are nested under the else block are executed if none of the previous if or elif commands evaluated to True. In our example, the only way this could happen is if there is a problem with the condition number—so we will print a message to the user to that effect:

cond_number = 2

if cond_number == 1:
    print "blank"

elif cond_number == 2:
    print "A"

elif cond_number == 3:
    print "B"

else:
    print "Unexpected condition number"
A

Let’s wrap up by going through the program logic.

  • First, we set a condition number.
  • Then, we test if the condition number is equal to 1. If this is True, then we print the output “blank” to the screen. Because something in the construct has evaluated to True, we skip the rest of the tests and the program finishes.
  • If it evaluates to False, then we move down to the next elif statement, which asks if the condition number is 2. If that is True, then it prints “A” to the screen before moving out of the construct and finishing.
  • If it is False, then we move down to the next elif statement and do the same thing for the condition number 3.
  • Finally, if nothing has evaluated to True, we enter the else block and execute its print statement.

Let’s just check that our experimental design works as expected. Before we do so, let’s convert our code into a custom function so that we can easily apply it to different condition numbers:

def print_condition(cond_number):

    if cond_number == 1:
        print "blank"

    elif cond_number == 2:
        print "A"

    elif cond_number == 3:
        print "B"

    else:
        print "Unexpected condition number"

Note

If you’re unclear on how we created this custom function, you might find it useful to review Data—numbers, variables, and functions.

Now, let’s apply it to a range of condition numbers and see what output is produced:

def print_condition(cond_number):

    if cond_number == 1:
        print "blank"

    elif cond_number == 2:
        print "A"

    elif cond_number == 3:
        print "B"

    else:
        print "Unexpected condition number"

print_condition(cond_number=1)
print_condition(cond_number=2)
print_condition(cond_number=3)
print_condition(cond_number=4)
blank
A
B
Unexpected condition number