Python Chapter 3 — control statements
Priority key · Priorities guide emphasis; they do not remove taught scope.
Exam recall
Choose the condition, initialise state, update it, then return at the right level. Test equality, zero iterations and the stopping value.
Selection and repetition priority/high
An if/elif/else chain executes the first matching branch. Separate if statements can execute several branches. Order range tests so broad conditions do not hide more specific ones.
A for loop visits items of an iterable; range(start, stop, step) excludes stop. range(5) gives 0–4; range(5, 0, -1) gives 5–1. A while loop repeats while its condition is true, so the controlling state must eventually change unless intentional termination occurs another way. Both loops can execute zero times.
break exits the nearest loop; continue skips to its next iteration. A return exits the function, including any loops inside it. Indentation therefore changes meaning: a return inside a loop can stop after the first item.
Three common loop states priority/high
flowchart TD A[Initialise state] --> B{Another item?} B -->|No| F[Return completed result] B -->|Yes| C[Read current item] C --> D{Meets condition?} D -->|Yes| E[Update count or sum or result] D -->|No| B E --> B
The result is returned after all required items are considered. A return inside the loop ends the function, not just the current iteration. This is why a correctly written condition can still produce an incomplete answer.
| State | Initial value | Update |
|---|---|---|
| Count of matches | 0 | Add 1 when condition holds. |
| Sum of matching values | 0 | Add the value when condition holds. |
| Product | 1 | Multiply by the value. |
With nested loops, identify whether an accumulator is for one row or the whole grid. Reset a row total inside the outer loop but before the inner loop. Resetting a grand total inside the outer loop loses earlier rows.
Worked example — include the boundary priority/high
Contract: return the sum of values at least limit. The output is one number.
def sum_at_least(values, limit):
total = 0 # Running sum of accepted values.
for value in values:
if value >= limit: # “At least” includes the boundary itself.
total += value # Add the value, not 1.
return totalvalue in [4, 7, 7, 2] | value >= 4 | total after iteration |
|---|---|---|
| 4 | True | 4 |
| 7 | True | 11 |
| 7 | True | 18 |
| 2 | False | 18 |
Using > would omit 4. Returning count, sum_values would give a tuple instead of the required number. The required result is 18.
Repeated input validation priority/medium
def read_mark():
while True:
text = input("Mark 0-100: ")
try:
mark = int(text)
except ValueError: # Retry only the expected conversion failure.
continue
if 0 <= mark <= 100: # Conversion succeeded; now check the range.
return markConversion failure retries; successful conversion outside the range also retries; only a valid integer returns. A flag is a Boolean recording a condition such as “found”; a sentinel is a special data value terminating processing. Neither is the same as a count of matches.
Trace deliberately priority/high
Write a row after each meaningful update. Include the condition, loop variable and state being changed. For while, include the final failed condition check if relevant. Do not confuse how many times a condition is checked with how many times the body runs. Use a tiny input whose result you can compute independently.
Practice
Exam focus: HCI 2025 Q8’s factors/perfect-number tasks and HCI 2024 Q9’s grid tasks require loop bounds, conditions and accumulators. HCI 2023 Q8 also requires boundary-aware movement. First decide whether the loop is controlled by a known count or by a condition; then state exactly what must change to make progress.
Common error test: if the requirement is “greater than 4”, include4 and 5. If the sentinel is−1, include another negative value such as−2. These inputs separate the required condition from tempting broader or narrower ones.
14A — original. Write count_large(values, limit) returning only the number of values strictly greater than limit. Give results for ([4, 7, 7, 2], 4), ([4], 4) and ([], 4).
14B — original. readings is a list of integers ending optionally with sentinel −1. Write sum_before_stop(readings) returning the sum before the first −1, ignoring it and everything afterwards. Other negative integers are valid readings. Test [3, -2, -1, 100] and [-1].
Hints
14A: the boundary value must fail a strict comparison. 14B: the stopping rule is equality to −1, not “any negative”.
14C — original, trace. Trace this loop for values = [2, 5, 1, 4]: start total = 0; for each value, if total < 4, add the value, otherwise subtract 1. Record total after every iteration. Explain why the decision is not based on the current value alone.
Revision checklist
- 14.1 Construct Boolean expressions with correct grouping and comparisons.
- 14.2 Write if/elif/else chains and explain mutually exclusive branches.
- 14.3 Trace for and while loops, including zero iterations.
- 14.4 Use range(start, stop, step) correctly for positive and negative steps.
- 14.5 Write count-controlled and condition-controlled loops.
- 14.6 Use counters, accumulators, flags and sentinels appropriately.
- 14.7 Trace and write nested loops.
- 14.8 Prevent off-by-one errors and infinite loops.
- 14.9 Implement repeated input checking with a clear exit condition.
- 14.10 Test loops at the smallest permitted input and boundary values.
Visual revision mindmap

Open this mindmap and its text version · All 21 mindmaps
Your mindmap framework
Centre: Python Chapter 3 — control statements. Build the six branches below. For each subbranch, add a short definition, a labelled sample and one exam trap from memory; then check the chapter.
flowchart LR C["14 • Revision map"] C --> B0["Boolean decisions"] C --> B1["Loop choice"] C --> B2["Loop state"] C --> B3["Nested control flow"] C --> B4["Validation and termination"] C --> B5["Visual checks and mistakes"]
-
Boolean decisions
- Translate strict versus inclusive comparison.
- Parentheses and precedence.
- if/elif/else: first matching branch.
- Separate if statements can execute several branches.
-
Loop choice
- for: traverse iterable or known range.
- while: repeat while a condition holds.
- range start/stop/step; excluded stop.
- Zero iterations and final failed condition.
-
Loop state
- Counters start at 0.
- Sums start at 0; products at 1.
- Flags record conditions.
- Sentinels end processing; test before adding when excluded.
-
Nested control flow
- Outer versus inner responsibility.
- Row accumulator versus grand total.
- break exits nearest loop; continue skips iteration.
- return exits the function, including loops.
-
Validation and termination
- Convert safely; catch expected exception.
- Apply range after successful conversion.
- Change controlling state or return on success.
- Avoid hiding unrelated errors with broad exception handling.
-
Visual checks and mistakes
- Table: item, state before, condition, state after.
- Draw branch arrows for total-dependent decisions.
- Test equality, empty input, immediate sentinel and other negative data.
- Avoid: wrong reset level; premature return; condition never changes.
Close the notes and test the map: explain one branch aloud, sketch its sample, then answer a linked practice question. Mark any missing link to revisit.
Source trail
9569 §§1.1.1,1.5.5; school Control Statements.
HCI 2023 Q8; 2024 Q9; 2025 Q8; school Tutorial 3.
Source guide records provenance and original-paper locations.