The flow of control is the order in which Python runs your statements. By default lines run top-to-bottom; with the right keywords we can choose which lines to run, or repeat them.
| Pattern | Behaviour | Keywords |
|---|---|---|
| Sequential | Lines run once, top to bottom | (default, no keyword) |
| Conditional | Choose which block runs | if, elif, else |
| Iterative | Repeat a block several times | for, while |
Every program is built from these three patterns combined — an idea sometimes called structured programming. A 1966 result by Bohm and Jacopini proved any algorithm can be written using just these three.
if → conditional; for / while → iterative; anything else → sequential.Most languages use { } to mark a block. Python uses indentation — the leading spaces — to show which lines belong inside an if, a loop, or a function.
if marks >= 33: print("Pass") # inside if print("Well done") # still inside print("End") # OUTSIDE, alwaysThe first two indented lines run only when the condition is True. The fourth line is back at the outer level, so it always runs.
| Mistake | Result |
|---|---|
| No indent after a colon line | IndentationError |
| Mixed 2-space and 4-space lines | IndentationError |
| Tabs and spaces mixed | TabError |
Forgotten colon at end of if | SyntaxError |
| Random extra indent at outer level | IndentationError |
The default behaviour: every statement runs once, from top to bottom. No loops, no decisions.
Most short programs follow Input → Process → Output:
A pure-sequential program just walks through these steps once and stops.
l = int(input("L: ")) b = int(input("B: ")) area = l * b print("Area =", area)Four lines, four steps — in order. Pure sequential flow.
p, r, t = 5000, 8.5, 3 si = (p * r * t) / 100 print("SI =", si)
c = float(input("C: ")) f = c * 9/5 + 32 print(c, "C =", f, "F")Three lines — input, process, output.
if or loops are mostly sequential between those blocks. Sequential is the default; the others are special cases that depart from it.A conditional statement lets the program pick one of several blocks based on a True/False test. Python uses three keywords: if, elif, else.
| Form | Use when… |
|---|---|
if | One block, run only when condition holds |
if-else | Exactly two paths, choose one |
if-elif-else | Three or more paths, only one runs |
marks = 47 if marks >= 33: print("Pass") else: print("Fail")
m = 67 if m >= 90: g = "A" elif m >= 75: g = "B" elif m >= 60: g = "C" else: g = "D" print(g) # CPython tests top-to-bottom; the first True branch runs, the rest are skipped.
Three separate if statements run their blocks independently — multiple can be True. An if-elif-else ladder runs at most one block. Pick based on whether the conditions are mutually exclusive.
if/elif/else line ends with a colon. Only one branch runs per execution. Order matters — put the most specific condition first.An iterative statement repeats a block several times. Python provides for and while.
for — counted repetitionUse when you know how many times to run, or want to walk through every item of a sequence.
for i in range(1, 6): print(i, i*i)Prints 1 1, 2 4, 3 9, 4 16, 5 25.
range(start, stop, step) generates numbers from start up to but not including stop:
range(5) → 0, 1, 2, 3, 4range(1, 6) → 1, 2, 3, 4, 5range(0, 10, 2) → 0, 2, 4, 6, 8range(10, 0, -1) → 10 down to 1while — condition-controlled repetitionUse when you don't know the count in advance.
n = 5 while n > 0: print(n) n -= 1Prints 5, 4, 3, 2, 1. The loop ends when
n > 0 becomes False.| When to use | for | while |
|---|---|---|
| Count is known up front | Yes | OK |
| Walking through list / string | Yes | No |
| Repeat until something changes | No | Yes |
| Risk of running forever | No | Yes |
while condition never becomes False. Always make sure the variable being tested actually changes inside the loop.Real programs combine all three patterns. A small login script is a good demo:
attempts = 0 while attempts < 3: # iterative pwd = input("PWD: ") if pwd == "vm123": # conditional print("Granted") break attempts += 1 else: print("Locked") # sequential
The while drives repetition, if chooses what runs each round, the rest is plain sequential code.
| Aspect | Sequential | Conditional | Iterative |
|---|---|---|---|
| Block runs | Once | 0 or 1 time | 0 or many |
| Decided by | Position | Condition | Condition / count |
| Keywords | (none) | if/elif/else | for, while |
| Risk | Forgotten step | Wrong condition | Infinite loop |
For each phrase, identify the flow type:
range(2, 10, 3) produces:
if-elif-else run per execution?
for gives:
for i in range(3): print(i)Output:
for i in range(1, 6, 2): print(i, end=" ")Output:
n = 3 while n > 0: print(n, end=" ") n -= 1Output:
x = 7 if x % 2 == 0: print("even") else: print("odd")Output:
total = 0 for i in range(1, 5): total += i print(total)Output:
m = 80 if m >= 90: g = "A" elif m >= 75: g = "B" else: g = "C" print(g)Output:
for ch in "abc": print(ch * 2, end="-")Output:
i = 1 while i < 20: print(i, end=" ") i *= 2Output:
Each snippet has exactly one error. Mark the line number and write the corrected line.
1 if x > 0 2 print("+ve")Wrong line: Correction:
1 for i in range(5): 2 print(i)Wrong line: Correction:
1 n = 5 2 while n > 0: 3 print(n)Wrong line: Correction:
1 age = 20 2 if age >= 18: print("vote") 3 elseif age < 18: print("wait")Wrong line: Correction:
1 m = 50 2 if m >= 33: print("Pass") 3 else m < 33: print("Fail")Wrong line: Correction:
1 for i in range(1, 5): 2 print(i) 3 print("done")Wrong line: Correction:
for loop and a while loop with one example each.
Each program should read input where required and produce a clear output. Use a separate sheet if you need more space.
A = 3.14 * r * r and C = 2 * 3.14 * r. (Pure sequential.)
if-elif-else to print whether it is positive, negative, or zero.
for loop to print the multiplication table of any number entered by the user, from 1 to 10. Output format: 5 x 1 = 5, 5 x 2 = 10, …
while loop to print the numbers from 10 down to 1 on one line, separated by spaces, followed by "Lift off!".
for loop.
(i) total = a + b (ii) if n % 2 == 0: print("Even") (iii) for i in range(5): print(i)