Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
if is reserved word
Grade
>= 90 ?
True
False
if (expression)
statement1;
else
statement2;
If
False
Service charge = RM 2
Combined Selection
Each
of combined selection
(AND)
If (expression1)
AND (expression2)
statement;
If both expression1 and
expression2 are true, statement
will be executed
of combined selection
(AND)
If (expression1)
AND (expression2)
statement;
If both expression1 and
expression2 are true, statement
will be executed
Example:
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.
Condition1
False
True
Condition2
Statements2
Rest of
algorithm
False
True
Condition3
Statements3
False
Statements4
Example:
Syntax
SWITCH (selector)
CASE label1: Statements1
BREAK
CASE label2: Statements2
BREAK
..
..
..
DEFAULT: Statements_n
END SWITCH
CASE ..
Statements1
BREAK
CASE ..
DEFAULT
Statements2
BREAK
Rest of
algorithm
Statements_n
BREAK
Problem Input:
- a character, musical_note
Problem Output:
- a message showing the corresponding musical
note
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
Exercise 1
Design
Exercise 2
Grade
90 and above
80 - 89
70 - 79
60 - 69
Below 60
Conclusion
This
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.