Lesson 5 of 10
Programming: Lesson 5

Count-Controlled Iteration: FOR Loops

Repeating a block of code a known number of times. This lesson covers Python's range() function, step values, iteration over collections, nested loops, and the common patterns that appear in every exam.

55 minutes Python + C# toggle
Language: Saved automatically

Imagine having to print "Hello" 1000 times. Writing 1000 print statements would take an hour and produce a 1000-line file. A FOR loop does it in two lines. But loops are not just about saving keystrokes - they are the mechanism behind searching a list, processing every row in a spreadsheet, drawing every pixel on a screen, and simulating every second of a physics model. Once you can loop, you can automate anything that has a pattern.

Think about it: List three real programs where a FOR loop over a known range is clearly the right tool. Then list three programs where you do not know in advance how many times to repeat. The second list is what WHILE loops (next lesson) are for - can you predict the difference?
Terms you need to know
Count-controlled loop
A loop that repeats a fixed, pre-known number of times. In Python: FOR. In C#: for(...).
Loop variable (i)
The variable that takes each value in the range. Commonly named i, j, k for nested loops.
range()
Python function that generates a sequence of integers. range(5) gives 0,1,2,3,4. range(1,6) gives 1,2,3,4,5.
Step
The increment between values in a range. range(0, 10, 2) gives 0,2,4,6,8. Negative step counts down.
Nested loop
A loop inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop.
Accumulator
A variable that collects a running total inside a loop. Must be initialised to 0 (for sum) before the loop starts.
Writing count-controlled loops

A FOR loop iterates over a sequence. range() generates that sequence. The three-argument form is range(start, stop, step) - stop is never included.

# range(stop) - from 0 up to (not including) stop
for i in range(5):
    print(i)          # 0 1 2 3 4

# range(start, stop) - from start up to stop
for i in range(1, 6):
    print(i)          # 1 2 3 4 5

# range(start, stop, step) - step of 2
for i in range(0, 10, 2):
    print(i)          # 0 2 4 6 8

# Counting down with negative step
for i in range(5, 0, -1):
    print(i)          # 5 4 3 2 1
// C# for loop: for(init; condition; update)
for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);    // 0 1 2 3 4
}

// From 1 to 5 inclusive
for (int i = 1; i <= 5; i++)
    Console.WriteLine(i);    // 1 2 3 4 5

// Step of 2
for (int i = 0; i < 10; i += 2)
    Console.WriteLine(i);    // 0 2 4 6 8

// Counting down
for (int i = 5; i > 0; i--)
    Console.WriteLine(i);    // 5 4 3 2 1
range() stop is exclusive - a very common exam trap

range(1, 6) generates 1, 2, 3, 4, 5 - NOT 6. The stop value is excluded. This is consistent with Python's slicing (s[1:4] excludes index 4). In C#, i <= 5 includes 5 because you are writing the condition explicitly. Always count the values to check your range boundaries.

Common loop patterns:

# Accumulator pattern - sum numbers 1 to 10
total = 0                     # initialise BEFORE the loop
for i in range(1, 11):
    total = total + i         # or: total += i
print(total)                 # 55

# Iterate over a list (see Lesson 7 for full list coverage)
names = ["Alice", "Bob", "Carol"]
for name in names:
    print("Hello,", name)

# Multiplication table row for 7
for i in range(1, 13):
    print(f"7 x {i} = {7 * i}")
Exam angle

In exam trace questions, always track: (1) the loop variable value at the start of each iteration, (2) any accumulator variable after each iteration, and (3) the total number of iterations. For range(a, b, s), the number of iterations is ceil((b-a)/s). Common error: forgetting to initialise accumulators to 0 before the loop (putting them inside means they reset each iteration).

Loops inside loops

A nested loop has an inner loop that runs completely for each iteration of the outer loop. If the outer loop runs N times and the inner loop runs M times, the inner body executes N * M times in total.

# Nested loop: multiplication table (3 rows x 3 cols = 9 cells)
for row in range(1, 4):        # outer: 3 iterations
    for col in range(1, 4):    # inner: 3 iterations per outer
        print(row * col, end=" ")
    print()                    # newline after each row
# Output:
# 1 2 3
# 2 4 6
# 3 6 9
// C# nested loop: multiplication table
for (int row = 1; row <= 3; row++)
{
    for (int col = 1; col <= 3; col++)
    {
        Console.Write(row * col + " ");
    }
    Console.WriteLine();
}
Loop State Stepper
Loop Trace Table Generator
Set range parameters and see the complete trace table with loop variable and accumulator values.
start:
stop:
step:
Iterationi (loop var)total (accumulator: total += i)

Three Quick Challenges

Predict the output

What does this accumulator loop print? Trace through each iteration.

total = 0
for i in range(1, 5):
    total += i
print(total)
int total = 0;
for (int i = 1; i < 5; i++)
    total += i;
Console.WriteLine(total);
Fill in the blank

Complete the loop to count from 0 to 4:

for i in (5): print(i)
for (int i = 0; i < ; i++) Console.WriteLine(i);
Spot the bug

This loop crashes with a NameError. What is missing?

for i in range(1, 6):
    total += i
print(total)

Test yourself

1. How many times does the body of for i in range(3, 8) execute?

range(3, 8) generates 3, 4, 5, 6, 7 - that is 5 values (8-3=5). The stop value (8) is excluded. A common mistake is to include 8 giving 6 iterations, or to count from 0 giving 8.

2. What are the values produced by range(0, 10, 3)?

Starting at 0, stepping by 3: 0, 3, 6, 9. The next would be 12 but that exceeds the stop value of 10, so it is not generated. Note 9 < 10 so it IS included. 12 >= 10 so it is excluded.

3. A student initialises total = 0 inside a FOR loop. What is the bug?

Every iteration starts by setting total = 0 again, wiping out the previous sum. To accumulate correctly, total must be initialised to 0 before the loop starts. At the end of each iteration it should hold the running total, not be reset to zero.

4. If an outer loop runs 4 times and an inner loop runs 3 times, how many times does the inner body execute?

The inner loop runs completely (3 times) for each iteration of the outer loop (4 times). Total = 4 * 3 = 12. This is why nested loops can be slow with large values: a 1000-iteration outer and 1000-iteration inner gives 1,000,000 executions of the inner body.

5. What is the output of: for i in range(5, 0, -2): print(i)?

range(5, 0, -2) starts at 5, counts down by 2, stopping before 0: 5, 3, 1. The next would be -1 but that is less than the stop value (0) when stepping negatively, so iteration ends. range(5, 0, -1) would give 5, 4, 3, 2, 1.
Challenge question

Write a program that uses a FOR loop to find the largest number in a list of 10 integers entered by the user. You cannot use Python's max() function or C#'s LINQ Max(). You must use a loop and a variable that tracks the current maximum. What should the initial value of the maximum variable be, and why?

Python solution:
numbers = []
for i in range(10): numbers.append(int(input("Number " + str(i+1) + ": ")))
maximum = numbers[0] # initialise to first element
for num in numbers:
if num > maximum: maximum = num
print("Largest:", maximum)

Initial value choice: Initialise to numbers[0] (the first element). Do NOT initialise to 0 or -1 - if all numbers are negative, 0 or -1 would incorrectly be "larger" than all of them. Using the first element guarantees the answer is always from the actual dataset. Alternatively, use Python's float('-inf') (negative infinity) which is guaranteed to be smaller than any real number.
Printable Worksheets

Practice what you have learned

Three levelled worksheets. Download, print and complete offline.

Recall
FOR Loop Trace Tables
Complete trace tables for FOR loops with range(), accumulators and nested loops.
Download
Apply
Loop Programs
Write programs to sum series, print patterns, find maximums and build multiplication tables.
Download
Exam Style
Exam-Style Questions
Predict loop outputs, fix range boundaries, trace nested loops and explain accumulator errors.
Download
Lesson 5 - Programming
Count-Controlled Iteration: FOR Loops
Starter activity
Ask students to write down the numbers range(1, 6) produces WITHOUT running code. Then range(0, 10, 3). Then range(5, 0, -1). Check answers before proceeding. This reveals the "stop is exclusive" misconception immediately - a common source of off-by-one errors in student programs.
Lesson objectives
1
Write FOR loops using range() with one, two and three arguments.
2
Use negative step values to count downwards.
3
Write accumulator patterns: initialise before loop, update inside loop.
4
Write and trace nested FOR loops, counting total iterations.
5
Complete trace tables for FOR loops showing loop variable and accumulator at each step.
Key vocabulary
Count-controlled iteration
Repeating a known, fixed number of times. Use FOR when you know the count before the loop starts.
Accumulator
Variable that builds up a result across iterations. Must be initialised to a starting value BEFORE the loop.
Discussion questions
What is the key difference between a for loop and a while loop? When would you choose each one?
Why must an accumulator variable be initialised to a starting value before the loop begins? What goes wrong if you forget?
If range(5) gives 0, 1, 2, 3, 4 - can you think of a bug that would result from assuming it goes to 5?
Exit tickets
Write a FOR loop that prints the even numbers from 2 to 20 inclusive. [2 marks]
Trace: total=0; for i in range(1,5): total += i*2. What is total after the loop? Show trace table. [4 marks]
How many times does the inner body execute: for i in range(4): for j in range(3): print(i,j)? [2 marks]
Homework suggestion
Write a program that asks the user for an integer n and outputs the sum of all even numbers from 2 to n. Then extend it: also find the product (multiply them all together). Test with n = 10: sum should be 30, product should be 3840.