Chapter 5

advertisement
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 (expression)
statement1;
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:
IF account balance < 300 THEN
service charge = RM1
ELSE
service charge = RM2
END IF
Account
< 300?
False
True
Service charge = RM 1
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 of one possible structure:
IF (condition1) THEN
Statements1
ELSE IF (condition2) THEN
Statements2
ELSE IF (Condition3) THEN
Statements3
ELSE IF (Condition4) THEN
Statements4
END IF
END IF
END IF
Note: The nest can be to many levels.
The following figure shows the execution of this structure.
Multiple Way Selection (Nested IF)
True
Statements1
Condition1
False
True
Statements2
Condition2
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 Design
ALGORITHM Grades
INPUT mark
IF ( mark < 0 OR mark > 100 ) THEN
OUTPUT “ Mark out of range”
ELSE IF ( mark  90 AND mark  100 ) THEN
OUTPUT “A”
ELSE IF ( mark  80 ) THEN
OUTPUT “B”
ELSE IF ( mark  70 ) THEN
OUTPUT “C”
ELSE IF ( mark  60 ) THEN
OUTPUT “D”
ELSE
OUTPUT “E”
END IF
END IF
END IF
END IF
END IF
END 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 Design
ALGORITHM Music
INPUT musical_note
SWITCH (musical_note)
CASE ‘c’ : OUTPUT “ do “
BREAK
CASE ‘d’ : OUTPUT “ re “
BREAK
CASE ‘e’ : OUTPUT “ mi “
BREAK
CASE ‘f’ : OUTPUT “ fa “
BREAK
CASE ‘g’ : OUTPUT “ sol “
BREAK
CASE ‘a’ : OUTPUT “ la “
BREAK
CASE ‘b’ : OUTPUT “ ti “
BREAK
DEFAULT: OUTPUT “ Invalid note was read “
END SWITCH
END Music
Multiple Way Selection (SWITCH)
Example:
Write an algorithm to use SWITCH statement to present menu asking
the user to enter a specified letter to perform the corresponding task. The
algorithm is to calculate the area of the circle if the letter a is read, and
to calculate the circumference if letter c is read.
Analysis stage:

Problem Input:
- radius of the circle
- a character to perform a specified task

Problem Output:
depending on the character read, the output is:
- area of the circle
or
- circumference of the circle
Multiple Way Selection (SWITCH)
Algorithm Design
ALGORITHM Circle
OUTPUT “ Enter the radius of a circle: “
INPUT radius
OUTPUT “ Enter a to calculate the area of a circle or c to calculate its
circumference:”
INPUT ch
SWITCH (ch)
CASE ‘a’ : area  3.14 * radius * radius
OUTPUT “ Area = “ , area
BREAK
CASE ‘c’ : circum  2 * radius * 3.14
OUTPUT “ Circumference = “ , circum
BREAK
DEFAULT: OUTPUT “ Invalid letter was read “
END SWITCH
END 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 student’s serial number and the student’s 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
A
80 - 89
B
70 - 79
C
60 - 69
D
Below 60
F
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/lect
ures/ch4/index.html
 Lesley Anne. Simple Program Design, A
Step-by-Step Approach. 4th Edition.
Thomson Course Technology.

Download