Lesson 2 of 5 · Open
If, then, else
How a set of instructions takes a different path depending on the case.
An online store says: “Shipping is free on orders of €50 or more, otherwise it costs €5.” So for every order, the store has to decide which case applies. Until now, an algorithm always ran the same way, one step after another. Here the next step depends on what is in the cart.
The store checks a condition: a statement that is either true or false. “The order total is at least €50” is such a statement. At €58 it is true, at €44 it is false. There is no third option.
“It looks like rain,” on the other hand, is not a condition. Two people see the same sky and judge it differently. “The weather app shows a chance of rain of at least 60%” is one: everyone reads the same number. Like every step of an algorithm, a condition has to be unambiguous.
Quick check
Is “The battery is almost empty” a condition?
Two paths, exactly one runs
A branch checks a condition and then takes one of two paths. It is written with the words if, then, and else: if the condition is true, then the steps after “then” follow. Else, that is, if it is false, the steps after “else” follow. After that, both cases go on with the same next step.
New terms
- condition
- a statement that is either true or false, with no room for interpretation
- true
- the condition holds, for example €58 for “at least €50”
- false
- the condition does not hold, for example €44 for “at least €50”
- branch
- checks a condition and takes one of two paths: if true, then …, else …
Example
Shipping on €58
The store's rule as a branch: if the order total is at least €50, then shipping costs €0, else it costs €5. The cart holds items worth €58. The picture shows the path that actually runs.
- Check the condition: €58 is at least €50. It is true.
- The steps after then run: shipping costs €0.
- The steps after else are skipped.
- To pay: €58 + €0 = €58
If the cart only holds items worth €44, the condition is false. Then the steps after else run: €44 + €5 = €49. This time the path after then is skipped.
If without else
Example
Low power mode on a phone
A phone is set up like this: if the battery is below 20%, then turn on low power mode. There is no “else” here.
- Battery at 14%: the condition is true, low power mode turns on.
- Battery at 63%: the condition is false. Nothing happens, and the phone keeps working as before.
If there is no “else,” the path for the case “false” is empty. Exactly one of the two paths still runs, it is just that one of them does nothing.
Right on the boundary
With an order total of exactly €50, a single word decides. “At least €50” includes €50, and so does “at most €50.” “More than €50” and “less than €50” leave it out. The table shows three order totals and two conditions:
| Order total | At least €50 | More than €50 |
|---|---|---|
| €49 | false | false |
| €50 | true | false |
| €51 | true | true |
Quick check
The battery is at exactly 20%. Is “The battery is below 20%” true?
Typical mistake
Carrying out the steps under “else” as well after the ones under “then.” That cannot be right: for the €58 cart, the store would charge €0 for shipping and then €5 after all. The €50 boundary would have no effect. A branch chooses, it does not run through both paths one after the other.
Remember
Tasks
Task 1 · Sorting
Condition or not? Sort each line.
6 cards left
Task 2 · Several right
The order total is exactly €75. Which conditions are true?
Task 3 · Choice
Parking garage: if you park for at most 30 minutes, then it is free, else it costs €2. You park for exactly 30 minutes. What runs?
Go deeper
Task 4 · Fill in · optional
Complete the text about the phone lock.
“The password entered is correct” is a . If it is , the phone unlocks. If it is false, the steps after run, and the phone stays locked.
Task 5 · Pairs · optional
When does the path after else run? Match each condition with the case in which it is false.
Task 6 · Input · optional