The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by inaazlin, 2018-12-11 21:16:01

2.1(c) Design a Solution - Selection

Design a Solution - Selection

Keywords: Selection

2.1 Design a Solution

c) Explain each control structure and their
respective purposes.
d) Apply appropriate control structure.

Control Structure
• Sequence
• Selection
• Looping

Edited by: azlina mohamad | computer science unit

Control Structure : Selection

Use of selection structure in daily life:

Choosing between a hot or cold drink.
Making a right or left turn at a junction.
Selecting which scarf to wear to class.
Deciding whether or not to iron a shirt.
Picking side dishes at Kenny Roger Roaster’s

Restaurant.

2

Selection Control Structure

• The selection control structure performs action(s) based on
a certain condition.

• In pseudocode, selection will be represented using if
statement

• In flow chart, the selection will be represented using
selection symbol.

condition false
true

Types of Selection Structure
1 Single selection (if-selection)

Follow the set of instructions only when the condition is true

2 Dual selection (if-else selection)
Follow different set of instructions for true or false conditions

3 Multiple selection (nested if-else / nested if )
Follow the set of instructions based on the condition chosen
4

Single Selection (if)

•if structure

• Checks a condition to perform action(s).
• If condition is true, perform action(s)
• If condition is false, skips it (i.e. perform nothing)

Control Structure : Selection condition
action
Example 1:
if (it is raining) condition
action
bring an umbrella
condition
Example 2: action
if (you have a quiz tomorrow)

study tonight
otherwise

watch a movie

Single selection (If – Selection)

1 • Follow the set of instructions only when the condition
is true

contains only one set of instructions,
 which are processed when the condition is true

Single selection
(if – selection)

Example 1 Example 2

if (it is raining) if (you have a quiz tomorrow)
bring an umbrella
study tonight

Single selection format
(if selection)

• • Flowchart

if (condition) condition T
instruction
F Instruction
end if

Example 1: single selection. IPO analysis
Input: mark
Write a pseudocode for the following Process: to determine whether
problem statement: mark is greater than and equal
to 70.
Print “Qualified” when user enters Output: “ Qualified”
marks greater than and equal to 70.

Answer Pseudocode Option answer pseudocode
Start Start

Read mark Read mark
if (mark>=70) if mark greater than and equal to 70

Display “Qualified” Display “Qualified”
end if end if
stop stop

Example 1: single selection. start
Read marks
Draw a flowchart for the
following problem if T
statement: (marks
>=70)
Print “Qualified” when
user enters marks greater
than and equal to 70.

F Print

“Qualified”

stop

Example 2: single selection

•Jamil’s Trading Store wants a program that allows a cashier to enter
an item’s price and the quantity purchased by a customer. The shop
gives the customer a 10% discount when the quantity purchased is
over 5. The program should calculate and display the total amount
when the quantity purchased is over 5.

Example of Problem 1

IPO analysis

Single selection
(if selection)

Input : Price, quantity
Process:
Output : To calculate the total amount
when the quantity purchased is
over 5

total amount

Algorithm
Single selection (if selection)

Start start
read price, quantity
if (quantity > 5) Read price, quantity
total_amount= (price*quantity)*0.9
print total_amount T
end if
If (quantity >
Stop 5)

F total_amount=

(price*quantity)*0.9

Print
total_amount

stop

2 Dual selection – (If-else selection)
• Follow different set of instructions for true
or false conditions

• contains two set of instructions.

Example: Instruction Set 1
if (you have a quiz tomorrow) Processed when
condition is true
study tonight
else Instruction Set 2
Processed when
watch a movie condition is false

2 DIfu-aEl slesleecSteiolne–ct(iIof-enlse selection)

••FFoollloowwddiiffffeerreenntt set of instructions ffoorr true
otrrufaelsoer fcaolsnedictoionndsitions

if (you are a female)
use the toilet on your left

else
use the toilet on your right

end if

Dual selection (if-else selection )format

• Pseudo code • Flowchart

if (condition) condition T
instruction 1
F Instruction 1
else
instruction 2 Instruction 2

end if

Example 3 : Dual selection – if else selection

Print “Qualified” when user enters mark greater than and
equal to 70, else print “Disqualified”.

IPO analysis
Input: mark
Process: to determine whether the mark is greater than and
equal to 70 or not.
Output: “ Qualified” or “Disqualified”.

Example 3 : Dual selection – if else selection

Print “Qualified” when user enters mark greater than and
equal to 70, else print “Disqualified”.

start

Answer Pseudocode Read marks
Start
F if T
Read mark (marks
if (mark>=70)
>=70)
Display “Qualified”
else Print Print
“Disqualified” “Qualified”
Display “Disqualified”.
end if
stop

stop

Example 4 : Dual selection – if else selection

Print message "Excellent!” when user enter mark
greater than and equal to 80, otherwise print
“Sorry, try again”.

Dual selection (if-else selection )

Output 1 Input

Print “Excellent!” when user enter mark

greater than and equal to 80, else print

Condition

“Sorry, try again”.

Output 2

Example of Problem 2

IPO analysis

Dual selection
(if –else selection)

Input mark
Process
Output To determine whether the mark is
greater or equal to 80 or not

“Excellent!” or “ Sorry, try again”

Example of Problem 2 start T

Pseudocode Read marks Print
“Excellent!
Start If(marks >=
read marks 80) ”
if (marks >= 80)
print “Excellent!” F

else Print
“Sorry, try
print “Sorry, try again”
again”
end if
stop
Stop

Example of Problem 3

A high school poetry competition is open only for
students above 15 years old. Display “Eligible” if
the students meet the requirement and display
“Not eligible” if otherwise.

Example of Problem 3

IPO analysis

Dual selection
(if –else selection)

Input age
Process
Output To determine whether the age is
above 15 years old or not

or
To determine whether the
students are eligible or not to
enter Poetry Competition

“Eligible!” or “ Not eligible ”

Example of Problem 3 Start

Start Read age T
read age
if (age > 15) age > 15 Print
“Eligible”
print “Eligible” F
else
Print “Not
print “Not eligible” eligible”
end if
Stop

Stop

Example of Problem 4

If x is greater than y, display “x is bigger
than y” else display “x is smaller than y”.

Example 4: Dual selection – if else selection
IPO analysis

Input x,y
Process
Output To determine whether x is greater
than y or not
“x is bigger than y” or “x is smaller
than y”

Example 4: Dual selection – if else selection

Pseudocode Flowchart

Start

Start Read
read x,y x,y
if (x>y)
T
print “x is bigger than y”
else x>y

print “x is smaller than y” F Print “x is
end if bigger than
Stop Print “x is
smaller than y” y”

Stop

Example 5. Dual selection – if else selection

•Mariam wants a program that calculates and
displays her annual bonus, given her annual sales
amount. Mariam receives a 2% bonus when her
annual sales are at least RM15,000; otherwise, she
receives a 1.5% bonus.

An Introduction to Programming with C++
(Diane Zak 7th edition)
Page 120

IPO analysis

Input: annual_sales
Process: to determine whether annual_sales greater than
rm15000 or not.
Output: bonus

Pseudocode

Start
Read annual_sales

if annual_sales >=15000
bonus=0.02 x annual_sales
else
bonus=0.015 x annual_sales
end if
Print bonus
stop

Multiple selection – (nested if-else/ nested if)

3 Follow the set of instructions based on the
condition chosen

• a.k.a extended selection structure
• Capable of choosing from several alternatives.
• Can be created by using 2 forms/statement:

• nested if-else
• nested if

Multiple selection – (nested if-else/ nested if)

3 Follow the set of instructions based on the
condition chosen

if (you are going to Banggol Judah)
take the right turn

else
if (you are going to Kg. Joh)
take the left turn
else
go straight

end if

Multiple selection (nested if-else selection )
Example of Problem 6

To determine and display a message based on the

given grade.

Grade Message
A “Excellent”
B
C “Good”
F “Average”

Other grades “Fail”
“Error”

33

Example of Problem 6

IPO analysis

Multiple selection
(nested if –else selection)

Input grade

Process To determine and display a
Output message based on given grade.
“Excellent” or “Good” or
“Average” or “Fail” or “Error”

Pseudocode
Multiple selection
(nested if –else selection)

Start
Enter grade

if (grade == ‘A’)
display “Excellent”

else
if (grade == ‘B’)
display “Good”
else
if (grade == ‘C’)
display “Average”
else
if (grade == ‘F’)
display “Fail”
else
display “error”
end if
end if
end if

end if
Stop

OPTION ANSWER Pseudocode
Multiple selection
(nested if –else selection)

Start
Enter grade

if grade is A
display “Excellent”

else
if grade is B
display “Good”
else
if grade is C
display “Average”
else
if grade is F
display “Fail”
else
display “error”
end if
end if
end if

end if
Stop

Flowchart
Multiple selection
(nested if –else selection)

start

Read grade T
Print
grade == ‘A’
F “Excellent”

grade ==‘B’ T
F
Print “Good”
grade ==‘C’
F T Print
“Average”
grade ==‘F’
F T

Display “Error ” Print “Fail”

stop

Multiple selection
(nested if –else selection)

if (condition 1) Rule Number 1:
instruction 1;
Only ONE instruction is
else executed.
if (condition 2)
instruction 2;
else
end if

end if

Multiple selection
(nested if –else selection)

Example of Problem 7

To determine and display the grade based on the given

marks.

Mark Grade
greater than or equal to 80 A
greater than or equal to 60 B
greater than or equal to 50 C
below then 50
Failed

39

Example of Problem 7

IPO analysis

Multiple selection
(nested if –else selection)

Input mark

Process To determine and display the
grade based on given mark.

Output “A” or “B” or “C” or “Failed”

Pseudocode

Multiple selection
(nested if –else selection)

Start
read marks
if (marks>= 80)
print “A”
else
if (marks>= 60)
print “B”
else
if (marks>= 50)
print “C”
else
print “Failed”
end if
end if
end if

Stop

Flowchart

Multiple selection
(nested if –else selection)

start

Read marks

marks>= 80 T Print
“A”

F T Print
marks>= 60 “B”

F T Print
marks>= 50 “C”

F

Print “Failed”

stop

Example of Problem 8

Multiple selection
(nested if –else selection)

To determine and display whether the number

entered is a positive number, negative number

or zero.

Example of Problem 8

IPO analysis

Multiple selection
(nested if –else selection)

Input number

Process To determine and display the
Output number based on certain
criteria

“number is positive” or “number
is negative” or “number is zero”

Pseudocode

Multiple selection
(nested if –else selection)

start
read number
if (number > 0)
display “number is positive"
else
if (number < 0)
display “number is negative"
else
display “number is zero“
end if
end if

stop

Flowchart
Multiple selection
(nested if –else selection)

start

Read number T
print“number
(number > 0) is positive”
F
T
(number < 0) print“number
F is negative”

Print “number is
zero”

stop

Multiple selection
(nested if –else selection)

if (condition 1) Rule Number 1:
instruction 1; Only ONE instruction is
if (condition 2)
instruction 2; executed.
end if

end if

Multiple selection
(nested if selection)

Example of Problem 9

Solve this problem statement that requires the user to
determine whether the user are entitled for the post
or not. The candidate is entitled for the post only if she
is a female and her age is above 30.

Example of Problem 9

IPO analysis

Multiple selection
(nested if selection)

Input Gender, age

Process To determine whether user is
Output entitle for the post or not.
“You are entitle for the post” or
no output

Multiple Selection (if-else-if)

• EXAMPLE OF PROBLEM 10

The program will be assigned based on the
value of a code. Display the name of the
program that will be assigned based on
the following table:

Code Program .
1 Degree in Multimedia
2 Degree in Networking
3 Degree in Robotics


Click to View FlipBook Version