If, elif, else

Have you ever thought, "If I do well on the exam, then I'll treat myself to something nice". Let's learn how to code this situation in today's lesson.

In this advanced topics lesson we will learn about control flow. Control flow (or flow of control) is the order in which the instructions of a program are executed. Since this is a broad and difficult topic, we will cover only one aspect of it, the conditional aspect of it.

Review of conditional expressions

We will be using conditional expressions again, we learned these back in the Operators with Booleans lesson. There we explored how conditional expressions can evaluate to either True or False. In this lesson we will use conditional expressions in a slightly different way, to control the flow of the program, by using if and else statements.

To make a more tangible connection of what this definition means, let's look at one of the conditional expressions we learned before, the double equality sign.

The expression a == 1 evaluates to True. This happens because the value on the left-hand side of the double equality sign is the same as the value on the right-hand side. When an expression is either True or False, it is a boolean datatype.

What is an if-else statement?

The if and else statements are used to dictate the flow of a program based on whether a certain condition is met. Here's how it works: a condition is evaluated, and if it turns out to be true, a specific block of code (let's call it action A) will execute. If the condition is false, a different block of code (referred to as action B) will run instead. In this context, we could have a setup where if the variable a equals 1, Action A is carried out; if not, Action B is triggered.

Let's look at just the if part. In the following code editor, the condition within the parenthesis after the if is evaluated. Since this condition is True, the code within the curly brackets is run, and the text, "The condition is true" is printed out.

Next let's look at the else part. In the following code editor, the condition within the parenthesis after the if is evaluated. Since this condition is False, the code within the curly brackets after the if does not run, and instead the code within the else curly brackets is run, such that the text, "The condition is false" is printed out.

We can expand the if-else statement to include additional conditions. In the next code editor we add an else if statement, which will run if the first condition is False, but the second condition is True. The order is important, because if the first condition is True, then the second (and beyond) condition(s) will not even be checked.

Conditions with multiple expressions

In the next code editor we combine two conditions to make for a more complex and expressive condition. This approach is beneficial when we desire a certain block of code to execute only if multiple conditions hold true. To achieve this, we employ either the or symbol to represent "or" or the and symbol for "and," allowing us to properly intertwine these conditions.

In the above code editor even though in the first condition the a == 1 part was False, since the b == 2 part was True, the entire condition was True and the first block of code was run.

To look at more combinations of these, let's put the if-else statement into a function, where we may vary the values of the arguments to see how the conditional expression is evaluated.

Putting if-else into a function

In this section we will wrap the if-else condition into a function so we can explore all the potential outcomes of it.

In the next code editor, we write a function, named coffee_reward, that takes two inputs: a course variable (a string representing the name of a course) and a grade variable (a string representing a grade). Depending on the combination of the course and grade provided, it will print a different message indicating the reward someone would get.

The function operates through a series of conditional statements to determine which message to print:

  • 1. If the course is either "Espresso Making" or "Latte Art" and the grade is "A", it prints the message, "a brand new espresso machine!"
  • 2. If the first condition isn't met but the grade is "A", or "B" it prints, "a gourmet coffee bean package". We used the in operator here to check if the grade is any of the potential values within the list.
  • 3. If neither of the above conditions is met, it defaults to printing, "a coffee brewing workshop".

After defining the function, it is called twice with different sets of arguments to demonstrate the different outputs it can produce based on the inputs. The first call with "Espresso Making" and "A" as arguments results in, "a brand new espresso machine!" being printed. The second call with "Coffee History" and "A" as arguments results in, "a gourmet coffee bean package" being printed.