{ } but with indentation, which makes this chapter an important introduction to how Python sees your code.
goto is not required. Modern structured programming — Python included — is built on this idea.
13.1 The Three Patterns of Flow
13.2 Indentation — How Python Groups Statements
Most programming languages use braces { } to mark the start and end of a block of statements. Python does not. Instead, Python uses indentation — the horizontal white-space at the start of each line. Every indented "step" is a sub-block of the line above.
13.2.1 The rules
- A line ending with a colon
:(afterif,for,while,def,class, …) starts a block. - The statements belonging to that block must all be indented to the same depth — conventionally 4 spaces.
- When the indentation returns to the previous level, the block has ended.
- Blocks can be nested — simply indent further each time.
if marks >= 33: # block starts print("Pass") # ← 4 spaces in if marks >= 75: # nested block print("Distinction") # ← 8 spaces in print("Well done") # back to 4 spaces — still inside outer if print("Result card printed") # ← 0 spaces — outside the if
13.2.2 Common indentation mistakes
| Mistake | Problem |
|---|---|
Missing indent after if | IndentationError: expected an indented block |
| Extra indent on a normal statement | IndentationError: unexpected indent |
| Inconsistent depth inside the same block (e.g., first line 4 spaces, next line 5) | IndentationError: unindent does not match any outer indentation level |
| Mixing tabs and spaces | Looks the same to the eye, but Python considers them different. Use your editor's "convert to spaces" option. |
13.3 Sequential Flow
The sequential (or default) flow is the simplest: statements execute in the order they are written, top to bottom, one after the other. No decisions, no repetition — just straight-line execution.
radius = float(input("Radius: ")) # line 1 runs first pi = 3.14159 # then line 2 area = pi * radius ** 2 # then line 3 print("Area =", area) # finally line 4The program does exactly four things in the exact order shown.
if or while, every Python program is purely sequential. All the programs from Chapter 8 through Chapter 11 followed this pattern.
13.4 Conditional Flow
A conditional (or decision) flow runs a different piece of code depending on whether a condition is True or False. It lets a program make choices. The Python keywords are if, elif (else-if) and else.
marks = int(input("Enter marks: ")) if marks >= 33: # is the condition True? print("Pass") # Yes-branch else: print("Fail") # No-branchOnly one of the two
print() calls runs — never both.
The full set of conditional forms (if, if-else, if-elif-else), flowcharts, nested-if and worked programs are covered in Chapter 14 — Conditional Statements.
13.5 Iterative Flow
An iterative (or loop) flow runs the same block of code several times. Python provides two loop keywords:
for— when you know how many times to repeat, or you want to loop over every item of a sequence.while— when you want to repeat as long as some condition staysTrue.
# for-loop form for i in range(1, 6): print(i) # 1, 2, 3, 4, 5 # equivalent while-loop n = 1 while n <= 5: print(n) n += 1
The full set of loop forms plus range() (start / stop / step), for–while conversion, break, continue and nested loops are covered in Chapter 15 — Iterative Statements.
13.6 Putting It Together
Real programs use all three patterns. A small example combining sequence, decision and repetition:
total = 0 # sequential — initialise for n in range(1, 11): # iterative — visit 1 to 10 if n % 2 == 0: # conditional — only even numbers total += n print("Sum of evens =", total) # sequential — show resultOutput:
Sum of evens = 30 (2 + 4 + 6 + 8 + 10)
13.7 Comparison at a Glance
| Flow | Keyword(s) | Runs the block… | Typical use |
|---|---|---|---|
| Sequential | (none — the default) | once, top to bottom | the default; calculations, printing |
| Conditional | if, elif, else | zero or one time, based on a condition | branching — pass/fail, discount/no-discount, grade |
| Iterative | for, while | zero or more times, as long as a condition holds | repetition — sums, tables, counting, processing each student |
📌 Quick Revision — Chapter 13 at a Glance
- Flow of control = order in which statements are executed.
- Three fundamental patterns (Böhm–Jacopini theorem, 1966): Sequential · Conditional · Iterative. These three together can express every algorithm.
- Sequential flow — statements execute top-to-bottom, one after another. The default of every Python program.
- Conditional flow —
if,elif,else. Run a block only when a condition isTrue; choose between two (or more) paths. - Iterative flow —
fororwhile. Repeat a block as long as a condition holds or for every item of a sequence. - Python uses indentation (not
{ }) to mark blocks. Every indented step is a sub-block of the line above. - Indent rules:
- A line ending with
:opens a block. - All statements inside must share the same depth (use 4 spaces by PEP 8).
- Return to the previous depth to end the block.
- Never mix tabs and spaces.
- A line ending with
- Mis-indentation →
IndentationError. A missing indent gives "expected an indented block"; an extra indent gives "unexpected indent". - Real programs combine all three flows — e.g., "loop through marks, add up only the passing ones, print the total".
- Details of conditional statements → Chapter 14 · details of loops → Chapter 15.