As we write computer programs, there will be plenty of situations where we want the computer to choose between different options for a specific reason. For example, we want the program to decide whether to charge for an adult ticket or a child one depending on a age. Or in a more serious scenario, whether to approve a large bank loan for a customer depending on existing financial performance. We can use a type of statements called selection statements to pick particular code.
TODO: Add comic of computer picking between two doors. "Which door do I take?"
When a computer needs to choose between several options, we must provide it with a statement to evaluate based on the current conditions. The statement either needs to be evaluated as true or false. Now this may sound complicated, but we do this all the time. An example would be if I say "I'll go to the beach if it's sunny" (this is the statement), and then I look outside and see it is snowing (this is the condition). I think to myself whether my statement is true or false based on the conditions, and in this case my statement is false so I choose to stay home and do programming. Everyday there are many scenarios that you encounter that could be represented with selection statements:
- Have I added enough milk into my cereal bowl?
- Am I late for the bus?
- Have I done all my chores?
- Do I have enough money to buy this ice cream?
These are pretty general examples, but these highlight a lot of the different components used in selection statements.
Exercise: Scenarios and Situations
List three scenarios that you have come across today, and the outcome for these decisions. Example: I needed to decide whether I was late for the bus. If I wasn't late then I continued to the bus stop, however if I was then I asked Mum for a ride to school.
Let's think about selection statements in programming more professionally. The statements the computer needs to evaluate must either be true or false. The statement 17 is greater than 5 is true, there is no denying that. The value true or false can actually be stored in a variable, called a boolean variable and is written as "True" or "False". I will now start refering to the boolean values with a capital T or F so you know when I'm talking about this special value. For most of your selection statements, you will be using strings and numbers so lets look at a few different tools that you can use.
< and > - The greater and lesser than signs (if you forget which way they go, think of it as a crocodile mouth and it always points to the larger number)
<= and >= - These symbols are for greater than or equal to, and lesser than or equal to.
== - Equal to, to check if a number or a string is exactly equal to something. Remember one equal sign if for when you want to make a variable equal to something, but two equal signs is for when you want to check if something is equal to something.
Here are some examples so you can see them in action:
54 < 59 evaluates to True
"Apple" == "Banana" evaluates to False
4 >= 23 evaluates to False
9000 <= 9001 evaluates to True
TODO: Check if it's common practice to have the larger number on one side
Evaluate if the following statements are true or false, if you don't know the result write down what you think the answer could be.
- 55 < 22
- 34433 <= 34343
- age = 16 age > 12
- 0 >= 0
- "Batman" == "batman"
TODO: Could be the place to describe different types
The most basic type of selection statement is the if statement, which simply does something if a
if temperature > 32: print("It is very warm today")
The first line is the
Python uses indentation to show what is
TODO: Spaces or tabs?
TODO: Talk about combining multipe statements together.
Try and follow through a selection block with different values.
Try and create selection blocks from flowcharts