Conditional Execution#

One type of control structure involves selection, choosing what piece of code to execute based on a condition’s true/false value. We can also describe this as conditional execution.

A Mental Model#

Let’s imagine a real-world scenario. It’s 10:45am on Tuesday and our class just wrapped up. You want to grab coffee before your next class.

Since we meet in DeBartolo, your closest option will be Duncan Student Center’s Hagerty Cafe. But, lots of other people just got out of class and might also be on the hunt for coffee. Enter a condition you need to test for: Can I get coffee from Hagerty Cafe before my next class?

There are other contributing factors, but let’s stick with a simplified underlying question.

If the answer is yes, you go to Hagerty and get coffee. If the answer is no, you’re out of luck (or will need to try something else).

Let’s say there’s a long line at Hagerty- you won’t be able to get coffee in time for your next class. The no answer to the initial condition might lead you to test a new condition: Can I get coffee from O'Shaughnessy's Charron Family Commons before my next class?

If Hagerty works, you’ll never have to think about Charron. If Hagerty doesn’t work, but Charron does, you’re good to go! If Hagerty doesn’t work AND Charron doesn’t work, you might give up on coffee (or test similar conditions for other campus coffee locations).

The scenario we just walked through is an example of conditional execution.

Visual Representation#

Let’s represent this visually.

Remember in our initial scenario, you aren’t introducing additional conditions.

In the second scenario, you’re introducing an additional condition (based on the result of the first condition test).

Conditional Execution in Python#

In Python, this logic is expressed using the if, else, and elif syntax. From W3Schols, “Python Conditions”:

  • “An ‘if statement’ is written by using the if keyword”

  • “The elif keyword is Python’s way of saying ‘if the previous conditions were not true, then try this condition’”

  • “The else keyword catches anything which isn’t caught by the preceding conditions”

Pseudocode Examples#

Let’s walk through a few examples that introduce Python syntax.

# if statement- this is the initial condition
if INITIAL CONDITION:
  code that runs if the initial condition is true

Note

The code that is conditionally executed is nested under the if statement.

Let’s add another component.

# if statement- this is the initial condition
if INITIAL CONDITION:
  code that runs if the initial condition is true

else: # else statement- this does not introduce a new condition
  code that runs if the initial condition is flase

We can call that block of code an if-else statement.

But what if we want to introduce additional conditions? That’s where elif comes in.

# if statement- this is the initial condition
if INITIAL CONDITION:
  code that runs if the initial condition is true

elif SECOND CONDITION: # elif statement- this introduces a new condition
  code that runs if the second condition is true

Note that the elif condition will only be tested if the initial if condition is FALSE.

Putting it all together (with our Hagerty example):

if hagerty works: # initial condition
  get coffee from hagerty

elif charron works: # second condition
  get coffee from charron

else: # code that runs if no other conditions are true
  no coffee for you :\

Python Example#

Let’s look at an alternate example, this time with working Python syntax.

# assign variables
x = 5
y = 7

# if statement
if x > y:
    print("The value of x is greater than the value of y")

# else if statement
elif x == y:
    print("The value of x is equal to the value of y")

# else statement
else:
    print("The value of x is less than the value of y")

Recap#

  • if introduces and evaluates an initial condition. The lines of code indented under if will only run when the if statement is True

  • else-if or elif introduces and evaluates another condition. The lines of code indented under elif will only run when the elif statement is True

  • else does not introduce a new condition. The lines of code indented under else will only run when the preceeding if/elif statements are not true

Indentation#

As we continue to introduce more programming concepts and further our work in Python, we’re starting to see code blocks and indentation at work.

In Python syntax, code blocks are marked by indentation, or indented lines of code.

# an example of indentation in Python
if expression:
   statement
   statement
else:
   statement
   statement

Comprehension Check#

Clipboard icon if-then-else Comprehension Check

Application#

Q1: Write a program that asks a user to enter a color value and returns an output message comparing that value with your favorite color. This program will use a combination of the input() function, comparison operators, and if-then-else logic. Answer to this question includes program + comments that document process and explain your code.

Sample output for this program:

Your favorite color: green

My age: blue

We don't have the same favorite color.

Q2: Write a program that lets the user play a guessing game.

  • First, your program should set a number as the “correct answer”

  • Then, your program will ask the user to guess a number

  • Your program should give a message indicating whether the user’s guess is correct, too high, or too low

This program will use a combination of the input() function, comparison operators, and if-then-else logic. Answer to this question includes program + comments that document process and explain your code.

Sample output for this program (for a correct answer of 7:

You guessed 13.

This guess is too high.

Better luck next time!