Revise Computing
Revision notesQuestionsFor schools
Log inStart revising free
Revision notesQuestionsFor schoolsLog in
GCSE Computer Science/Paper 2/Programming Fundamentals

Iteration (count- and condition-controlled loops)

The use of the three basic programming constructs used to control the flow of a program: Iteration (count- and condition-controlled loops)

2.2.1 Programming Fundamentals

Practise exam questions

Programming Fundamentals — Note 4 of 6

Practical use of the techniques, Understanding of each technique

What you need to know

Iteration means repeating a set of instructions. There are two main types: count-controlled loops (FOR loops) that repeat a fixed number of times, and condition-controlled loops (WHILE and DO-UNTIL) that repeat until a condition changes. Loops can also be nested, meaning one loop inside another.

What to know

  • FOR loops are count-controlled: they repeat a FIXED number of times using a counter variable.
  • WHILE loops are condition-controlled: they check a condition BEFORE each iteration and repeat while it is True.
  • DO-UNTIL loops check the condition AFTER each iteration, so the code always runs at least once.
  • Use a WHILE loop when you don't know in advance how many iterations are needed (e.g. 'repeat until user enters stop').
  • Use a FOR loop when you know exactly how many iterations (e.g. 'ask 3 questions', 'check each item in an array').
  • Loops can be nested: a loop inside another loop (e.g. sorting algorithms have nested loops).
  • range(stop) starts at 0 and stops BEFORE the stop value: range(5) gives 0, 1, 2, 3, 4.
  • range(start, stop, step) sets where to start, where to stop (not included) and how much to change by. A negative step counts down, so to include 0 the stop value must be -1.
  • Totalling: set total = 0 BEFORE the loop, then add each value inside the loop with total += value.
  • Counting: set count = 0 BEFORE the loop, then add 1 inside an if whenever the condition is met: count += 1.

Exam technique

  • Tip:While loops for 'repeat until stop' — the condition must eventually become False. while team != "stop" is common.
  • Tip:Make sure inputs that change the loop condition are INSIDE the loop, not just outside it. A typical mistake is to calculate but not re-ask for input.
  • Tip:Initialise totals/counters BEFORE the loop, not inside it (otherwise they reset each iteration — a classic logic error).
  • Tip:while hours != 0 is safer than while hours > 0 (user could enter -1 and bypass the check).
  • Mistake:In Python, for x in list: — x is the item VALUE, not an index. Do not then try to access list[x] as x is not an index number.
  • Tip:OCR Exam Reference Language for i = 0 to 4 INCLUDES the end value (0 to 4). Python's range(5) gives the same values because the stop value is excluded. Watch for off-by-one errors when converting between them.
  • Tip:In a while loop that processes user input, ask for the first value BEFORE the loop and ask again as the LAST line inside the loop. Without the second input the loop never ends.

Code examples

FOR loop and WHILE loop
python
# FOR loop (count-controlled)
for i in range(5):
    print("Iteration", i)

# WHILE loop (condition-controlled)
password = ""
while password != "secret":
    password = input("Enter password: ")
print("Access granted!")
Count-controlled loops with range()
python
# Count up: 0, 1, 2, 3, 4 (the stop value 5 is NOT included)
for i in range(5):
    print(i)

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

# Count down: 10, 9, ... 0 (stop at -1 so that 0 is included)
for i in range(10, -1, -1):
    print(i)
Condition-controlled loops with while
python
# Number condition: variable set BEFORE the loop
total = 0
while total < 100:   # repeats while the condition is True
    total += 10      # variable must change INSIDE the loop

# Text condition: repeat until the user types exit
text = ""
while text != "exit":
    text = input("Type something (or 'exit' to quit): ")
    print("You typed:", text)
Totalling and counting with a for loop
python
total = 0
count_over_10 = 0

for i in range(5):
    num = int(input("Enter a number: "))

    total += num            # keep a running total

    if num > 10:            # count the numbers over 10
        count_over_10 += 1  # increment by 1

print("Total:", total)
print("Numbers over 10:", count_over_10)
Totalling and counting with a while loop
python
total = 0
count_over_10 = 0

num = int(input("Enter a number (negative to stop): "))  # first input BEFORE the loop

while num >= 0:             # run until a negative number is entered
    total += num            # keep a running total

    if num > 10:            # count the numbers over 10
        count_over_10 += 1

    num = int(input("Enter a number (negative to stop): "))  # next input as the LAST line

print("Total:", total)
print("Numbers over 10:", count_over_10)

Ready to test yourself?

Practise 2.2.1 questions
← Previous noteSelection2.2.1 Programming FundamentalsNext note →Arithmetic operators2.2.1 Programming Fundamentals

Back to Programming Fundamentals

Revise another OCR GCSE topic: Data Types, Additional Programming Techniques

Programming Fundamentals

Note 4 of 6

  1. 1Variables, constants, operators, inputs, outputs and assignments
  2. 2Sequence
  3. 3Selection
  4. 4Iteration (count- and condition-controlled loops)
  5. 5Arithmetic operators
  6. 6Boolean operators AND, OR and NOT
Topic overview

© 2026 Revise Computing · revisecomputing.co.uk

Privacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).