- Understand what a fixed loop is and when it is the appropriate loop to use
- Write fixed loops using SQA pseudocode and Python syntax
- Trace through a fixed loop to predict its output, including loops with running totals
- I can explain what a fixed loop is and give an example of when one is needed
- I can write a correct fixed loop in both SQA pseudocode and Python
- I can trace a fixed loop step by step and state the final output
- I can use a fixed loop with a running total to solve a programming problem
Answer before the lesson begins. These check prior knowledge from earlier lessons — it's fine if you're unsure.
1. What is the value of count after these three lines run?
count = 0 count = count + 1 count = count + 1
2. Which arithmetic operator gives the remainder after division?
3. What does print(str(42) + " points") output?
Key vocabulary
range(1, 6) gives 1, 2, 3, 4, 5.Fixed Loops
What is a fixed loop?
A fixed loop (also called a count-controlled loop) repeats a block of code a specific, predetermined number of times. The key feature of a fixed loop is that the number of iterations is known before the loop begins — you do not need to check a condition to decide when to stop.
Fixed loops are the right choice when you know exactly how many repetitions are needed. Examples: display a 10-times table (always 10 lines), collect 5 exam scores from the user (always 5), or generate 100 random numbers. If you do not know in advance how many times to repeat — for example, keep asking until the user enters a valid score — you would use a conditional loop instead (SDD10).
Pseudocode syntax
The SQA reference language uses the following structure for a fixed loop:
FOR <counter> FROM <start> TO <end> DO
<loop body>
END FOR
The counter variable starts at <start> and increases by 1 after each iteration. The loop body executes once for each value from start to end, inclusive. For example, FOR i FROM 1 TO 5 DO runs with i = 1, 2, 3, 4, 5 — that is 5 iterations.
Python syntax
In Python, a fixed loop uses for together with the built-in range() function:
for counter in range(start, stop):
# loop body (indented by 4 spaces)
range(start, stop) generates integers from start up to, but not including, stop. This is a critical difference from pseudocode:
| Python | Values produced | No. of iterations |
|---|---|---|
range(5) | 0, 1, 2, 3, 4 | 5 |
range(1, 6) | 1, 2, 3, 4, 5 | 5 |
range(1, 11) | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 | 10 |
range(0, 10, 2) | 0, 2, 4, 6, 8 | 5 (step of 2) |
When only one argument is given — range(n) — Python starts at 0 and produces n values (0 to n−1). This is the simplest way to loop exactly n times.
Using a running total inside a loop
One of the most important patterns in programming is using a fixed loop to accumulate a running total. The variable that holds the total must be initialised before the loop (not inside it), otherwise it resets to zero on every iteration:
total = 0 # initialise BEFORE the loop
for counter in range(1, 6): # counter takes values 1, 2, 3, 4, 5
total = total + counter # add counter to the running total
print(total) # prints 15
Tracing a fixed loop
When tracing a loop in an exam, draw a table showing the loop counter and any key variables after each iteration. Tracing is systematic — work through it step by step and do not skip iterations.
For the example above, the trace table looks like this:
| Iteration | counter | total |
|---|---|---|
| Before loop | — | 0 |
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
After the loop ends, print(total) outputs 15. The loop body ran exactly 5 times — once for each value of counter.
Indentation in Python
Python uses indentation (4 spaces) to mark the loop body. Every line that is part of the loop must be indented to the same level. Any line that returns to the original indentation level runs after the loop has finished. Getting indentation wrong is one of the most common causes of bugs.
total = 0
for counter in range(5):
total = total + counter # indented — inside the loop
print("running:", total) # indented — inside the loop
print("done:", total) # not indented — runs AFTER the loop
Worked examples
Write a program that prints each whole number from 1 to 5 on a separate line.
FOR counter FROM 1 TO 5 DO
SEND counter TO DISPLAY
END FORfor counter in range(1, 6):
print(counter)1 2 3 4 5 (each on its own line). Note that range(1, 6) is used, not range(1, 5), because range excludes the stop value.Write a program that adds up every whole number from 1 to 10 and displays the result.
total variable to 0 before the loop begins. This must be outside the loop.total = 0
for counter in range(1, 11):
total = total + counter
print(total)total grows: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55. The final output is 55.Write a program that prints the 7 times table from 7 × 1 to 7 × 10.
FOR multiplier FROM 1 TO 10 DO
SEND "7 × " + multiplier + " = " + (7 * multiplier) TO DISPLAY
END FORfor multiplier in range(1, 11):
print("7 ×", multiplier, "=", 7 * multiplier)7 × 1 = 7, 7 × 2 = 14, … and ends with 7 × 10 = 70. The expression 7 * multiplier is evaluated on each iteration using the current value of multiplier.Write a program that asks the user to enter 3 test scores, then calculates and displays the average.
total = 0 before the loop. Use range(3) to loop exactly 3 times (count = 0, 1, 2 — but the value of count is not used inside the loop body, only the scores matter).total = 0
for count in range(3):
score = int(input("Enter score: "))
total = total + score
average = total / 3
print("Average:", average)average = total / 3 and print() are outside the loop (not indented). They run once, after all 3 scores have been collected. If they were inside the loop, the average would be calculated and printed after every single score.A gym tracks how many calories a member burns across 5 exercise sessions. Write a program that asks the user to enter 5 calorie values, adds them together, and displays the total calories burned.
Answer the following:
- Why is a fixed loop the right choice for this program?
- Write the pseudocode using SQA reference language.
- Write the Python code.
- A fixed loop is correct because the number of sessions (5) is known before the program starts. The loop runs exactly 5 times — there is no condition to check, and the number of repetitions never changes.
-
SET total TO 0 FOR session FROM 1 TO 5 DO RECEIVE calories FROM (INTEGER) KEYBOARD SET total TO total + calories END FOR SEND "Total calories: " + total TO DISPLAY -
total = 0 for session in range(5): calories = int(input("Enter calories burned: ")) total = total + calories print("Total calories:", total)
range(1, 5) produces 1, 2, 3, 4 — it does not include 5. To count from 1 to 5 inclusive, you need range(1, 6). A common version of this mistake is writing range(1, 10) when you want to loop 10 times — you actually get only 9 iterations.total = 0 inside the loop body resets it to zero on every iteration, so only the last value is ever added. The total variable must be set to 0 once, before the loop begins.Fixed loop questions in the SQA exam typically use the command words write, describe, or trace. For write questions, remember the exact pseudocode structure: FOR <counter> FROM <start> TO <end> DO … END FOR. The TO value is inclusive in pseudocode (the loop runs for that value), but range() in Python is exclusive — this asymmetry is frequently tested.
For trace questions, always draw a table with one column for each variable (including the counter) and one row per iteration. State clearly what the final output is. Do not guess — work through each iteration systematically. Examiners award marks for each correct row, so a partially correct trace still earns marks.
Questions 1–5 are auto-checked. Questions 6–10 are self-marked — write your answer, then reveal the model answer to check your work.
1. How many times does the body of for counter in range(5): execute? TYPE 1
2. What values does range(2, 6) produce? TYPE 1
3. What does the following code print?
for counter in range(1, 4):
print(counter)
TYPE 1
4. A program uses a running total. Where should total = 0 be placed? TYPE 1
5. A teacher wants to record 30 pupils' test scores and calculate the class average. Which type of loop is most appropriate? TYPE 1
6. Explain the difference between range(5) and range(1, 6). Include the values each one produces and state how many iterations each gives. TYPE 2
range(5) produces the values 0, 1, 2, 3, 4. When only one argument is given, Python starts at 0 by default and counts up to (but not including) the stop value, giving 5 iterations.
range(1, 6) produces the values 1, 2, 3, 4, 5. It starts at 1 and counts up to (but not including) 6, also giving 5 iterations.
Both produce 5 iterations, but the actual values differ — range(5) includes 0, while range(1, 6) starts at 1 and ends at 5. Which to use depends on whether the loop counter value matters inside the loop body.
7. Trace through the code below. Copy and complete the table showing the value of counter and total after each iteration, then state the final output.
total = 0
for counter in range(1, 5):
total = total + counter
print(total)
TYPE 2
Trace table:
| Iteration | counter | total |
|---|---|---|
| Before loop | — | 0 |
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
range(1, 5) gives counter = 1, 2, 3, 4 (four iterations). Final output: 10
8. Write Python code for a program that prints the first 6 multiples of 4 (i.e., 4, 8, 12, 16, 20, 24). TYPE 2
for multiplier in range(1, 7):
print(4 * multiplier)
range(1, 7) gives multiplier = 1, 2, 3, 4, 5, 6. Multiplying each by 4 gives 4, 8, 12, 16, 20, 24 — each printed on its own line.
9. Write a complete Python program that asks the user to enter 5 numbers, adds them together, and then prints both the total and the average. TYPE 2
total = 0
for count in range(5):
number = float(input("Enter a number: "))
total = total + number
average = total / 5
print("Total:", total)
print("Average:", average)
float() is used here because the user's numbers may include decimal values. total is initialised before the loop and only the final print statements run after the loop, once all 5 numbers have been collected.
10. The code below contains a bug. Identify the bug, explain what the code currently prints and what it should print, then write the corrected version.
total = 0
for counter in range(1, 6):
total = 0
total = total + counter
print(total)
TYPE 3
Bug: total = 0 is inside the loop body (it is indented). This means it resets total to 0 at the start of every iteration, before adding counter. After each iteration, total only ever holds the current value of counter.
What it currently prints: 5 (only the last value of counter, because total is reset to 0 then set to 5 on the final iteration).
What it should print: 15 (the sum 1 + 2 + 3 + 4 + 5).
Corrected code:
total = 0
for counter in range(1, 6):
total = total + counter
print(total)
Moving total = 0 outside and before the loop means it is only set once. The loop then accumulates the sum correctly: 1, 3, 6, 10, 15.
Suggested timing: 60 minutes. Warm up 10 min; notes + examples 20 min; now you try 5 min; task set 25 min.
Key misconception to address: The off-by-one error with range(). Many pupils write range(1, 10) expecting 10 iterations. Drill this in the warm-up debrief: range excludes the stop value. A useful mental model is "range goes up to, not through, the stop."
Live demo suggestion: Type the running total example in PyCharm or REPL.it. Show what happens when total = 0 is placed inside the loop — pupils see the output is wrong (5 instead of 15). Then move it outside and re-run. The live fix lands much better than just describing the mistake.
Extension question: Ask pupils to modify the Q9 program to also display the highest number entered. This requires an additional variable (e.g. highest) and an IF statement inside the loop — previewing SDD10 and SDD7 content.
SQA command words covered: "write" (pseudocode/Python), "trace" (Q7), "identify" (Q10 bug-finding), "explain" (Q6, Q10).