Download presentation
Presentation is loading. Please wait.
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 RESULT Condition A 1 Condition B AND OR NAND NOR XOR
4
How to construct in structured chart
Structure: a named box with small circle at the top-right corner Selection Action o
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
IF-THEN : Structured Chart
Block X Process S o condition 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 Algorithm: 1……
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 Deduct
* 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
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 : Block X Process R o Process S condition else
12
IF-THEN-ELSE : 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
IF-THEN-ELSE : Example
BEGIN GENDER Selection END GENDER Selection Check Gender malecount = malecount + 1 o femalecount = femalecount + 1 Gender = ‘M’ else IF gender = ‘M’ THEN malecount = malecount +1; ELSE femalecount = femalecount+1; ENDIF
15
Combined IFs Used when there are two or more conditions to be tested
Conditions are connected with logical operators – AND, OR AND / OR table Condition1 Condition2 AND OR T Execute true actions F Execute false actions
16
Combined IFs General structure format : Block X Condition1 [AND][OR]
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
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() Check Applicant Process Application o age > 21 and category = ‘A’ BEGIN CHECKAPPLICANT selection IF age > 21 AND category = ‘A’ THEN DO processapplication() ENDIF END CHECKAPPLICANT selection
19
Combined IFs : Example 2 Discount will be given for sales price between $1 and $100 Discount discAmount = sales_price * disc o sales_price >= 1 and sales_price <=100 Pseudocode is similar to the IF-THEN and IF-THEN-ELSE
20
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 condition1 Block Q Process Q o condition n Block R Block S condition2 …. Process R o Process S
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; 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
Multiple IFs : Example Subject Math Geometry English Biology Message =
MathMark <>-1 Message = Message + “ Math” o GeometryMark <>-1 Message = Message + “ Geometry” o EnglishMark <>-1 Message = Message + “ English” o BiologyMark <>-1 Message = Message + “ Biology” o
25
Multiple IF : Example BEGIN ENGLISH selection
IF EnglishMark <> -1 THEN Message = Message + “ English”; ENDIF END ENGLISH selection BEGIN BIOLOGY selection IF BiologyMark <> -1 THEN “ Biology”; END BIOLOGY selection END SUBJECT sequence BEGIN SUBJECT sequence BEGIN MATH selection IF MathMark <> -1 THEN Message = Message + “ Math”; ENDIF END MATH selection BEGIN GEOMETRY selection IF GeometryMark <> -1 THEN “Geometry”; END GEOMETRY selection
26
Nested IF IF-THEN-ELSE within another IF-THEN_ELSE structure
General structure format : Block X Process Q o Block_R Condition1 else Process R1 o Process R2 Condition2 else
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; Statements - Process R2 will be executed; ENDIF; END BLOCK_R selection 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
BEGIN GRADE selection IF mark>=80 THEN grade = “Distinction”; ELSE BEGIN M_TEST selection IF mark>=65 THEN grade = “Merit”; BEGIN P_TEST selection IF mark>=50 THEN grade = “Pass”; Nested IF : Example Grade mark>=80 else o grade = ‘Distinction’ o M_Test mark>=65 ELSE grade = “Fail”; ENDIF END P_TEST selection END M_TEST selection END GRADE Selection else o o grade = ‘Merit’ P_Test mark>=50 else o o grade = ‘Pass’ grade = ‘Fail’
30
CASE STRUCTURE Another way to express nested IF
If one condition is true, the following conditions will not be tested General structure format : Block X Process R o Process S condition1 Process Q …. else Condition n
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
CASE : Example Commission …… Check code productCode = ‘A’ else =‘B’
10/100 *sales o comm = 15/100 *sales o comm = 20/100*sales o comm = 5/100 * sales o
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 – 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 pseudocode that will read a record on the file, calculate the commission owing for that record, and print the retail price, commission and employee number.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.