Selection is a decision or question. At some point, a program may need to ask a question because it has reached a step where one or more options are available. Depending on the answer given, the program will follow a certain step and ignore the others.
Selection is making a decision on which logical path to follow. In a theme park, the queuing system for a roller coaster might read ‘IF front row, queue here, else, all other rows queue here’.
Why is selection important?
Selection allows there to be more than one path through a program.
Many solutions feature several choices or decisions. These decisions lead to different paths through the program. These paths represent the result of making a choice. Without selection it would not be possible to include different paths in programs, and the solutions we create would not be realistic.
Selection in programming
Once an algorithm has been designed and perfected, it must be translated – or programmed – into code that a computer can read.
We create programs to implement algorithms. Algorithms consist of steps, where programs consist of statements.
Selection is implemented in programming using IF statements.
IF statements
Programs consist of a set of instructions that are carried out one after another. Sometimes there may be more than one path (or set of steps) that can be followed. At this point, a decision needs to be made. This decision is known as selection.
For example, this simple algorithm prints out a message depending on how old you are:
- Ask how old you are
- IF you are 70 or older, say “You are aged to perfection!”
The selection comes in step 2. If you are aged 70 or over, one message is displayed.
In programming, selection is implemented using an IF statement