Flow of control
The order in which lines run is the flow of control. There are three kinds:
- Sequence: one line after another.
- Selection: choose a path (
if). - Repetition (iteration, looping): repeat a block (
while,for).
The lines inside each statement form a block and must be indented.
if and if-else
if condition:
block A # runs when True
else:
block B # runs when FalseThe condition is any expression that gives True or False, like m >= 33. Only one of the two blocks runs. else is optional: a plain if runs its block only when True and does nothing otherwise.
n = int(input("Number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
if-elif-else: many choices
Use elif ("else if") when there are more than two paths. Python checks from the top and runs the block of the first True condition, then skips the rest.
if m >= 90:
grade = "A"
elif m >= 75:
grade = "B"
elif m >= 60:
grade = "C"
elif m >= 33:
grade = "D"
else:
grade = "Retry"Order matters. If m >= 33 came first, 95 marks would get grade D.
An if inside another if is a nested if.
The while loop
i = 1 # 1. start
while i <= 5: # 2. check
print(i)
i += 1 # 3. changeThe block repeats while the condition is True. You must start the variable before, and change it inside, or the loop never ends (an infinite loop). Use while when you do not know in advance how many times to repeat, e.g. "keep asking until the password is right".
The for loop and range()
for takes each item of a sequence (range, string, list) in turn.
total = 0
for n in range(1, 6):
total += n
print(total) # 15range(stop): 0 to stop − 1. range(start, stop): start to stop − 1. range(start, stop, step): jumps by step; step can be negative to count down.
for ch in "IP":
print(ch) # I then Pwhile vs for
for: number of repeats known (a range or a list). while: repeat until a condition changes.
Extra: break leaves a loop at once; continue skips to the next round.
Try it: trace table
On paper, make a table with columns i and output. Trace i = 10; while i > 0: print(i); i -= 3 by hand. Then run it. Did you get 10, 7, 4, 1? Next, use the slider in step 6 of the 3D: find the smallest marks that give grade B.
Key formulas and definitions
- if cond: … else: … → exactly one block runs
- if … elif … elif … else → first True wins
- while cond: → start, check, change
- range(start, stop, step): stop not included; range(n) = 0 … n−1
Worked examples
1. Write a program to print whether a number is positive, negative or zero.
n = int(input()) if n > 0: print("Positive") elif n < 0: print("Negative") else: print("Zero")
2. What does this print? i = 1 while i < 10: print(i, end=" ") i *= 2
i = 1, 2, 4, 8 are all < 10, then i = 16 stops it. Output: 1 2 4 8
3. Find the sum of the first 10 natural numbers using for.
s = 0 for n in range(1, 11): s += n print(s) → 55. Check: 10 × 11 ÷ 2 = 55.
4. List the values from range(15, 2, -4).
Start 15, go down by 4, stop before 2: 15, 11, 7, 3.
5. Print the multiplication table of 7 (7 × 1 to 7 × 10).
for i in range(1, 11): print(7, "x", i, "=", 7 * i)
6. Find the factorial of 5 with a while loop.
f = 1; n = 5 while n > 1: f *= n; n -= 1 Trace: f = 5, 20, 60, 120. Output 120.
7. How many times does the body run? for k in range(3, 20, 5)
k = 3, 8, 13, 18 → 4 times.
Common mistakes
- Writing range(1, 10) and expecting 10 to be included.
- Forgetting to change the variable in a while loop, making it infinite.
- Putting a broad condition (m >= 33) before narrow ones (m >= 90) in an elif ladder.
- Missing the colon or indentation after if, elif, else, while or for.