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) |
|
# Not used in Python |
This will loop until the user inputs the colour "Red". Loop iterates once before a check is carried out |