Representing iteration
There are two ways of representing algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs.:
- pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding.
- a flowchartA diagram that shows a process, made up of boxes representing steps, decision, inputs and outputs.
Representing iteration in pseudocode
Writing in pseudocode is rather like writing in a programming languageA language used by a programmer to write a piece of software. . Each step of the algorithm is written on a line of its own, in sequence.
Consider this simple six-step algorithm for cleaning your teeth:
- set number_of_teeth_cleaned to 0
- put toothpaste on toothbrush
- use toothbrush to clean a tooth
- increase number_of_teeth_cleaned by 1
- if number_of_teeth_cleaned < 32 then go back to step 3
- rinse toothbrush
With each iteration, a decision needs to be made as to whether to continue iterating or not. Decisions are represented as selection.
In pseudocode, the algorithm would look like this:
number_of_teeth_cleaned = 0
put toothpaste on toothbrush
REPEAT
procedure clean_tooth
number_of_teeth_cleaned = number_of_teeth_cleaned + 1
UNTIL number_of_teeth_cleaned = 32
rinse toothbrush
Representing iteration in a flowchart
A flowchart uses a diagram to explain the steps of an algorithm.
In a flowchart, the algorithm would look like this: