Lesson 7: Conditional Logic
If
An "if" statement takes a condition, which must result in a true or false value. If the condition is true, a specific piece of code is run.
In the following example, the "if" statement checks the age of the cat. If the cat is older than seven, some text is printed out. Also note the colon after the "if" condition.
Code Examples
catAge = 4 food = "cat food" if catAge > 7: food = "senior cat food" print("This cat eats", food)
catAge = 8 food = "cat food" if catAge > 7: food = "senior cat food" print("This cat eats", food)
Indentation
In the above examples, notice the two spaces on the line following the "if" statement. This is known as indentation. In python, this is an important concept. If one or more lines are indented after an "if" statement (or any other statement), then these lines will run as part of that statement.
if else if
If the condition in the "if" statement is not met, "elif" and "else" blocks can also be used. The "elif" statement takes another condition, and if the first "if" condition is not met, then the "elif" condition is checked, and the code inside the "elif" statement is run.
There may also be an "else" that is executed if the condition is not true. If there are "if" and "elif" statements, they would both need to be false for the code inside the "else" block to be executed.
Code Examples
catAge = 0 food = "unknown" if catAge > 7: food = "senior cat food" else: food = "cat food" print("This cat eats", food)
catAge = 0 food = "unknown" if catAge > 7: food = "senior cat food" elif catAge < 1: food = "kitten food" else: food = "cat food" print("This cat eats", food)
Nested Conditional Statements
An "if" statement can even be nested inside another statement. The nested "if" statement would only be executed if the outer "if" or "else" statement applies.
See in the following example, that the because the cat's age is greater than 1, then next if condition is checked, whether the cat's age is greater than 7. Note what would happen if the cat's age was 0, then the food would be "kitten food", and there would be no further conditions to check.
Code Example
catAge = 7 food = "unknown" if catAge > 1: if catAge > 7: food = "senior cat food" else: food = "regular cat food" else: food = "kitten food" print("This cat eats", food)
Try It Out
Fast food restaurants across the globe have started introducing "Self order kiosks" in store. These kiosks allow the customers to avoid lengthy queues but still get exactly what they need. Conditional logic is used within these kiosks to enable customers to place their orders. Each customer can order very differently. Thanks to conditional logic (by using "if", "else" and "elif" statements) the kiosk is able to cater for whichever direction the customer chooses to go in. You want breakfast at 2pm? No problem! Or, you can order just a coffee, dessert, or a tasty meal. Conditional logic is what enables you to make your choices, your way.
So, lets explore. Most likely the first question you will be asked is - "Do you want to eat in or take out?"
Conditional logic kicks in as soon as you make your choice.
In this next scenario, the "elif" condition will be dominant as there will be several branches.
In order for the kiosk to be able to fully cater for your needs, the "elif" condition will be used. As you now know, the "elif" condition allows for several branches (multiple choices). Just say you choose a burger, the kiosk will most likely ask if you would like to make this a meal. If you select yes, then you will be brought through a series of options for your selection, fries, side, drink etc.
Towards the end of your order you will be given two options: continue your order or checkout. This is an example of the "else" condition where there are two branches (two choices) - one false and one true.
Try It Out - Create an if condition
Try It Out - Create an else condition
Try It Out - Create an elif condition
Fix It - Problem 1
Fix It - Problem 2
Resources