Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.

Similar presentations


Presentation on theme: "Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses."— Presentation transcript:

1 Selection Control Structures

2 Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses of simple selection, multiple selection, and nested selection in algorithms Introduce the case construct in pseudocode Develop algorithms using variations of the selection control structure

3 Simple Program Design, Fourth Edition Chapter 4 3 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 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: greater than = equal to<= less than or equal to >= greater than or equal to not equal to The Selection Control Structure

4 Simple Program Design, Fourth Edition Chapter 4 4 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 Only one of the THEN or ELSE paths will be followed, depending on the result of the condition in the IF clause 1 Simple Selection (Simple IF Statement)

5 Simple Program Design, Fourth Edition Chapter 4 5 2 Simple Selection with Null False Branch (Null ELSE Statement) The null ELSE structure is a variation of the simple IF structure 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

6 Simple Program Design, Fourth Edition Chapter 4 6 3 Combined Selection (Combined IF Statement) 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 the conditions, then both conditions 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 needs to be true for the combined condition to be considered true

7 Simple Program Design, Fourth Edition Chapter 4 7 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 Note that the AND and OR operators can also be used with the NOT operator, but great care must be taken and parentheses should be used to avoid ambiguity as illustrated on page 38 of the textbook

8 Simple Program Design, Fourth Edition Chapter 4 8 4 Nested Selection (Nested IF Statement) Nested selection occurs when the word IF appears more than once within an IF statement Nested IF statements can be classified as linear or non-linear

9 Simple Program Design, Fourth Edition Chapter 4 9 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

10 Simple Program Design, Fourth Edition Chapter 4 10 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

11 Simple Program Design, Fourth Edition Chapter 4 11 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

12 Simple Program Design, Fourth Edition Chapter 4 12 Example 4.1 Read Three Characters Design an algorithm that will prompt a terminal operator for three characters, accept those characters as input, sort them into ascending sequence, and output them to the screen A Defining diagram (illustrated on page 40) B Solution algorithm The solution algorithm requires a series of IF statements to sort the three characters into ascending sequence as the code shows on page 41 of the textbook In this solution, most of the logic of the algorithm is concerned with the sorting of the three characters into alphabetic sequence

13 Simple Program Design, Fourth Edition Chapter 4 13 Example 4.1 Read Three Characters C Desk checking Two sets of valid characters will be used to check the algorithm; the characters k, b and g as the first set, and z, s and a as the second Examine the input data, expected results, and desk check table illustrated on page 42 of the textbook Line numbers have been used to identify each statement within the program Note that when desk checking the logic, each IF statement is treated as a single statement

14 Simple Program Design, Fourth Edition Chapter 4 14 Example 4.2 Process Customer Record A program is required to read a customer’s name, a purchase amount, and a fax code. The tax code has been validated and will be one of those listed on page 43 of the textbook. The program must then compute the sales tax and the total amount due, and print the customer’s name, purchase amount, sales tax, and total amount due A Defining diagram (shown on page 43) B Solution algorithm The solution algorithm requires a linear nested IF statement to calculate the sales tax illustrated in the code on page 43 of the textbook

15 Simple Program Design, Fourth Edition Chapter 4 15 Example 4.2 Process Customer Record C Desk checking Two sets of valid input data for purchase amount and tax code will be used to check the algorithm Examine the input data, expected results, and desk check tables shown on page 44 of the textbook As the expected result for the two test cases matches the calculated result the algorithm is correct

16 Simple Program Design, Fourth Edition Chapter 4 16 Example 4.3 Calculate Employee’s Pay A program is required by a company to read an employee’s number, pay rate, and the number of hours worked in a week. The program is then to validate the pay rate and the hours worked fields and, if valid, compute the employee’s weekly pay and print it along with the input data A Defining diagram (shown on page 45) B Solution algorithm The solution to this problem will require a series of simple IF and nested IF statements First, the variables ‘pay_rate’ and ‘hrs_worked’ must be validated, and if either is found to be out of range, an appropriate message is to be placed into a variable called ‘error_message’

17 Simple Program Design, Fourth Edition Chapter 4 17 Example 4. 3 Calculate Employee’s Pay Boolean variables The variable valid_input_fields is a Boolean variable, it may contain only one of two possible values (true or false) When using the IF statement with a Boolean variable, the IF statement can be simplified in pseudocode C Desk checking Two sets of valid input data for pay rate and hours worked will be used to check this algorithm as illustrated in the three tables on pages 46 and 47 of the textbook

18 Simple Program Design, Fourth Edition Chapter 4 18 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 Nested IFs often look cumbersome in pseudocode and depend on correct structure and indentation for readability

19 Simple Program Design, Fourth Edition Chapter 4 19 Example 4.4 Process Customer Record A program is required to read a customer’s name, a purchase amount, and a tax code. The tax code has been validated and will be one of the items listed on page 49 of the textbook. The program must then compute the sales tax and the total amount due, and print the customer’s name, purchase amount, sales tax, and total amount due A Defining diagram (shown on page 49) B Solution algorithm (shown on page 49) The solution algorithm will be expressed using a CASE statement

20 Simple Program Design, Fourth Edition Chapter 4 20 Example 4.4 Process Customer Record C Desk checking Two sets of valid input data for purchase amount and tax code will be used to check the algorithm Note that the case structure serves as a single pseudocode statement Examine the input data, expected results, and desk check tables shown on page 50 of the textbook

21 Simple Program Design, Fourth Edition Chapter 4 21 Summary This chapter covered the selection control structure in detail Descriptions and pseudocode examples were given for simple selection, null ELSE, combined IF, and nested IF statements The case structure was introduced as a means of expressing a linear nested IF statement in a simpler and more concise form


Download ppt "Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses."

Similar presentations


Ads by Google