2. Selection

posted Jan 6, 2015, 1:36 AM by Maurice Nyamoti   [ updated May 6, 2015, 1:07 AM ]

Selection

In selection control, execution of statements depends on a condition which is either a true or false

There are four types of selection controls. Namely:
  1. IF.....THEN
  2. IF.....THEN.....ELSE
  3. NESTED IF
  4. CASE SECTION

IF....THEN

The IF....THEN selection is used if only one option is available. All other options are ignored
Format:
IF <condition> THEN
    Statements;
ENDIF
if....then
Flowchart Sample

IF....THEN....ELSE

This type of selection is suitable where there are two available options
General Format
IF <condition> THEN
    Statements;
ELSE
    Statements;
EndIF
if....then....else
Flowchart Sample

Nested IF Selection

This type of selection is used where two or more options have to be considered to make a selection.

General Format
IF <condition> THEN
            statements
    ELSE
    IF <condition> THEN
            statements
    ELSE
        IF<condition> THEN
            Statements
    ELSE
            Statements
        ENDIF
    ENDIF
ENDIF

NESTED IF
Flowchart Sample

CASE Construct

This is an alternative to the Nested IF especially where there are several options to choose from. Its preferred because it reduces the many lines of code. However, the boolean expression for the case selection can only be expressed using integers and alphabetic characters only. The General format should be

CASE integer OF or
CASE Char OF
Example:
Case x of
label 1: statement 1
label 2: statement 2
label 3: statement 3
.
.
.
label n: statement n-1
Else
statement
Endcase
case x of
Comments