Slide 3 - dhimas ruswanto

Report
SELECTION
STRUCTURE
1



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.
2

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
3

Structure: a named box with small circle at
the top-right corner
Selection Action
o
4


Selection can represents a singlealternative, dual-alternatives or more
alternatives
These number of variations represented
using the following structures:
1.
2.
3.
4.
5.
6.
IF-THEN
IF-THEN-ELSE
Combined IFs
Multiple IFs
Nested Ifs
CASE
5



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
6
Block X
condition
o
Process S
BEGIN BLOCK-DESCRIPION Selection
IF condition THEN
Statement Process S will be executed;
ENDIF;
END BLOCK-DESCRIPTION Selection
7
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
8
Process
Discount
value >= bulk_order_level
o
Deduct
Discount
discount= value
* dis_rate/100
new_value=
value-discount
9
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
10

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
condition
Process R
else
o
Process S
o
11
INDENTED
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
12
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
13
BEGIN GENDER Selection
Check Gender
Gender = ‘M’
else
IF gender = ‘M’ THEN
malecount = malecount +1;
ELSE
femalecount = femalecount+1;
ENDIF
o
o
malecount =
femalecount = END GENDER Selection
malecount + 1 femalecount + 1
14



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
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
15

General structure format :
Block X
Condition1
[AND][OR]
condition2
Process R
else
o
Process S
o
16
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
17
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:
1…….
2. IF age > 21 AND category = ‘A’ Then
Do processApplication()
Structured chart:
Check Applicant
age > 21 and
category = ‘A’
o
Pseudocode:
BEGIN CHECKAPPLICANT selection
IF age > 21 AND category = ‘A’ THEN
DO processapplication()
ENDIF
END CHECKAPPLICANT selection
Process Application
18
1.
Discount will be given for sales price between
$1 and $100
Discount
sales_price >= 1 and
sales_price <=100
discAmount = o
sales_price * disc
Pseudocode is similar to the IF-THEN and IF-THEN-ELSE
19

Using two or more IF-THEN statement
 All conditions will be tested even though the first IFTHEN condition is true.
 General structure format :
Block X
Block Q
condition1
o
Process Q
Block R
….
condition2
o
Process R
Block S
condition n
Process S
o
20
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
21

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.
22
1. …..
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”
23
Subject
Math
MathMark
<>-1
o
Message =
Message +
“ Math”
Geometry
GeometryMark
<>-1
o
Message =
Message +
“ Geometry”
English
EnglishMark
<>-1
o
Message =
Message +
“ English”
Biology
BiologyMark
<>-1
o
Message =
Message +
“ Biology”
24
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
25


IF-THEN-ELSE within another IF-THEN_ELSE
structure
General structure format :
Block X
Condition1
else
Process Q
o
Block_R
Condition2
Process R1
o
else
o
Process R2
o
26
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
27

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
28
BEGIN GRADE selection
IF mark>=80 THEN
grade = “Distinction”;
ELSE
BEGIN M_TEST selection
IF mark>=65 THEN
Grade
grade = “Merit”;
ELSE
else
mark>=80
BEGIN P_TEST selection
o
o
grade =
M_Test
IF mark>=50 THEN
‘Distinction’
grade = “Pass”;
mark>=65
ELSE
else
o
o
grade = “Fail”;
grade =
P_Test
‘Merit’
ENDIF
END P_TEST selection
else
mark>=50
ENDIF
o
o
grade =
grade =
END M_TEST selection
‘Pass’
‘Fail’
ENDIF
END GRADE Selection
29



Another way to express nested IF
If one condition is true, the following conditions
will not be tested
General structure format :
Block X
condition1
Process Q
Condition n
o
….
Process R
o
else
Process S
o
30
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
31

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
32
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
33
Commission
……
Check code
productCode
else
= ‘A’
=‘B’
=‘C’
o
o
o
o
comm =
comm =
comm =
comm =
10/100 *sales 15/100 *sales 20/100*sales 5/100 * sales
34
…….
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
35

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.
36

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
37

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.
38

similar documents