Iteration and Repetition (SQA National 5 Computing Science): Revision Note
Exam code: X816 75
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 |
|---|---|
| |
This outputs “Hello” ten times | Python’s |
You can use a condition or arithmetic operation to change how the counter increases
SQA Pseudocode | Python |
|---|---|
| |
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 |
|---|---|
| |
This loop continues until the user types “Red” | |
| |
Post-condition loop (REPEAT … UNTIL)
SQA Pseudocode | Python |
|---|---|
| |
Python has no |
Nested loops
A nested loop is a loop inside another loop
SQA Pseudocode | Python |
|---|---|
| |
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 DISPLAYFeature tested | Expected response/criteria | Marks | Commentary |
|---|---|---|---|
1. Initialisation | Correct initialisation of necessary variables ( | [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 ( | [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 | [1 mark] | Tests the correct application of complex conditional logic to control iteration based on multiple factors |
4. Calculation and Progression | Correctly increments | [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!
Did this page help you?