1 SELECTION STRUCTURE. 2 Also known as decision structure. Selective structure allows to choose...

Preview:

Citation preview

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 0 0 1 1

Condition B 0 1 0 1

AND 0 0 0 1

OR 0 1 1 1

NAND 1 1 1 0

NOR 1 0 0 0

XOR 0 1 1 0

4

How to construct in structured chart

Structure: a named box with small circle at the top-right corner

Selection Actiono

5

Selective Structure

Selection can represents a single-alternative, dual-alternatives or more alternatives

These number of variations represented using the following structures:1. IF-THEN2. IF-THEN-ELSE3. Combined IFs4. Multiple IFs5. Nested Ifs6. 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 So

condition

BEGIN BLOCK-DESCRIPION SelectionIF condition THEN

Statement Process S will be executed; ENDIF;

END BLOCK-DESCRIPTION Selection

IF-THEN : Pseudocode

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/100new_value = value - discount

9

IF-THEN : Example

ProcessDiscount

DeductDiscount

new_value=value-discount

discount= value * dis_rate/100

value >= bulk_order_level

o

10

IF-THEN : ExampleBEGIN DISCOUNT selection

IF value >= bulk_order_level THENBEGIN DEDUCTDISCOUNT sequence

discount = value * dis_rate/100

new_value = value – discountEND DEDUCTDISCOUNT sequence

END IFEND 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 Ro

Process So

condition else

12

IF-THEN-ELSE : Pseudocode

Pseudocode :BEGIN BLOCK-DESCRIPION Selection

IF condition THENStatement – Process R will be executed ;

ELSEStatement – Process S will be executed;

ENDIFEND BLOCK-DESCRIPTION Selection

IND

EN

TE

D

13

IF-THEN-ELSE : Example

Problem : counting a number of male and a number of female in a group of studentsAlgorithm:1. malecount = 0, femalecount = 02. ………..3. IF gender = ‘M’ Then

malecount = malecount + 1 ELSE

femalecount = femalecount + 1

14

Check Gender

malecount = malecount + 1

ofemalecount = femalecount + 1

o

Gender = ‘M’else

IF-THEN-ELSE : Example

BEGIN GENDER Selection

END GENDER Selection

IF gender = ‘M’ THENmalecount = malecount +1;

ELSEfemalecount = femalecount+1;

ENDIF

15

Combined IFs

Condition1 Condition2 AND OR

T T Execute true actions Execute true actions

T F Execute false actions Execute true actions

F T Execute false actions Execute true actions

F F Execute false actions Execute false actions

Used when there are two or more conditions to be tested

Conditions are connected with logical operators – AND, OR

AND / OR table

16

Combined IFs General structure format :

Block X

Process Ro

Process So

Condition1[AND][OR]condition2 else

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

Check Applicant

Process Applicationo

age > 21 and category = ‘A’

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:

BEGIN CHECKAPPLICANT selectionIF age > 21 AND category = ‘A’ THEN

DO processapplication()ENDIF

END CHECKAPPLICANT selection

1…….2. IF age > 21 AND category = ‘A’ Then

Do processApplication()

19

Combined IFs : Example 2

1. 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 Qo

condition n

Block R Block S

condition2

….

Process Ro

Process So

21

Multiple Ifs : Pseudocode

BEGIN BLOCK-DESCRIPION sequenceBEGIN BLOCK_Q selection

IF condition1 THENStatement - Process Q will be executed ;

ENDIFEND BLOCK_Q selectionBEGIN 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 Algorithm1. …..2. If MathMark <> -1 Then

Message = Message + “ Math”

3. If GeometryMark <> -1 Then Message = Message + “ Geometry”

4. If EnglishMark <> -1 Then Message = Message + “ English”

5. If BiologyMark <> -1Then Message = Message + “ Biology”

24

Multiple IFs : Example

Subject

GeometryMath

MathMark<>-1

Message = Message +

“ Math”

o

GeometryMark<>-1

Message = Message +

“ Geometry”

o

English

EnglishMark<>-1

Message = Message + “ English”

o

Biology

BiologyMark<>-1

Message = Message + “ Biology”

o

25

Multiple IF : Example

BEGIN SUBJECT sequence BEGIN MATH selection

IF MathMark <> -1 THENMessage = 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

Nested IF IF-THEN-ELSE within another IF-THEN_ELSE

structure General structure format :

Block X

Process Qo

Block_Ro

Condition1 else

Process R1o

Process R2o

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; 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 selectionENDIF

END GRADE Selection

Grade

grade = ‘Distinction’

o

mark>=80 else

mark>=65 else

grade = ‘Pass’

ograde =

‘Fail’

omark>=50 else

M_Test

grade = ‘Merit’

P_Test oo

o

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 Ro

Process S

condition1

Process Qo

….

o

elseCondition n

31

Pseudocode – CASE

BEGIN BLOCK_DESCRIPTION selectionSELECT expressionCASE condition1

Statements - Process Q will be executed;CASE conditionn

Statements - Process R will be executed; ELSE

Statements - Process S will be executed; ENDCASEEND 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

1. ..2. 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

comm = 15/100 *sales

o

productCode = ‘A’

comm = 10/100 *sales

oelse=‘B’

comm =20/100*sales

o comm =5/100 * sales

o=‘C’

…… Check code

Commission

35

CASE : Example…….BEGIN CHECKCODE selection

SELECT productCodeCASE ‘A’:

comm = 10/100 * sales;CASE ‘B’:

comm = 15/100 * sales; CASE ‘C’:

comm = 20/100 * sales; ELSE

comm = 5/100 * sales; ENDCASEEND 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 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.

Recommended