SelectionRepresenting selection

When designing algorithms, there are many steps where decisions must be made. This decision is known as selection, and can be displayed in pseudocode or flow diagrams.

Part ofComputer ScienceAlgorithms

Representing selection

There are two ways of representing :

  • a (also known as a flowchart)

Representing selection in pseudocode

Writing in pseudocode is rather like writing in a . 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.