Iteration (AQA GCSE Computer Science)
Revision Note
Iteration
What is iteration?
Iteration is repeating a line or a block of code using a loop
Iteration can be:
Definite (count controlled) - this is when the code is repeated a fixed number of times (e.g. using a for loop)
Indefinite (condition controlled) - this is when the code is repeated until a condition is met (e.g. using a while loop or a do while loop)
Examples
Iteration | Pseudocode | Python |
FOR loop (Definite Iteration) |
|
|
This will print the word "Hello" 10 times (0-9 inclusive) | ||
|
| |
This will print the even numbers from 2 to 10 inclusive | ||
|
| |
This will print the numbers from 10 to 0 inclusive | ||
WHILE loop (Indefinite Iteration) |
|
|
| This will loop until the user inputs the colour "Red". Check condition is carried out before entering loop | |
DO WHILE loop (Indefinite Iteration) |
|
|
| This will loop until the user inputs the colour "Red". Loop iterates once before a check is carried out |
How to identify programming constructs
You can identify which programming constructs are used by looking at certain keywords
The keywords if, elseif, else, endif indicate that the construct is selection
The keywords for, while, do indicate that the construct is iteration
If none of these keywords are used, this is a good indication that the construct is sequence
Worked Example
Tick (✓) one box in each row to identify whether each programming construct has or has not been used in the program [3]
total ← 0
for i ← 1 to 5
num ← USERINPUT
total ← total + num
next i
print(total)
How to answer this question
Always look for keywords first, if you find any then that construct must have been used
Sequence must always be used if the program works
Answer
Nested Iteration
What is nested iteration?
Nested means to be 'stored inside the other'
An example of nested iteration is one loop inside of another loop
It is worth noting that selection can be nested as well
Examples of nested programming
Last updated:
You've read 0 of your 10 free revision notes
Unlock more, it's free!
Did this page help you?