Making Decisions

Branch your program's behaviour with if, elif and else.

  • Define and explain Making Decisions in your own words
  • Use key terms such as selection accurately
  • Apply what you have learned to new examples and questions
  • Avoid the common mistakes learners make with this topic

This lesson focuses on Making Decisions: branch your program's behaviour with if, elif and else.

Definition: Making Decisions

Branch your program's behaviour with if, elif and else.

Key ideas

Selection lets programs decide

An if statement asks a true-or-false question and runs different code for each answer. elif chains let you test several possibilities in order, and else catches everything left over. Getting the order of conditions right matters — the first true condition wins.

Programs are input, process, output

Nearly every program follows the same shape: take something in, do something with it, and show a result. A quiz program inputs answers, processes them against the correct ones, and outputs a score. Seeing this pattern helps you plan any program before you type.

Key term — selection: Choosing which instructions to run based on a condition, using if, elif and else. Also called branching.

Trace the Total

What does this program print? First line: total = 0. Next two lines: for n in range(1, 6): then total = total + n (indented). Last line: print(total).

range(1, 6) produces the numbers 1, 2, 3, 4, 5 — the stop value 6 is not included. Start: total = 0. First iteration, n = 1, so total becomes 0 + 1 = 1. Second iteration, n = 2: total becomes 1 + 2 = 3. Third iteration, n = 3: total becomes 3 + 3 = 6. Fourth iteration, n = 4: total becomes 6 + 4 = 10. Fifth iteration, n = 5: total becomes 10 + 5 = 15. The loop ends and print(total) outputs the final value.

Answer: The program prints 15 — the sum of the numbers 1 to 5.

Common mistakes
  • Forgetting that range stops one short Correction: range(1, 6) gives 1 to 5 — always check whether your stop value is included (it never is).
  • Mixing up = and == Correction: = stores a value while == compares — writing if x = 5: is a syntax error in Python; conditions need the double equals, as in if x == 5:.

Practice

What does this print? Line 1: x = 3. Line 2: y = 4. Line 3: print(x + y * 2)
Remember BIDMAS: multiplication before addition.

11 — y * 2 is 8, plus x (3) gives 11.

What does this print? Line 1: for i in range(3): Line 2 (indented): print(i)
Where does range(3) start, and where does it stop?

0, then 1, then 2, each on its own line — range(3) starts at 0 and stops before 3.

What is the value of count after this loop? Line 1: count = 0. Line 2: while count is less than 3: Line 3 (indented): count = count + 1
Trace each pass: does the condition stay true?

3 — the loop runs while count is 0, 1 and 2, adding 1 each time, then stops when count reaches 3.

What does this print? Line 1: name = 'Ada'. Line 2: print('Hello, ' + name)
The + joins two strings together.

Hello, Ada.

Quick check

Making Decisions — quick check

Which of these best defines "selection"?

Choosing which instructions to run based on a condition, using if, elif and else. Also called branching.

Write a function double(n) that returns twice its input, then state what double(7) returns.

def double(n): followed by an indented line return 2 * n — and double(7) returns 14.

Fix the bug: if age = 16: print('old enough for a moped')

if age == 16: — a single = is assignment and causes a syntax error here; == is the comparison you need.
Key takeaways
  • Making Decisions: branch your program's behaviour with if, elif and else.
  • Selection lets programs decide: An if statement asks a true-or-false question and runs different code for each answer.
  • sequence: Instructions carried out one after another in the order they are written — the default behaviour of every program.
  • Watch out for: forgetting that range stops one short