You are on page 1of 31

PROBLEM SOLVING &

ALGORITHMS
CHAPTER 5: CONTROL STRUCTURES SELECTION

CONTENTS
One

way selection
Two way selection
Multiple way selection

What is Selection?
Structure

in pseudocode to
illustrate a choice between two or
more actions, depending on
whether a condition is true or
false

What is Selection?
The

condition in the IF statement


is based on a comparison of two
items, and is usually expressed
with one of the following relational
operators:
< less than
> greater than
<= less than or equal to
>= greater than or equal to
< > not equal to

One Way Selection

Syntax of one-way (if) selection:


if
if (expression)
(expression)
statement1;
statement1;
statement2;
statement2;

if is reserved word

expression must evaluate to true (nonzero) or


false (0)

statement1 executed if value of expression


true

statement1 bypassed if value false; program


execution moves to statement2

statement2 is executed regardless

One Way Selection


Algorithms Example:
IF grade >= 90 THEN
Student grade = A

Grade
>= 90 ?

True

False

Two Way Selection


Syntax

of two-way (if...else) selection:

if (expression)
statement1;
else
statement2;
If

expression true, statement1


executed, otherwise statement2
executed
else is reserved word

Two Way Selection


Algorithms Example:
Account
< 300?

IF account balance < 300 THEN


service charge = RM1
True
ELSE
service charge = RM2
END IF
Service charge = RM 1

False

Service charge = RM 2

Combined Selection
Each

connected with the logical


operators AND or OR
If the connector AND is used to
combine the conditions, then both
condition must be true for the
combined condition to be true
If the connector OR is used to combine
any two conditions, then only one of
the conditions need to be true for the
combined condition to be considered
true

Combined Selection (AND)


Syntax

of combined selection

(AND)
If (expression1)
AND (expression2)
statement;
If both expression1 and
expression2 are true, statement
will be executed

Combined Selection (AND)


Syntax

of combined selection

(AND)
If (expression1)
AND (expression2)
statement;
If both expression1 and
expression2 are true, statement
will be executed

Combined Selection (AND)


Algorithms

Example:

IF exam marks >= 20


AND project marks >=30 THEN
Grade = pass

Multiple Way Selection

You can choose statement(s) to


run from many sets of choices.
There are two cases for this:

Multi way selection by nested IF


structure
Multi way selection by SWITCH
structure

Multiple Way Selection (Nested


IF)
The

structure that contains


another structure of the same type
is called a nested structure.
In the Nested IF structure, the
statements that exists between IF
and ELSE or between IF and END
IF can contain IF statement.

Multiple Way Selection (Nested


IF)

Syntax
Syntax of
of one
one possible
possible structure:
structure:
IF
IF (condition1)
(condition1) THEN
THEN
Statements1
Statements1
ELSE
ELSE IF
IF (condition2)
(condition2) THEN
THEN
Statements2
Statements2
ELSE
ELSE IF
IF (Condition3)
(Condition3) THEN
THEN
Statements3
Statements3
ELSE
ELSE IF
IF (Condition4)
(Condition4) THEN
THEN
Statements4
Statements4
END
END IF
IF
END
END IF
IF
END
END IF
IF

Note:
Note: The
The nest
nest can
can be
be to
to many
many levels.
levels.
The
The following
following figure
figure shows
shows the
the execution
execution of
of this
this structure.
structure.

Multiple Way Selection (Nested


IF)
True
Statements1
Statements1

Condition1
False
True
Condition2

Statements2
Rest of
algorithm

False
True
Condition3

Statements3

False
Statements4

Multiple Way Selection (Nested


IF)

Example:

Write an algorithm that inputs a student mark


and outputs the corresponding grade, where
grades are as follows:
mark
grade
90-100 A
80-89
B
70-79
C
60-69
D
< 60
E

Multiple Way Selection (Nested


IF)
Algorithm
Algorithm Design
Design
ALGORITHM
ALGORITHM Grades
Grades
INPUT
INPUT mark
mark
IF
IF (( mark
mark << 00 OR
OR mark
mark >> 100
100 )) THEN
THEN
OUTPUT
OUTPUT Mark
Mark out
out of
of range
range
ELSE
ELSE IF
IF (( mark
mark 90
90 AND
AND mark
mark 100
100 )) THEN
THEN
OUTPUT
OUTPUT A
A
ELSE
ELSE IF
IF (( mark
mark 80
80 )) THEN
THEN
OUTPUT
OUTPUT B
B
ELSE
ELSE IF
IF (( mark
mark 70
70 )) THEN
THEN
OUTPUT
OUTPUT C
C
ELSE
ELSE IF
IF (( mark
mark 60
60 )) THEN
THEN
OUTPUT
OUTPUT D
D
ELSE
OUTPUT
ELSE
OUTPUT E
E
END
END IF
IF
END
END IF
IF
END
END IF
IF
END
END IF
IF
END
END IF
IF
END
END Grades
Grades

Multiple Way Selection (Nested


IF)
In

the previous example, only one


statement should be executed for any
value of x.
In the nested case, only one statement
is executed exactly. Therefore, it is
better than the sequence case.

Multiple Way Selection


(SWITCH)
Note that the nest can be done to many levels.

Practically, it is not preferable to have more than 3


nested levels.

The SWITCH control structure is the substitute for


the nested IF structure of many levels of nesting.

The SWITCH control structure is used to select one


of several paths. It is especially useful when the
selection is based on the value of a single variable or
a simple expression (called the case selector).

The case selector may be an integer, character, or


Boolean variable or expression.

Multiple Way Selection


(SWITCH)

Syntax
SWITCH (selector)
CASE label1: Statements1
BREAK
CASE label2: Statements2
BREAK
..
..
..

DEFAULT: Statements_n
END SWITCH

Multiple Way Selection


(SWITCH)
The semantics (execution) of this statement:
If the value of selector equals to one of the labels
values, the statements of that case are executed and
the execution continues to the statement after END
SWITCH.
If the value of the selector does not match with any
of the labels values, the statements in the DEFAULT
case are executed and the execution continues to
the statement that follows END SWITCH.
Note that if you remove BREAK from the cases, the
statements of the matched case are executed and
the execution proceed to the cases that follow.
The following figure shows this execution:

Multiple Way Selection


(SWITCH)
SELECTOR

CASE ..

Statements1
BREAK

CASE ..

DEFAULT

Statements2
BREAK

Rest of
algorithm

Statements_n
BREAK

Multiple Way Selection


(SWITCH)
Example:
Write an algorithm that inputs a character and displays
a suitable musical note (i.e. do, re, mi, etc.)
Analysis stage:

Problem Input:
- a character, musical_note

Problem Output:
- a message showing the corresponding musical
note

Multiple Way Selection


(SWITCH)
Algorithm
Algorithm Design
Design
ALGORITHM
ALGORITHM Music
Music
INPUT
INPUT musical_note
musical_note
SWITCH
SWITCH (musical_note)
(musical_note)
CASE
CASE c
c :: OUTPUT
OUTPUT do
do
BREAK
BREAK
CASE
CASE d
d :: OUTPUT
OUTPUT re
re
BREAK
BREAK
CASE
CASE e
e :: OUTPUT
OUTPUT mi
mi
BREAK
BREAK
CASE
CASE f
f :: OUTPUT
OUTPUT fa
fa
BREAK
BREAK
CASE
CASE g
g :: OUTPUT
OUTPUT sol
sol
BREAK
BREAK
CASE
CASE a
a :: OUTPUT
OUTPUT la
la
BREAK
BREAK
CASE
CASE b
b :: OUTPUT
OUTPUT titi
BREAK
BREAK
DEFAULT:
DEFAULT: OUTPUT
OUTPUT Invalid
Invalid note
note was
was read
read
END
END SWITCH
SWITCH
END
END Music
Music

Multiple Way Selection


(SWITCH)
Example:
Example:
Write
Write an
an algorithm
algorithm to
to use
use SWITCH
SWITCH statement
statement to
to present
present menu
menu asking
asking
the
the user
user to
to enter
enter aa specified
specified letter
letter to
to perform
perform the
the corresponding
corresponding task.
task. The
The
algorithm
algorithm is
is to
to calculate
calculate the
the area
area of
of the
the circle
circle ifif the
the letter
letter aa is
is read,
read, and
and
to
to calculate
calculate the
the circumference
circumference ifif letter
letter cc is
is read.
read.
Analysis
Analysis stage:
stage:

Problem
Problem Input:
Input:
-- radius
radius of
of the
the circle
circle
-- aa character
character to
to perform
perform aa specified
specified task
task

Problem
Problem Output:
Output:
depending
depending on
on the
the character
character read,
read, the
the output
output is:
is:
-- area
area of
of the
the circle
circle
or
or
-- circumference
circumference of
of the
the circle
circle

Multiple Way Selection


(SWITCH)
Algorithm
Algorithm Design
Design
ALGORITHM
ALGORITHM Circle
Circle
OUTPUT
OUTPUT Enter
Enter the
the radius
radius of
of aa circle:
circle:
INPUT
INPUT radius
radius
OUTPUT
OUTPUT Enter
Enter aa to
to calculate
calculate the
the area
area of
of aa circle
circle or
or cc to
to calculate
calculate its
its
circumference:
circumference:
INPUT
INPUT ch
ch
SWITCH
SWITCH (ch)
(ch)
CASE
CASE a
a :: area
area
3.14
3.14 ** radius
radius ** radius
radius
OUTPUT
OUTPUT Area
Area == ,, area
area
BREAK
BREAK
CASE
CASE c
c :: circum
circum
22 ** radius
radius ** 3.14
3.14
OUTPUT
OUTPUT Circumference
Circumference == ,, circum
circum
BREAK
BREAK
DEFAULT:
DEFAULT: OUTPUT
OUTPUT Invalid
Invalid letter
letter was
was read
read
END
END SWITCH
SWITCH
END
END Circle
Circle

Exercise 1
Design

an algorithm that will


receive two integer items and
display to the screen their sum,
difference, product and quotient.
Note that the quotient calculation
(first integer divided by second
integer) is only to be performed if
the second integer does not equal
zero.

Exercise 2

Design an algorithm that will prompt an


operator for a students serial number and the
students exam score out of 100. Your
program is then to match the exam score to a
letter grade and print the grade to the screen.
Calculate the letter grade as follows:
Exam score

Grade

90 and above

80 - 89

70 - 79

60 - 69

Below 60

Conclusion
This

chapter covered the selection


control structure in detail
Depends on the problem you may
use one way, two way and
multiple way selection
For multiple way selection you can
use either IF or SWITCH

REFERENCE
http://www.cs.fsu.edu/~cop3014p/l

ectures/ch4/index.html
Lesley Anne. Simple Program
Design, A Step-by-Step Approach.
th Edition. Thomson Course
4th
Technology.