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.

  1. Check the condition: €58 is at least €50. It is true.
  2. The steps after then run: shipping costs €0.
  3. The steps after else are skipped.
  4. 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.

  1. Battery at 14%: the condition is true, low power mode turns on.
  2. 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 totalAt least €50More than €50
€49falsefalse
€50truefalse
€51truetrue

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

A condition is a statement that is either true or false. A branch checks it: if it is true, the steps after then follow, otherwise the ones after else.

Tasks

Three tasks complete the lesson. The order does not matter, the text above stays where it is.

Task 1 · Sorting

Condition or not? Sort each line.

Drag every card into its group, or tap the group.

6 cards left

The trip takes longer than 45 minutes

Task 2 · Several right

The order total is exactly €75. Which conditions are true?

Several answers are right. Pick all of them.

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?

One answer is right.

Go deeper

Three more tasks, optional. They do not count for completing the lesson.

Task 4 · Fill in · optional

Complete the text about the phone lock.

Tap a word, it goes into the marked gap.

“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.

Drag every card to its place, or tap it and then the place.

  • at least €25
  • more than €25
  • less than €25
  • at most €25

Task 6 · Input · optional

Free shipping on €40 or more, else €6. Your cart holds €33, and you add one more item for €8. What do you pay in total?

Enter the result.

€

Back to the chapter