1 / 16

Selection Control Structures

4. Selection Control Structures. Simple Program Design Third Edition A Step-by-Step Approach. 4. Objectives. To elaborate on the uses of simple selection, multiple selection, and nested selection in algorithms To introduce the case construct in pseudocode

gerek
Télécharger la présentation

Selection Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4 Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach

  2. 4 Objectives • To elaborate on the uses of simple selection, multiple selection, and nested selection in algorithms • To introduce the case construct in pseudocode • To develop algorithms using variations of the selection control structure

  3. 4 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 • This ‘condition’ is based on a comparison of two items, and is usually expressed with one of the following relational operators: • < less than – > greater than • = equal to – <= less than or equal to • >=greater than or equal to – <> not equal to

  4. 4 Simple Selection (Simple IF Statement) • Simple selection occurs when a choice is made between two alternative paths, depending on the result of a condition being true or false • For example: IF account_balance < $300 THEN service_charge = $5.00 ELSE service_charge = $2..00 ENDIF

  5. 4 Simple Selection with Null False Branch (Null ELSE Statement) • The null ELSE statement is a variation of the simple IF structure • The null ELSE statement is used when a task is performed only when a particular condition is true IF student_attendance = part_time THEN add 1 to part_time_count ENDIF

  6. 4 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 conditions are combined using the connector AND, both conditions must be true for the combined condition to be true • Pseudocode algorithms should never be ambiguous • There are not precedence rules for logical operators in psuedocode, but there are precedence rules in most programming languages • Parentheses must be used in pseudocode to avoid ambiguity as to the meaning intended

  7. 4 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 • The AND and OR operators can also be used with the NOT operator, but great care must be taken and parentheses must be used to avoid ambiguity

  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. 4 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. 4 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 statements may be separated from the IF statement with which it is paired • Indentation is important when expressing this form of selection in pseudocode • There are an equal number of IF conditions as ELSE and ENDIF statements

  11. 4 Algorithms Using Selection • In the following example the problem will be defined, a solution algorithm will be developed, and the algorithm will be manually tested • To help define the problem, the processing verbs in the example have been underlined

  12. 4 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 (see figure on page 37 of the textbook) • B Solution algorithm • The solution algorithm requires a series of IF statements to sort the three characters into ascending sequence

  13. 4 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 set (i) Input data: (see figure on page 38 of the textbook) (ii) Expected results: (see figure at top of page 39 of the textbook) (iii) Desk check table: (see second figure on page 39 of the textbook)

  14. 4 Boolean Variables • The variable valid_input_fields is a Boolean variable; it may contain only one of two possible values (true or false) • The variable valid_input_fields acts as an internal switch or flag to the program • It will initially be set to true, and will be assigned the value false if one of the input fields is found to be invalid

  15. 4 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 • It makes the pseudocode easier to write and understand • This linear nested IF structure can be replaced with a case control structure • The path followed in the case structure depends on the value of the variable specified in the CASE OF clause

  16. 4 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

More Related