Iteration (Cambridge (CIE) O Level Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

What is iteration?

  • Iteration is repeating a line or a block of code using a loop

  • Iteration can be:

    • Count controlled

    • Condition controlled

    • Nested

Count Controlled Loops

What is a count controlled loop?

  • A count controlled loop is when the code is repeated a fixed number of times (e.g. using a for loop)

  • A count controlled loop can be written as:


    FOR <identifier> ← <value1> TO <value2>

    <statements>

    NEXT <identifier>

  • Identifiers must be an integer data type

  • It uses a counter variable that is incremented or decremented after each iteration

  • This can be written as:


    FOR <identifier> ← <value1> TO <value2> STEP <increment>

    <statements>

    NEXT <identifier>

  • A detailed look at a Python FOR statement:

xp66-srn-for-loop-pseudocode-computer-science-revision-notes

Examples

Iteration

Pseudocode

Python

Count controlled

FOR X ← 1 TO 10
    OUTPUT "Hello"
NEXT X
for x in range(10):
    print("Hello")

This will print the word "Hello" 10 times

FOR X ← 2 TO 10 STEP 2
    OUTPUT X
NEXT X
for x in range(2,12,2):
    print(x)
# Python range function excludes end value

This will print the even numbers from 2 to 10 inclusive

FOR X ← 10 TO 0 STEP -1
    OUTPUT X
NEXT X
for x in range(10,-1,-1):
    print(x)
# Python range function excludes end value

This will print the numbers from 10 to 0 inclusive

Condition Controlled Loops

What is a condition controlled loop?

  • A condition controlled loop is when the code is repeated until a condition is met

  • There are two types of condition controlled loops:

    • Post-condition (REPEAT)

    • Pre-condition (WHILE)

Post-condition loops (REPEAT)

  • A post-condition loop is executed at least once

  • The condition must be an expression that evaluates to a Boolean (True/False)

  • The condition is tested after the statements are executed and only stops once the condition is evaluated to True

  • It can be written as:


    REPEAT
    <statement>
    UNTIL <condition>

Iteration

Pseudocode

Python

Post-condition

REPEAT
    INPUT Colour
UNTIL Colour ← "red"

# NOT USED IN PYTHON

REPEAT
    INPUT Guess
UNTIL Guess ← 42

# NOT USED IN PYTHON

Pre-condition loops (WHILE)

  • The condition must be an expression that evaluates to a Boolean (True/False)

  • The condition is tested and statements are only executed if the condition evaluates to True

  • After statements have been executed the condition is tested again

  • The loop ends when the condition evaluates to False

  • It can be written as:


    WHILE <condition> DO
    <statements>
    ENDWHILE

  • A detailed look at a Python WHILE statement:

while-loop-password-pseudocode-computer-science-revision-notes

Iteration

Pseudocode

Python

Pre-condition

WHILE Colour != "Red" DO
    INPUT Colour
ENDWHILE
while colour != "Red":
    colour = input("New colour")
INPUT Temperature
WHILE Temperature > 37 DO
    OUTPUT "Patient has a fever"
    INPUT temperature
END WHILE
temperature = float(input("Enter temperature: "))
while temperature > 37:
    print("Patient has a fever")
    temperature = float(input("Enter temperature: "))

Nested Iteration

What is nested selection?

  • Nested iteration is a loop within a loop, e.g. a FOR inside of another FOR

  • Nested means to be 'stored inside the other'

Example

Pseudocode

Total ← 0 

FOR Row ← 1 TO MaxRow 
    RowTotal ← 0 

    FOR Column ← 1 TO 10 
        RowTotal ← RowTotal + Amount[Row, Column] 
    NEXT Column 

    OUTPUT "Total for Row ", Row, " is ", RowTotal 
    Total ← Total + RowTotal 

NEXT Row 

OUTPUT "The grand total is ", Total

Python

# Program to print a multiplication table up to a given number

# Prompt the user to enter a number
number = int(input("Enter a number: "))

# Set the initial value of the counter for the outer loop
outer_counter = 1

# Outer loop to iterate through the multiplication table
while outer_counter <= number:
    # Set the initial value of the counter for the inner loop
    inner_counter = 1

    # Inner loop to print the multiplication table for the current number
    while inner_counter <= 10:
        # Calculate the product of the outer and inner counters
        product = outer_counter * inner_counter

        # Print the multiplication table entry
        print(outer_counter, "x", inner_counter, "=", product)

        # Increment the inner counter
        inner_counter = inner_counter + 1

    # Move to the next number in the multiplication table
    outer_counter = outer_counter + 1

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.