N3/N4  ·  SDD  ·  Software Design & Development

Fixed Loops (for)

National 3 & National 4 Computing Science Lesson SDD4 of 10 Approx 55 min
Learning intentions
  • Understand what a fixed (counted) loop is and why programs use them
  • Use for and range() to repeat instructions a set number of times
  • Use the loop variable inside a loop
Success criteria
  • I can write a for loop using range()
  • I can explain how many times a given range() loop will run
  • I can use a fixed loop to build a running total
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.

1. If you were told "clap your hands 5 times," how many claps would that be?

2. Which of these is an example of repeating something a fixed number of times?

3. True or false: a fixed loop is useful when you already know exactly how many times something should repeat.

Key vocabulary

Iteration
A programming construct that repeats a block of instructions.
Fixed (counted) loop
A loop that repeats a known, set number of times, decided before the loop starts.
for
Python's keyword for writing a fixed loop, usually combined with range().
range()
A built-in function that produces a sequence of numbers for a for loop to repeat over.
Loop variable
The variable in a for loop that holds the current value from the sequence on each pass.
Running total
A variable that accumulates a sum across each pass of a loop.

Fixed Loops (for)

What is a fixed loop?

Iteration is the third of the three basic programming constructs (alongside sequence and selection) and means repeating a block of instructions. A fixed loop (also called a counted loop) repeats a known, set number of times, decided before the loop even starts. This is different from a conditional loop (covered next lesson), which repeats until something becomes true or false, without necessarily knowing the number of repeats in advance.

The for loop and range()

In Python, a fixed loop is written using for together with range(). range(5) produces the sequence 0, 1, 2, 3, 4 — five numbers, starting at 0. This is a common trip-up: range(5) runs the loop 5 times, but the last value it produces is 4, not 5, because counting starts from zero.

CodeProducesNumber of repeats
range(5)0, 1, 2, 3, 45
range(1, 6)1, 2, 3, 4, 55
range(0, 10, 2)0, 2, 4, 6, 85

range(start, stop) begins at start and stops just before stop — the stop value itself is never included. Adding a third number, range(start, stop, step), changes how much the sequence increases by each time, instead of the default of 1.

Using the loop variable

The structure of a fixed loop is for i in range(5): followed by an indented block, exactly like selection. i is the loop variable — it takes each value from range() in turn, and can be used inside the loop, for example to print it or use it in a calculation. The name i is just a convention, not a rule — any valid variable name works.

Building a running total

A very common use of a fixed loop is to build up a running total — a variable that accumulates a value across every pass of the loop. This requires setting the total to 0 before the loop starts, then adding to it once per pass, inside the loop.

Worked examples

Example 1 — a basic for loop

A program prints "Hello" three times.

1
for i in range(3):
2
    print("Hello")
3
This outputs Hello three separate times, once for each value i takes (0, 1, 2) — note i is never actually used here, and that's fine.
Example 2 — using the loop variable

A program prints the numbers 1 to 5.

1
for number in range(1, 6):
2
    print(number)
3
This outputs 1, 2, 3, 4, 5 on separate lines. range(1, 6) is used, not range(1, 5), because 6 is never reached — it stops just before it.
Example 3 — a running total

A program adds up the numbers 1 to 5.

1
total = 0 — set up before the loop starts.
2
for number in range(1, 6):
    total = total + number
3
print(total) outputs 15 (1+2+3+4+5), once the loop has finished all five passes.
Now you try

A program contains: total = 0, then for number in range(2, 9, 2): with total = total + number indented underneath, then print(total) after the loop.

Answer the following:

  1. What values does number take across the loop?
  2. How many times does the loop run?
  3. What value does total hold when the program finishes?
  1. number takes the values 2, 4, 6, 8 (starting at 2, stepping by 2, stopping before 9).
  2. The loop runs 4 times.
  3. total holds 20 (2+4+6+8).
Common mistakes
Assuming range(5) counts to 5. It actually produces 0, 1, 2, 3, 4 — five values, but stopping one short of 5.
Setting the running total inside the loop. Writing total = 0 inside the loop resets it to zero every single pass, so it never accumulates. It must be set once, before the loop starts.
Forgetting range() stops before its second argument. range(1, 5) produces 1, 2, 3, 4 — not up to and including 5.
Indentation errors inside the loop. Just like selection, Python uses indentation to know which lines repeat — a line that should repeat but isn't indented will only run once, after the loop finishes.
Tip

N4 pupils: when explaining a for loop, state clearly how many times it repeats and why — for example "this loop runs 5 times because range(5) produces five values, 0 to 4" — rather than just describing what happens inside it.

Task Set

Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.

1. How many times does for i in range(7): repeat? TYPE 1

2. What is the last value produced by range(1, 5)? TYPE 1

3. What values does range(0, 10, 5) produce? TYPE 1

4. Why must total = 0 be written before a running-total loop, not inside it? TYPE 1

5. What does the loop variable in for number in range(3, 6): take across the loop? TYPE 1

6. Explain, using an example, why range(5) produces the numbers 0 to 4 rather than 1 to 5. TYPE 2 N4

Python counts sequences starting from 0 by default. range(5) means "produce 5 numbers starting from 0," which gives 0, 1, 2, 3, 4 — five values in total, but stopping one short of 5 itself. This is different from everyday counting, which usually starts from 1.

7. Explain the purpose of a running total, and why it must be set up before the loop starts. TYPE 2 N3

A running total is a variable that builds up a sum across every pass of a loop, so a program can add many values together without a separate variable for each one. It must be set to 0 before the loop starts, because if it were set inside the loop, it would be reset back to 0 every single pass, and the accumulated value would be lost.

8. Practical: In IDLE, create a new file called sdd4_loops.py. Write a program using a for loop that prints the numbers 1 to 10. Test it and record your output below. TYPE 3

for number in range(1, 11):
    print(number)


Expected output: the numbers 1 to 10, each on its own line.

9. Practical: In sdd4_loops.py, extend your program to also calculate and print the running total of the numbers 1 to 10. Record your output. TYPE 3

total = 0
for number in range(1, 11):
    total = total + number
print(total)


Expected output: 55 (1+2+...+10), printed after the numbers from Q8.
Teacher notes — Shift+T to hide

Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 10 min; now you try 5 min; task set 20 min.

Key misconception to address: zero-indexing of range() — pupils very reliably expect range(5) to include 5. Trace through it on the board with a tally.

Live demo suggestion: live-code the running total example, then deliberately move total = 0 inside the loop and run it, so pupils see the bug happen rather than just being told about it.

Extension idea (S5 / N5-bridging, non-assessed): briefly show looping over a list directly (for item in [1, 2, 3]:) as a "look ahead" — keep clearly separate from the assessed task set.

Assessment standards covered: N3 O2.1; N4 O1.2, O2.1.