Selection In Programming

Teach any GCSE Computer Science class

We have put together a full GCSE Computer Science curriculum that will give you all the teaching materials you need to teach any topic.

Whether you’re a brand new Computer Science teacher, or you’ve been teaching ICT for years, our resources will save you hours and hours of lesson preparation every single week.

View the resources →

A selection is used to make choices depending on information.  An algorithm can be made smarter by using IF, THEN, and ELSE functions to reiterate instructions, or to move the process in question to different parts of the program.

Selection is also called a decision.  It is one of the three basic logic assemblies in computer programming.  The other two logic assemblies are sequence and loop.

In the assembly of a selection, a question is asked, and based on the answer the program takes one of two paths of action, after which the program moves on to the next event.

This assembly is sometimes referred to as an IF-THEN-ELSE procedure because it leads the program to act in the following way: IF Condition A is True, THEN carry out Action A, ELSE carry out Action Y.

All logic problems in programming can be resolved by launching algorithms using only the three logic assemblies, and they can be merged in an infinite number of ways.  The more intricate the computing requirement, the more intricate the combination of structures required to meet the relevant needs.

Importance of Selection

Selections enable including more than one route in a program.  Many solutions require multiple choices or decisions, and these choices result in various possible routes which the program can take.  These routes signify the outcome of making a choice.  Without selection it would not be possible to contain diverse routes in programs, and the solutions we come up with would not be realistic.

Programming

Once an algorithm has been planned and finalized, it must be translated into a code that a computer can understand.

We make programs to execute algorithms.  Algorithms have steps, while programs have statements.

IF Statement

Programs consist of a set of instructions that are executed one after another.  Sometimes there may be more than one route that can be followed: at this point, a decision needs to be completed.  This decision is referred to as a selection.

For example, the following algorithm displays a message depending on your age:

  1. Ask your age
  2. IF your age is 60 or older, say “You are a senior citizen!”

The selection comes in step 2.  If you are aged 60 or older, one message is displayed.

In programming, a selection is implemented using an IF statement.

IF-ELSE Statements

In programming, selections are usually denoted by the statements: IF and ELSE.

  • IF denotes the question
  • ELSE leads to a given action if the answer to the question is false

For example, this simple algorithm prints out a different message depending on how old you are.  Using IF and ELSE, the steps are as follows:

  1. Ask your age
  2. IF your age is 60 or older, say “You are a senior citizen!”
  3. ELSE say “You’re not a senior citizen yet!”

If this algorithm is tried using 65 as your age, the answer to the question at step 2 is true, so the algorithm tells us to say, “You are a senior citizen!”

If the algorithm is tried using 15 as your age, the answer to the question at step 2 is false, so the algorithm tells us to say, “You are not yet a senior citizen!”

IF-ELSE IF-ELSE Statements

Using IF and ELSE gives two possible choices or routes that a program can follow.  However, sometimes more than two choices are wanted.  To accommodate this, the statement ELSE IF is used.

This simple algorithm prints out a different message depending on how old you are.  Using IF, ELSE and ELSE IF, the steps are:

  1. Ask your age
  2. IF your age is 60 or older, say “You are a senior citizen!”
  3. ELSE IF you are exactly 50, say “Wow, you are in your prime!”
  4. ELSE say “You’re not a senior citizen yet!”

When using an IF-ELSE IF statement, the program will stop inspecting as soon as it receives a positive answer.  Therefore, it is imperative to include the IF and ELSE conditions in the correct sequence, in order to make the program as logical as possible.