Iteration and Repetition (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Iteration

  • Iteration means repeating a section of code using a loop

  • There are two main types of iteration:

    • Fixed loop - repeats a known number of times

    • Conditional loop - repeats until a condition is met

Fixed loops

What is a fixed loop?

  • A fixed loop repeats a section of code a specific number of times

  • A fixed loop can be written as:

SQA Pseudocode

Python

FOR counter FROM 1 TO 10 DO
    SEND "Hello" TO DISPLAY
END FOR
for counter in range(1, 11):
    print("Hello")

This outputs “Hello” ten times

Python’s range(1, 11) stops before the end value, so it counts 1 to 10

  • You can use a condition or arithmetic operation to change how the counter increases

SQA Pseudocode

Python

FOR counter FROM 1 TO 10 DO
    IF counter MOD 2 = 0 THEN
        SEND counter TO DISPLAY
    END IF
END FOR
for counter in range(1, 11):
    if counter % 2 == 0:
        print(counter)

This outputs the even numbers between 2 and 10

Conditional loops

What is a conditional loop?

  • A conditional loop repeats while or until a condition is met.

  • There are two types:

    • Pre-condition loop (WHILE)

      • The condition is checked before the loop runs

      • If the condition is true, the statements inside the loop are executed

    • Post-condition loop (REPEAT … UNTIL)

      • The condition is checked after the loop runs, so the code always executes at least once

Pre-condition loop (WHILE)

SQA Pseudocode

Python

SET colour TO ""
WHILE colour ≠ "Red" DO
    RECEIVE colour FROM KEYBOARD
END WHILE
colour = ""
while colour != "Red":
    colour = input("Enter colour: ")

This loop continues until the user types “Red”

RECEIVE temperature FROM KEYBOARD
WHILE temperature > 37 DO
    SEND "Patient has a fever" TO DISPLAY
    RECEIVE temperature FROM KEYBOARD
END WHILE
temperature = float(input("Enter temperature: "))
while temperature > 37:
    print("Patient has a fever")
    temperature = float(input("Enter temperature: "))

Post-condition loop (REPEAT … UNTIL)

SQA Pseudocode

Python

REPEAT
    RECEIVE colour FROM KEYBOARD
UNTIL colour = "Red"
while True:
    colour = input("Enter colour: ")
    if colour == "Red":
        break

Python has no REPEAT … UNTIL, but you can simulate it using a while True loop and a break statement

Nested loops

  • A nested loop is a loop inside another loop

SQA Pseudocode

Python

SET total TO 0
FOR row FROM 1 TO maxRow DO
    SET rowTotal TO 0
    FOR column FROM 1 TO 10 DO
        SET rowTotal TO rowTotal + Amount[row, column]
    END FOR
    SEND "Total for Row " & row & " is " & rowTotal TO DISPLAY
    SET total TO total + rowTotal
END FOR
SEND "The grand total is " & total TO DISPLAY
for row in range(1, 4):
    for column in range(1, 6):
        print("Row", row, "Column", column)

This will display every combination of row and column values

Worked Example

programmer is creating a simulation program to track the spread of a rumour on a social media platform. The simulation starts with 10 people sharing the rumour. Each day, the number of people sharing the rumour increases by a fixed amount (new shares = 20), but the total number of people sharing must be capped at 1,000.

The simulation needs to run until the total number of people sharing the rumour reaches or exceeds 1,000, or until 50 days have passed (whichever comes first).

Using Pseudocode, design the sequence of instructions that will calculate and display:

1. The total number of people sharing the rumour at the end of the simulation.

2. The number of days the simulation ran before it stopped.

Your design must implement an efficient loop structure that meets the termination requirements.

[4]

Answer

  • This question requires the calculation of the accumulated spread and the use of a complex conditional statement to handle two different stopping criteria (total shared reaching 1,000 OR days reaching 50)

Example solution:

SET totalShared TO 10
SET daysPassed TO 0
SET maxDays TO 50
SET newSharesPerDay TO 20

WHILE totalShared < 1000 AND daysPassed < maxDays DO
    SET daysPassed TO daysPassed + 1
    SET totalShared TO totalShared + newSharesPerDay
END WHILE

SEND "Simulation ran for" & daysPassed & " days" TO DISPLAY
SEND "Total people sharing:" & totalShared TO DISPLAY

Feature tested

Expected response/criteria

Marks

Commentary

1. Initialisation

Correct initialisation of necessary variables (totalShared, daysPassed, maxDays or equivalent)

[1 mark]

Ensures variables are declared and set before the loop, necessary for a running total of days and tracking the overall total (though this is not the standard algorithm, it requires the same setup)

2. Conditional Loop Structure

Use of a conditional loop structure (WHILE...DO or REPEAT...UNTIL)

[1 mark]

Tests the ability to choose the correct type of loop when the number of iterations is initially unknown

3. Complex Loop Condition

Loop condition must include two correct checks combined with the AND or OR logical operator to handle both termination criteria. For a WHILE loop, this is $totalShared < 1000$ AND $daysPassed < 50$

[1 mark]

Tests the correct application of complex conditional logic to control iteration based on multiple factors

4. Calculation and Progression

Correctly increments daysPassed and updates the running totalShared inside the loop body

[1 mark]

Tests that the loop successfully performs the required calculation and progression towards the exit condition in each cycle

Unlock more, it's free!

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

the (exam) results speak for themselves:

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

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

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.