Selection In Programming

A selection is used to make choices depending on information.  An algorithm can be made smarter by using IF, THEN and ELSE to reiterate instructions or move 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 a selection assembly, a question is inquired, 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 because it leads the program to conduct in this way: If Condition A is True then carry out Action X 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.

Importance of Selection

Selection enables having more than one route in a program.  Many solutions require many choices or decisions.  These choices result to various routes in the program.  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 make would not be realistic.

Programming

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

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

IF Statements

Programs comprise 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 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, selection is implemented using an IF statement

IF-ELSE

In programming, selection is usually denoted by the statements: IF and ELSE.

  • IF denotes the question
  • ELSE leads to the 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:

  • Ask your age
  • IF you are 60 or older, say “You are a senior citizen!”
  • ELSE say “You are not yet a senior citizen!”

If this algorithm is tried using 65 as our 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 our 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!”

Using ELSE IF to Provide More Choices

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:

  • Ask your age
  • IF you are 60 or older, say “You are a senior citizen!”
  • ELSE IF you are exactly 50, say “Wow, you are at your golden year!”
  • ELSE say “You are not yet a senior citizen!”

When using an ‘if-else if’ statement, the program will stop inspecting as soon as it receives a positive answer.  Therefore, it is very imperative to get the ‘if’ and ‘else’ conditions in the correct order to make the program as logical as possible.