Transcript 9

Multi Way Selection
•
•
You can choose statement(s) to run from
many sets of choices.
There are two cases for this:
(a) Multi way selection by nested IF
structure
(b) Multi way selection by SWITCH structure
Multi Way Selection by Nested IF Structure
• 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.
Multi Way Selection by Nested If Structure .. Cont.
• 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.
Multi Way Selection by Nested If Structure .. Cont.
True
Statements1
Condition1
False
True
Statements2
Condition2
Rest of
algorithm
False
True
Condition3
Statements3
False
Statements4
Multi Way Selection by Nested If Structure .. Examples
• Example 4:
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
Example 4 .. Cont.
1- Analysis stage:
• Problem Input:
- student’s mark, mark
• Problem Output:
- grade
• Criteria
- according to the previous grade table
Example 4 .. Cont.
2- 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
Comparison of Nested IF Structure and a Sequence of IF Structures
•
•
Some times the nested IF structure is a good solution for a given
problem than a sequence of IF structures.
Consider the following example:
A) Sequence case:
IF ( x > 0 ) THEN
pos_count  pos_count + 1
END IF
IF ( x < 0 ) THEN
neg_count  neg_count + 1
END IF
IF ( x = 0 ) THEN
zero_count  zero_count + 1
END IF
(B) Nested case:
IF ( x > 0 ) THEN
pos_count  pos_count + 1
ELSE IF ( x < 0 ) THEN
neg_count  neg_count + 1
ELSE
zero_count  zero_count + 1
END IF
END IF
• In the previous example, only one statement should be
executed for any value of x.
• In the sequence case, the sequence does not show
clearly that exactly one of the three statements is
executed for any value of x.
• In the nested case, only one statement is executed
exactly. Therefore, it is better than the sequence case.
Multi Way Selection by SWITCH Structure
• 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.
Multi Way Selection by SWITCH Structure
• Syntax
SWITCH (selector)
CASE label1: Statements1
BREAK
CASE label2: Statements2
BREAK
.
.
.
DEFAULT: Statements_n
END SWITCH
Multi Way Selection by SWITCH Structure .. Cont.
• 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:
Multi Way Selection by SWITCH Structure .. Cont.
selector
label1
Statements1
BREAK
DEFUALT
label2
Statements2
BREAK
Rest of
algorithm
…
Statements_n
BREAK
Multi Way Selection by SWITCH Structure Examples
• Example 5:
Write an algorithm that inputs a character and displays a
suitable musical note (i.e. do, re, mi, etc.)
1- Analysis stage:
• Problem Input:
- a character, musical_note
Problem Output:
- a message showing the corresponding musical note
Multi Way Selection by SWITCH Structure Examples.. Cont.
2- 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
Multi Way Selection by SWITCH Structure Examples.. Cont.
• Example 6
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.
1- 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
Multi Way Selection by SWITCH Structure Examples.. Cont.
2- 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