Representing selection
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 selection 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.
Look at this simple six-step algorithm for comparing your dog’s age with your own:
- ask how old the dog is in human years
- multiply human years by seven to find out how old the dog is in dog years
- print the answer on the screen
- ask how old you are
- if the dog’s age in dog years is older than your age, say ‘Your dog is older than you!’
- otherwise, say ‘Your dog is not older than you.’
In pseudocode, the algorithm would look like this:
OUTPUT 'How old is your dog?'
INPUT user inputs their dog's age in human years
STORE the user's input in the human_years variable
dog_years = human_years * 7
OUTPUT 'In dog years, your dog is aged ' + dog_years
OUTPUT 'How old are you?'
INPUT user inputs their age
STORE the user's input in the user_age variable
IF dog_years > user_age THEN
OUTPUT 'Your dog is older than you!'
ELSE
OUTPUT 'Your dog is not older than you.'
NOTE: when using pseudocode, you do not need to use THEN with IF and ELSE - although you can if you want to.