- How sequencing, selection and repetition control the order a program runs in.
- How to choose between count-controlled loops, condition-controlled loops and iteration over a data structure.
- How Python 3 uses indented code blocks.
- How to write clearer programs using single entry and single exit points.
A statement is one instruction in a program, such as assigning a value, asking for input or printing output.
Program flow means the order in which statements are executed. By default, Python runs statements from top to bottom, but control constructs can change that order.
Control construct
A control construct is a programming structure that controls which statements run, how many times they run, and in what order.
This diagram gives the big picture of the control constructs you choose from when writing a program.

Sequencing means statements are executed one after another in the order they are written.
balance = 20
balance = balance - 6
balance = balance * 2
print(balance)
The second line uses the current value of balance, then stores the new value back into balance.
Tracing a short sequence
- The first statement stores 20 in
balance.
- The second statement uses that value, subtracts 6, and stores 14 back in
balance.
- The third statement uses the updated value, doubles it, and stores 28.
- The final statement outputs 28.
Sequence matters
Changing the order of statements can change the result, especially when variables are updated.
Selection means the program chooses between different paths.
In Python, selection is written using if, elif and else.
mark = int(input("Enter mark: "))
if mark >= 80:
grade = "distinction"
elif mark >= 50:
grade = "pass"
else:
grade = "resit"
print(grade)
A condition is an expression that evaluates to either True or False, such as mark >= 50.
You can combine conditions using logical operators. In algorithms you may see AND, OR and NOT; in Python 3 these are written as and, or and not.
Choosing a selection branch
For mark = 63:
- Test
mark >= 80. This is false, so the if block is skipped.
- Test
mark >= 50. This is true, so grade = "pass" runs.
- Because one branch has been chosen, the
else branch is not run.
Testing the same value twice in the wrong order
If you test mark >= 50 before mark >= 80, then a mark of 90 will be classified as "pass" before the program ever reaches the distinction test. Put the most specific or highest boundary first when using elif.
Repetition means running the same block of code more than once. A repeated block is often called a loop.
A count-controlled loop is used when the number of repetitions is known before the loop starts.
In Python, this is usually a for loop with range().
total = 0
for week_number in range(4):
hours = int(input("Hours this week: "))
total = total + hours
print(total)
This repeats exactly 4 times.
range stops before the end value
range(4) gives 4 repetitions: 0, 1, 2, 3. range(1, 4) gives 1, 2, 3, so it only repeats 3 times.
A condition-controlled loop is used when the program should keep repeating while a condition is true, or until a condition becomes true.
In Python, this is usually a while loop.
valid_mark = False
while not valid_mark:
mark = int(input("Enter a mark from 0 to 100: "))
if mark >= 0 and mark <= 100:
valid_mark = True
else:
print("Invalid mark")
This loop may run once, many times or, if the condition is never changed properly, forever.
Choosing a loop type
A program must collect marks for 10 students, but each mark must be re-entered until it is between 0 and 100.
- The program knows there are exactly 10 students, so the outer loop should be count-controlled.
- The program does not know how many attempts each student will need to enter a valid mark, so the validation loop should be condition-controlled.
- The condition-controlled loop belongs inside the count-controlled loop, because each student needs their own valid mark.
Choosing the right construct
- Use sequence for steps that always happen in order.
- Use selection when the program must choose a path.
- Use count-controlled repetition when you know how many times to repeat.
- Use condition-controlled repetition when repetition depends on a condition changing.
- Use iteration over a data structure when you need to process every stored item.
A data structure is a way of storing multiple values together, such as a Python list.
Iteration over a data structure means visiting each item in turn and applying the same logic to it.
marks = [42, 50, 79]
pass_count = 0
for mark in marks:
if mark >= 50:
pass_count = pass_count + 1
print(pass_count)
This is different from for number in range(3). Here, the loop variable mark directly takes each value from the list.
Counting passes in a list
For marks = [42, 50, 79]:
- Start with
pass_count as 0.
- Visit 42 first. It is not at least 50, so the count stays 0.
- Visit 50 next. It is at least 50, so the count becomes 1.
- Visit 79 last. It is at least 50, so the count becomes 2.
A code block is a group of statements controlled by the same construct. In Python, indentation shows which statements belong to a block.
if temperature > 30:
print("Hot day")
print("Drink water")
print("Weather check complete")
Only the two indented print() statements are inside the if block. The final statement is not indented, so it runs after the selection has finished.
Indentation changes the meaning
In Python, indentation is not decoration. If a statement is indented under an if, for or while, it belongs to that block and may run differently.
A single entry point means there is one clear way into a block or subprogram. A single exit point means there is one clear way out.
A subprogram is a named section of code that can be called from elsewhere. In Python, subprograms are written using def. A function returns a value; a procedure-style subprogram performs actions and may not return a useful value.
def grade_from_mark(mark):
grade = ""
if mark >= 80:
grade = "distinction"
elif mark >= 50:
grade = "pass"
else:
grade = "resit"
return grade
This function has one entry point: the first line inside the function. It also has one main exit point: return grade.
Writing a single-exit function
- Decide the function’s job: convert one mark into one grade.
- Use selection to store the correct value in
grade, rather than returning immediately from each branch.
- Return
grade once at the end, so the exit point is easy to find and trace.
Structured programs are easier to trace
Single entry and single exit points make programs easier to read, test and debug because the flow of control is predictable.
Here is a small Python 3 program that uses the constructs appropriately.
def grade_from_mark(mark):
grade = ""
if mark >= 80:
grade = "distinction"
elif mark >= 50:
grade = "pass"
else:
grade = "resit"
return grade
marks = []
student_count = int(input("How many students? "))
for student_number in range(student_count):
valid_mark = False
while not valid_mark:
mark = int(input("Enter a mark from 0 to 100: "))
if mark >= 0 and mark <= 100:
valid_mark = True
else:
print("Invalid mark")
marks.append(mark)
pass_count = 0
for mark in marks:
if mark >= 50:
pass_count = pass_count + 1
print("Passes:", pass_count)
for mark in marks:
print(mark, grade_from_mark(mark))
This program uses:
- Sequencing: setup happens before input, input happens before output.
- Selection: marks are checked and grades are chosen.
- Count-controlled repetition: the program repeats once per student.
- Condition-controlled repetition: invalid marks are requested again.
- Iteration over a data structure: every mark in the list is processed.
- Single entry/exit subprogram style:
grade_from_mark() has one clear return point.
In the exam
- Match the construct to the wording: “exactly 5 times” suggests count-controlled repetition; “until valid” suggests condition-controlled repetition; “for each item” suggests iteration over a data structure.
- Check indentation carefully in Python questions, because it decides which statements belong inside a block.
- When writing a subprogram, make its purpose clear, use meaningful variable names, and avoid scattered exit points.
Check yourself
- When would you choose a
while loop instead of a for loop with range()?
- What is the difference between iterating over
range(5) and iterating over a list such as marks?
- Why can multiple exit points make a subprogram harder to trace?