Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.

Similar presentations


Presentation on theme: "PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION."— Presentation transcript:

1 PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION

2 CONTENTS One way selection Two way selection Multiple way selection One way selection Two way selection Multiple way selection

3 What is Selection? Structure in pseudocode to illustrate a choice between two or more actions, depending on whether a condition is true or false

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

5 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 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

6 One Way Selection Algorithms Example: IF grade >= 90 THEN Student grade = A Algorithms Example: IF grade >= 90 THEN Student grade = A Grade >= 90 ? False True

7 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 Syntax of two-way (if...else) selection: if (expression) statement1; else statement2; If expression true, statement1 executed, otherwise statement2 executed else is reserved word

8 Two Way Selection Algorithms Example: IF account balance < 300 THEN service charge = RM1 ELSE service charge = RM2 END IF Algorithms Example: IF account balance < 300 THEN service charge = RM1 ELSE service charge = RM2 END IF Account < 300? Service charge = RM 1 Service charge = RM 2 False True

9 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 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

10 Combined Selection (AND) Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed

11 Combined Selection (AND) Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed Syntax of combined selection (AND) If (expression1) AND (expression2) statement; If both expression1 and expression2 are true, statement will be executed

12 Combined Selection (AND) Algorithms Example: IF exam marks >= 20 AND project marks >=30 THEN Grade = pass Algorithms Example: IF exam marks >= 20 AND project marks >=30 THEN Grade = pass

13 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 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

14 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. 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.

15 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 Note: The nest can be to many levels. The following figure shows the execution of this structure. 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 Note: The nest can be to many levels. The following figure shows the execution of this structure.

16 Multiple Way Selection (Nested IF) Condition1 Condition2 Statements2 Statements3 Rest of algorithm Statements1 Condition3 Statements4 True False

17 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-89B 70-79C 60-69D < 60E 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-89B 70-79C 60-69D < 60E

18 Multiple Way Selection (Nested IF) Algorithm Design ALGORITHM Grades INPUT mark IF ( 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 Grades Algorithm Design ALGORITHM Grades INPUT mark IF ( 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 Grades

19 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. 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.

20 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. 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.

21 Multiple Way Selection (SWITCH) Syntax SWITCH (selector) CASE label1: Statements1 BREAK CASE label2: Statements2 BREAK. DEFAULT: Statements_n END SWITCH Syntax SWITCH (selector) CASE label1: Statements1 BREAK CASE label2: Statements2 BREAK. DEFAULT: Statements_n END SWITCH

22 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: 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:

23 Multiple Way Selection (SWITCH) Statements1 BREAK Statements2 BREAK Statements_n BREAK … Rest of algorithm CASE.. DEFAULT SELECTOR CASE..

24 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 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

25 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 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

26 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 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

27 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 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

28 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.

29 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 scoreGrade 90 and aboveA 80 - 89B 70 - 79C 60 - 69D Below 60F

30 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 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

31 REFERENCE http://www.cs.fsu.edu/~cop3014p/lect ures/ch4/index.html http://www.cs.fsu.edu/~cop3014p/lect ures/ch4/index.html Lesley Anne. Simple Program Design, A Step-by-Step Approach. 4 th Edition. Thomson Course Technology. http://www.cs.fsu.edu/~cop3014p/lect ures/ch4/index.html http://www.cs.fsu.edu/~cop3014p/lect ures/ch4/index.html Lesley Anne. Simple Program Design, A Step-by-Step Approach. 4 th Edition. Thomson Course Technology.


Download ppt "PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION."

Similar presentations


Ads by Google