1 / 38

CHAPTER 3

CHAPTER 3. SELECTION STRUCTURE. Introduction . Also known as decision structure. Selective structure allows to choose one of the many possibilities/options, depending on whether a condition is true or false .

kenda
Download Presentation

CHAPTER 3

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. CHAPTER 3 SELECTION STRUCTURE

  2. Introduction • Also known as decision structure. • Selective structure allows to choose one of the many possibilities/options, depending on whether a condition is true or false. • The condition in the selection is based on the comparison of two items, and is usually expressed with one of the relational operators.

  3. E.g. If JobVacant = “yes” AND QualificationMet = “yes” then action= “accepted”, else action = “rejected”. Relational operators = Equal To < Less Than <= Less Than Equal To > Greater Than >= Greater Than Equal To <> Not Equal To

  4. o Selection Action How to construct in structured chart • Structure: a named box with small circle at the top-right corner

  5. Selective Structure • Selection can represents a single-alternative, dual-alternatives or more alternatives • These number of variations represented using the following structures: • IF-THEN • IF-THEN-ELSE • Combined IFs • Multiple IFs • Nested Ifs • CASE

  6. IF-THEN • Used where a task is carried out only when a particular condition is true. • if the condition is false, no processing takes place – Condition will be bypassed. • For Example: In a situation where a person who is 12 years old or older must purchase a ticket of $1 to enter the playground and children below 12 can go for free. IF age >= 12 THEN ticket = 1.00

  7. Block X condition o Process S IF-THEN : Structured Chart • IF-THEN : Pseudocode BEGIN BLOCK-DESCRIPION Selection IF condition THEN Statement Process S will be executed; ENDIF; END BLOCK-DESCRIPTION Selection

  8. IF-THEN : Example Problem : A discount is given if the value is greater or equals bulk order level Algorithm: 1…… 2. If value >= bulk_order_level Then discount = value * dis_rate/100 new_value = value - discount

  9. IF-THEN : Example Process Discount value >= bulk_order_level o Deduct Discount discount= value * dis_rate/100 new_value= value-discount

  10. IF-THEN : Example BEGIN DISCOUNT selection IF value >= bulk_order_level THEN BEGIN DEDUCTDISCOUNT sequence discount = value * dis_rate/100 new_value = value – discount END DEDUCTDISCOUNT sequence END IF END DISCOUNT selection

  11. Block X condition else o o Process R Process S IF-THEN-ELSE • Used when a choice is to be made between 2 alternative paths depending on the result of the condition being true or false. • General structure format :

  12. IF-THEN-ELSE : Pseudocode Pseudocode : BEGIN BLOCK-DESCRIPION Selection IF condition THEN Statement – Process R will be executed ; ELSE Statement – Process S will be executed; ENDIF END BLOCK-DESCRIPTION Selection INDENTED

  13. IF-THEN-ELSE : Example Problem : counting a number of male and a number of female in a group of students Algorithm: 1. malecount = 0, femalecount = 0 2. ……….. 3. IF gender = ‘M’ Then malecount= malecount + 1 ELSE femalecount= femalecount + 1

  14. Check Gender Gender = ‘M’ else o o malecount = malecount + 1 femalecount = femalecount + 1 IF-THEN-ELSE : Example BEGIN GENDER Selection END GENDER Selection IF gender = ‘M’ THEN malecount = malecount +1; ELSE femalecount = femalecount+1; ENDIF

  15. Used when there are two or more conditions to be tested Conditions are connected with logical operators – AND, OR AND / OR table Combined IFs

  16. Combined IFs • General structure format : Block X Condition1 [AND][OR] condition2 else o o Process R Process S

  17. Combined IFs : Pseudocode BEGIN BLOCK-DESCRIPION Selection IF condition 1 [AND] [OR] condition 2 THEN Statement – Process R will be executed; [ELSE] [Statement – Process S will be executed;] ENDIF END BLOCK-DESCRIPTION Selection

  18. Check Applicant age > 21 and category = ‘A’ o Process Application Combined IFs : Example 1 The application will be processed only if the applicant is above 21 years old and he/she is in category ‘A’. Algorithm: Structured chart:Pseudocode: 1……. 2. IF age > 21 AND category = ‘A’ Then Do processApplication() BEGIN CHECKAPPLICANT selection IF age > 21 AND category = ‘A’ THEN DO processapplication() ENDIF END CHECKAPPLICANT selection

  19. Discount sales_price >= 1 and sales_price <=100 o discAmount = sales_price * disc Combined IFs : Example 2 • Discount will be given for sales price between $1 and $100 Pseudocode is similar to the IF-THEN and IF-THEN-ELSE

  20. Block Q Block R Block S …. condition n condition1 condition2 o o o Process Q Process R Process S Multiple IFs • Using two or more IF-THEN statement • All conditions will be tested even though the first IF-THEN condition is true. • General structure format : Block X

  21. Multiple Ifs : Pseudocode BEGIN BLOCK-DESCRIPION sequence BEGIN BLOCK_Q selection IF condition1 THEN Statement - Process Q will be executed ; ENDIF END BLOCK_Q selection BEGIN BLOCK-R selection IF condition2 THEN Statement - Process R will be executed; ENDIF; END BLOCK_R selection … BEGIN BLOCK-S selection IF condition n THEN Statement - Process S will be executed; ENDIF; END BLOCK-S selection END BLOCK-DESCRIPTION sequence

  22. Multiple IF : Example • Displaying Subjects taken by a student. Check each of the subject, whether the mark is -1, if yes, meaning they are not taking that subject. Else, it would show the subject name in the message.

  23. Multiple IF : Example Algorithm • ….. • If MathMark <> -1 Then • Message = Message + “ Math” • If GeometryMark <> -1 Then • Message = Message + “ Geometry” • If EnglishMark <> -1 Then • Message = Message + “ English” • If BiologyMark <> -1Then • Message = Message + “ Biology”

  24. MathMark <>-1 GeometryMark <>-1 BiologyMark <>-1 EnglishMark <>-1 Geometry Math Biology English o o o o Message = Message + “ Geometry” Message = Message + “ Biology” Message = Message + “ Math” Message = Message + “ English” Multiple IFs : Example Subject

  25. Multiple IF : Example BEGIN SUBJECT sequence BEGIN MATH selection IF MathMark <> -1 THEN Message = Message + “ Math”; ENDIF END MATH selection BEGIN GEOMETRY selection IF GeometryMark <> -1 THEN Message = Message + “Geometry”; ENDIF END GEOMETRY selection BEGIN ENGLISH selection IF EnglishMark <> -1 THEN Message = Message + “ English”; ENDIF END ENGLISH selection BEGIN BIOLOGY selection IF BiologyMark <> -1 THEN Message = Message + “ Biology”; ENDIF END BIOLOGY selection END SUBJECT sequence

  26. Block X Condition1 else o o Process Q Block_R Condition2 else o o Process R1 Process R2 Nested IF • IF-THEN-ELSE within another IF-THEN_ELSE structure • General structure format :

  27. Pseudocode – nested IF BEGIN BLOCK-DESCRIPION selection IF condition1 THEN Statements - Process Q will be executed; ELSE BEGIN BLOCK_R selection IF condition2 THEN Statements - Process R1 will be executed; ELSE Statements - Process R2 will be executed; ENDIF; END BLOCK_R selection ENDIF; END BLOCK-DESCRIPTION Selection

  28. Nested IF : Example • Consider a grading problem below. If the student mark is • 80 and above, grade is distinction. • less than 80 but more than or equal to 65, grade is Merit • Less than 65 but more than or equal to 50, grade is Pass • Below 50, grade is Fail

  29. Nested IF : Example BEGIN GRADE selection IF mark>=80 THEN grade = “Distinction”; ELSE BEGIN M_TEST selection IF mark>=65 THEN grade = “Merit”; ELSE BEGIN P_TEST selection IF mark>=50 THEN grade = “Pass”; ELSE grade = “Fail”; ENDIF END P_TEST selection ENDIF END M_TEST selection ENDIF END GRADE Selection Grade else mark>=80 o o M_Test grade = ‘Distinction’ mark>=65 else o o grade = ‘Merit’ P_Test else mark>=50 o o grade = ‘Pass’ grade = ‘Fail’

  30. Block X o o Process R Process Q condition1 Condition n else o Process S …. CASE STRUCTURE • Another way to express nested IF • If one condition is true, the following conditions will not be tested • General structure format :

  31. Pseudocode – CASE BEGIN BLOCK_DESCRIPTION selection SELECT expression CASE condition1 Statements - Process Q will be executed; CASE conditionn Statements - Process R will be executed; ELSE Statements - Process S will be executed; ENDCASE END BLOCK_DESCRIPTION selection

  32. CASE : Example • commission is determined by the product code • Product code ‘A’, 10% on sales • Product code ‘B’, 15% on sales • Product code ‘C’, 20% on sales • others, 5% on sales

  33. CASE : Example Algorithm • .. • CASE product_code = ‘A’: comm = 10/100 * sales CASE product_code = ‘B’: comm = 15/100 * sales CASE product_code = ‘C’: comm = 20/100 * sales ELSE comm = 5/100 * sales

  34. o o o o comm = 15/100 *sales comm = 10/100 *sales comm = 20/100*sales comm = 5/100 * sales CASE : Example Commission …… Check code productCode = ‘A’ else =‘B’ =‘C’

  35. CASE : Example ……. BEGIN CHECKCODE selection SELECT productCode CASE ‘A’: comm = 10/100 * sales; CASE ‘B’: comm = 15/100 * sales; CASE ‘C’: comm = 20/100 * sales; ELSE comm = 5/100 * sales; ENDCASE END COMMISSION selection

  36. Class Exercise 1 • A program is required to accept an object’s length and width and then decide whether it is a square or a rectangle. Do the algorithm and structured chart.

  37. Class Exercise 2 • The Fantastic Floral Company sells to wholesale and retail buyers. The wholesale buyer needs a resale number in order to buy at no tax and receive discounts. The retail buyer pays 6% tax. The following are the discounts to the wholesale buyer: Amount Discount less $100 2% $100 – less $500 5% $500 or more 10% Given an amount of purchase and buyer type, how much will the customer owe the company? Using stepwise refinement method, do problem analysis, structured chart and pseudocode

  38. Class Exercise 3 • A transaction record on a sales commission file contains the retail price of an item sold, a transaction code that indicates the sales commission category to which an item can belong, and the employee number of the person who sold the item. The transaction code can contain the values S, M, L, which indicate that the percentage commission will be 5%, 7% or 10%, respectively. Using stepwise refinement method, do problem analysis, structured chart and pseudocodethat will read a record on the file, calculate the commission owing for that record, and print the retail price, commission and employee number.

More Related