Iteration is the term given to the repetition of a block of statements (code) within a computer program. Basically, think loops!
There are 3 types of iteration that you need to learn for GCSE:
- FOR .. TO .. NEXT
- REPEAT .. UNTIL
- WHILE .. DO .. ENDWHILE
Programming languages may put their own syntax/wording on these loop structures, but look closely and you’ll see that they do relate back to the above list.
Not all loops behave in the same way so it is important to choose the right iteration code for the job/purpose.
REPEAT .. UNTIL ..
After each iteration of the loop a condition is checked to see if an exit criteria has been met yet.
This means that at least one iteration of the loop will be executed.
Because the condition/check is placed at the bottom of the loop, this is referred to as bottom testing (or a post condition).
WHILE .. DO .. ENDWHILE
A condition tests whether to enter the loop, or simply bypass it.
If the loop is entered, the condition is checked again before each iteration to see if the criteria has been met yet (to exit the loop).
Because the condition is placed at the start of the loop, this is referred to as top testing (or a pre-condition).
FOR .. TO .. NEXT
The amount of iterations (times around the loop) is predetermined (fixed) at the beginning of the loop.
This type of loop is ideal when you already know how many iterations of the code is required.