For Loops and While Loops at KS3: The Difference Explained with Examples

Loops let a program repeat a block of code without writing it out multiple times. In KS3 computing, students learn two types: the for loop, which repeats a set number of times, and the while loop, which repeats as long as a condition is true. Choosing the right loop makes programs shorter, clearer, and easier to maintain.

What is iteration in KS3 computing?

Repetition — also called iteration — is one of the three fundamental programming constructs, alongside sequence and selection. The DfE national curriculum for computing requires KS3 students to understand how programs use sequence, selection, and iteration to solve problems (gov.uk/government/publications/national-curriculum-in-england-computing-programmes-of-study).

Without loops, printing the numbers 1 to 100 would require 100 separate print statements. A loop does the same job in two or three lines, making programs far more efficient and readable.

What is a for loop?

A for loop is used for counted iteration — when you know in advance exactly how many times the loop should run. The loop counts through a sequence (often a range of numbers) and executes the body once for each value.

For loop syntax in pseudocode

FOR counter FROM 1 TO 10
OUTPUT counter
ENDFOR

For loop syntax in Python

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

range(1, 11) generates the numbers 1 to 10 (the upper limit is not included, which is a common source of confusion).

Worked example: printing a times table

The following Python program prints the 6 times table from 1 × 6 to 12 × 6.

for number in range(1, 13):
result = number * 6
print(number, "x 6 =", result)

Output:

1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
...
12 x 6 = 72

The loop variable number takes the values 1, 2, 3, … 12 in turn. Each iteration calculates and prints one line of the table.

What is a while loop?

A while loop is used for condition-controlled iteration — when you do not know in advance how many times the loop will run. The loop checks a condition at the start of each iteration and only continues if that condition is True.

While loop syntax in pseudocode

WHILE condition DO
code to repeat
ENDWHILE

While loop syntax in Python

while condition:
code to repeat

Worked example: input validation

A common real-world use of a while loop is to keep asking a user for input until they enter a valid value.

age = int(input("Enter your age: "))
while age < 0 or age > 120:
print("That is not a valid age. Please try again.")
age = int(input("Enter your age: "))
print("Thank you. Your age is", age)

The loop keeps running as long as the user's input is outside the range 0–120. Once they enter a valid number, the condition becomes False and the loop stops.

Worked example: countdown

count = 10
while count >= 1:
print(count)
count = count - 1
print("Blast off!")

Each iteration decreases count by 1. When count reaches 0 the condition count >= 1 becomes False and the loop ends.

For loop vs while loop: when to use which?

Feature | For loop | While loop
Number of repetitions | Known in advance | Not known in advance
Controlled by | A counter (range) | A condition
Typical use | Counting, times tables, processing lists | Input validation, game loops, waiting for an event
Risk | Off-by-one errors | Infinite loops if condition never becomes False
Pseudocode keyword | FOR … ENDFOR | WHILE … ENDWHILE

BBC Bitesize (bbc.co.uk/bitesize/subjects/zvc9q6f) includes interactive exercises where students can practise both loop types with immediate feedback — useful for Year 7, 8, and 9 revision.

Common mistakes with loops at KS3

1. Infinite loops

If the condition in a while loop never becomes False, the program runs forever. This is called an infinite loop and will cause the program to freeze.

# Infinite loop — count is never changed inside the loop!
count = 10
while count >= 1:
print(count)
# MISSING: count = count - 1

Always make sure something inside the loop changes the variable that controls the condition.

2. Off-by-one errors

range(1, 10) generates 1 to 9, not 1 to 10. Students who expect 10 values but write range(1, 10) get only 9. To include the number n, write range(1, n + 1).

# Prints 1 to 9 — off by one!
for i in range(1, 10):
print(i)

# Correct: prints 1 to 10
for i in range(1, 11):
print(i)

3. Forgetting to update the loop variable

In a while loop, if you forget to update the variable being tested, the loop runs forever. Always check that your loop variable changes every iteration.

4. Using a for loop when input validation is needed

Because you cannot know in advance how many attempts a user will need to enter valid data, a while loop is always the right choice for input validation. Using a for loop with a fixed number of attempts is a workaround, not a solution.

Frequently asked questions

What is the difference between a for loop and a while loop at KS3?

A for loop repeats a fixed, known number of times and is controlled by a counter. A while loop repeats as long as a condition is true and is used when you do not know in advance how many iterations are needed. At KS3 the key question is: "Do I know how many times I want to repeat?" If yes, use a for loop; if no, use a while loop.

How do you write a for loop in Python at KS3?

In Python, for i in range(start, stop): runs the indented block once for each value from start up to (but not including) stop. For example, for i in range(1, 6): loops with i = 1, 2, 3, 4, 5. The body of the loop must be indented with four spaces.

What causes an infinite loop and how do you fix it?

An infinite loop occurs when the condition in a while loop never becomes False. This usually happens because the variable being tested is never updated inside the loop. To fix it, make sure the loop body changes the variable so the condition will eventually be false — for example, decreasing a counter or validating user input.

Can you use a for loop for input validation in Python?

Technically yes, but it is not good practice. Input validation should use a while loop because you cannot predict how many attempts the user will need. A for loop with a fixed limit would stop asking after that limit even if the user still hasn't entered valid data.

What does range() do in a Python for loop?

range(start, stop) generates a sequence of integers from start up to (but not including) stop. range(1, 11) gives 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. You can also add a third argument for the step: range(0, 20, 2) gives even numbers from 0 to 18.

For Socratic computing tutoring at KS3 — see aitutors.me.