Chapter 4

advertisement
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.1 The Selection Control Structure
You can use the selection control structure in pseudocode to illustrate a
choice between two or more actions, depending on whether a condition is
true or false.
General Format:
IF [condition]
THEN
[true actions]
ELSE
[false actions]
ENDIF
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
= equal to
>= greater than or equal to
>
<=
<>
greater than
less than or equal to
not equal to
When the condition following the IF is true only the actions following the
THEN and before the ELSE are performed.
When the condition following the IF is false only the actions following the
ELSE and before the ENDIF are performed.
There are a number of variations of the IF statement.
1
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.4.1. Simple Selection (simple IF statement)
Simple selection occurs when a choice is made between two alternate paths,
depending on the result of a condition being true or false.
The structure is represented in pseudocode using the keywords IF, THEN,
ELSE and ENDIF.
Example:
IF Gender = “M”
THEN
Add 1 to Male-Count
ELSE
Add 1 to Female-Count
ENDIF
4.1.2. Simple Selection with Null False Branch (null ELSE statement)
It is used when a task is performed only when a particular condition is true.
If the condition is false, then no processing will take place and the IF
statement will be bypassed.
The ELSE clause is not used.
Example:
IF Age > 18
THEN
Add 1 to Adult-Count
ENDIF
2
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.1.3. Combined Selection (combined IF statement)
The AND and OR operators
A combined IF statement is one that contains multiple conditions, each
connected with the logical operators AND or OR.
If the connector AND is used to combine two conditions, then both
conditions must be true for the combined condition to be true.
If the connector OR is used to combine two conditions, then only one of the
conditions needs to be true for the combined condition to be considered true.
Examples:
IF Age > 18 AND Gender = “F”
THEN
Add 1 to Female-Adult-Count
ENDIF
IF Age < 12 OR Age > 65
THEN
Add 1 to Half-Price-Count
ENDIF
The NOT operator
The NOT operator can be used for the logical negation of a condition as
follows:
IF NOT (Record-Code = ’23’)
THEN
Update customer record
ENDIF
3
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.1.4. Nested Selection (nested IF statement)
A nested selection occurs when the word IF appears more than once within
an IF statement.
A nested IF statements can be classified as linear or non-linear.
4.1.4.1
Linear nested IF statements
The linear nested IF statement is used when a field is being tested for
various values and a different action is to be taken for each value.
This form of nested IF is called linear, because each ELSE immediately
follows the IF condition to which it corresponds.
Example:
Transaction Code
A
C
D
Any other code
Action
Add a new customer record
Change customer record
Delete customer record
Display error message
IF Tran-Code = “A”
THEN
Add a new customer record
ELSE
IF Tran-Code = “C”
THEN
Change customer record
ELSE
IF Tran-Code = “D”
THEN
Delete customer record
ELSE
Display error message
ENDIF
ENDIF
ENDIF
4
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.1.4. Nested Selection (nested IF statement) – continued
4.1.4.2. Non-Linear nested IF statements
A non-linear nested IF occurs when a number of different conditions need to
be satisfied before a particular action can occur.
It is termed non-linear because the ELSE statement may be separated from
the IF statement with which it is paired.
Example:
Condition 1
Salaried
Employee
Hourly
Employee
Hourly
Employee
Condition 2
Action
Pay base salary
Hours <= 40
Calculate pay at hourly rate
Hours > 40
Calculate pay with overtime
IF Employee-Code = “H”
THEN
IF Hours-Worked <= 40
THEN
Calculate pay at hourly rate
ELSE
Calculate pay with overtime
ENDIF
ELSE
Pay base salary
ENDIF
5
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.2 Algorithms Using Selection
Let us look at some programming examples that use the selection control
structure.
In each example, the problem will be defined, a solution algorithm will be
developed and the algorithm will be manually tested.
Examine each of the solutions as illustrated in Examples 4.1 through 4.3 on
pages 40 through 47 of the textbook.
6
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.3 The Case Structure
The CASE control structure in pseudocode is another way of expressing a
linear nested IF statement.
It is used in pseudocode for two reasons:
It can be directly translated into many high-level languages, and
It makes the pseudocode easier to write and understand.
Using nested IF statements in pseudocode often look cumbersome and
depend on correct structure and indentation for readability.
General format:
CASE OF variable-name
VALUE-1: statement block-1
VALUE-2: statement block-2
VALUE-3: statement block-3
.
.
.
VALUE-n: statement block-n
VALUE-OTHER: statement block-other
ENDCASE
The value of the “variable-name” is compared to each VALUE-n from the
top down.
When there is an equal condition the corresponding “statement block” is
executed.
If there is no match the VALUE-OTHER “statement block” is executed.
7
Simple Program Design, 4th. Ed., L. Robertson
Chapter 4 – Selection Control Structures
4.3 The Case Structure – continued
Example:
Condition
Age < 3
Age < 13
Age < 20
Age > 64
Other
Action
Ticket Price = 25% of standard fare
Ticket Price = 50% of standard fare
Ticket Price = 150% of standard fare
Ticket Price = 50% of standard fare
Ticket Price = 100% of standard fare
CASE OF age
< 3: Calculate Ticket Price = 25% of standard fare
< 13: Calculate Ticket Price = 50% of standard fare
< 20: Calculate Ticket Price = 150% of standard fare
> 64: Calculate Ticket Price = 50% of standard fare
VALUE_OTHER: Calculate Ticket Price = 100% of standard fare
ENDCASE
Examine the program statement and the appropriate solution illustrated in
Example 4.4 on pages 49 through 50 of the textbook.
8
Download