Three ways for a program to proceed
Every program ever written is built from three control structures, and the syllabus names all three. Sequence is one statement after another. Selection chooses between alternatives. Iteration repeats.
In Python, which statements belong to a structure is decided by indentation rather than by brackets. That makes the code readable — and makes a misplaced space a genuine bug rather than an untidiness.
Indentation is part of the language
In most languages, indentation is decoration. In Python it is grammar: everything indented under an if runs only when the condition is true, and moving a line out by four spaces changes what the program does. A statement indented one space too far produces an IndentationError; one indented too little runs when it should not, which is worse because it produces no error at all.
Selection: if, elif, else
An if tests a condition and runs its block only when the condition is True. elif offers another condition to try if the first failed, and else catches everything remaining.
The order matters. Python tests each condition in turn and stops at the first that is True, so a broad condition placed early prevents any later, narrower one from ever being reached.
This grading code always prints "C". Explain why. if m >= 40: print("C") / elif m >= 60: print("B") / elif m >= 80: print("A")
- A mark of 90 is tested against the first condition,
m >= 40.Python evaluates the conditions strictly in order. - 90 is indeed at least 40, so the condition is True.The test does not know that a later branch would be a better match.
- The block runs and the whole chain is abandoned — the elif branches are never examined.That is what elif means: try this only if nothing above matched.
- The fix is to order the conditions from the most restrictive downward: 80 first, then 60, then 40.Then a mark of 90 meets the strictest test first and is graded correctly.
The broadest condition is tested first, so it matches everything. Order from most restrictive to least.
Iteration: for and while
A for loop repeats a known number of times, or once for each item of a collection. Use it when the count is fixed in advance: every student in a list, every number from 1 to 10.
A while loop repeats as long as a condition remains true, and the number of repetitions is not known when the loop begins. Use it when the stopping point depends on something that happens during the loop — a user typing "quit", a value falling below a threshold.
Select Loop total and step through. range(1, 5) gives 1, 2, 3, 4 — the loop body runs four times, not five, because the second number is where it stops rather than the last value used.
The infinite loop
A while loop whose condition never becomes false runs for ever. The usual cause is forgetting to change the variable the condition depends on: while count < 10: with nothing inside that increases count. Before writing a while loop, ask what changes inside it that will eventually stop it — and make sure that change is not inside an if that might never run.
Nesting, and choosing the right structure
Control structures can contain each other. A loop containing an if tests each item; a loop containing a loop processes a grid, running the inner loop completely for every single pass of the outer one — so nested loops of 10 and 10 execute the inner body 100 times.
Two keywords occasionally help. break leaves a loop immediately, useful when the answer has been found and further searching is pointless. continue skips the rest of the current pass and moves to the next. Both should be used sparingly: a loop with several exits is harder to reason about than one with a well-chosen condition.
Write a program that keeps asking for a number until the user enters one between 1 and 100, then prints it.
- The number of attempts is unknown, so this is a while loop, not a for loop.The user may get it right first time or on the twentieth attempt.
- Read a value once before the loop:
n = int(input("Enter 1-100: ")).The condition needs something to test on its first evaluation. while n < 1 or n > 100:— repeat while the value is invalid.The condition describes when to keep looping, which is the opposite of the acceptance rule.- Inside: print a message and read again, so
nchanges each pass.Without a new input the condition never changes and the loop never ends. - After the loop,
print(n).Reaching this line means the condition failed, so the value must be valid.
A while loop testing for the invalid range, reading a new value each pass.
Before you leave this chapter
- Sequence, selection, iteration — every program is built from these three.
- Indentation decides what belongs to a block, and is part of the language.
- Order if/elif conditions from most restrictive to least, or the broad one matches everything.
- for = known number of repeats; while = unknown, condition-controlled.
- range(a, b) stops before b, and a while loop must change the variable its condition tests.