Chap 04 - Algorithm Design For Selection Control Structure
Chap 04 - Algorithm Design For Selection Control Structure
03 Logical Operators
01
Analysis of Problems Requiring
Selection Structure
1.1 Analysis of Problems Requiring
Selection Structure
● Selection analogy : need to make decision
● Example :
○ You need to choose to make
hot milo or cold milo?
1.1 Analysis of Problems Requiring
Selection Structure
● Selection analogy : If it is raining…
● If it is raining…
○ I will use umbrella so that I don’t get wet.
● If it is raining…
○ I will use my umbrella (If the condition is
true)
○ I will not use my umbrella (If the condition is
not true)
1.1 Analysis of Problems Requiring
Selection Structure
Example:
2.1.1 Arithmetic Operators
Example
Action Operator
Operation Resultant
Addition + 3+5 8
Subtraction - 7-4 3
Multiplication * 8*5 40
Division / 9/4 2
Equal to == 5 == 7 False
Less than or
<= 5 <= 7 True
equal to
Greater than or
>= 5 >= 7 False
equal to
● Example:
○ When a credit card customer’s balance is less than RM500
(True), then the customer can charge another purchase. When
the balance is not less than RM500 (False), then he cannot
charge another purchase.
○ Expression should be :
2.1.3 Logical Operators
Example Example
Operator Description
operation resultant
! NOT NOT true False
AND OR
● Conjunction vs Disjunction:
○ Example:
■ age > 5 AND age ≤ 10
■ age > 5 OR age ≤ 10 All age
Age: 6 -10
03
Logical Operators
3.1 Boolean Expression and Boolean Value
(NOT Operator)
● ! (NOT) Operator
○ Expression:
○ Example:
3.2 Boolean Expression and Boolean Value
(AND Operator)
● && (AND) Operator
○ Expression:
○ Example:
3.2 Boolean Expression and Boolean Value
(AND Operator)
PROBLEM EXAMPLE :
Situation: Analysis:
■ An Identity Card and a Student Card are ■ Expression is written as Identity Card
required for a student to sit for the final AND Student Card
exam. ■ When both operand are True, resultant is
True
■ When both operand are False, resultant is
False
■ When the is operand True AND False,
resultant is False
Conclusion:
Student can sit for the final exam only when he/she has both cards.
3.3 Boolean Expression and Boolean Value
(OR Operator)
● || (OR) Operator
○ Expression:
○ Example:
3.3 Boolean Expression and Boolean Value
(OR Operator)
PROBLEM EXAMPLE :
Situation: Analysis:
■ An Identity Card or a Student Card is ■ Expression is written as Identity Card OR
required for a student to sit for the final Student Card
exam. ■ When both operand is True, resultant is
■ When the student has an identity card, True
student can sit for the final exam. ■ When both operand is False, resultant is
■ When the student has a Student Card, False
student can sit for the final exam. ■ When the operand True OR False,
resultant is True
Conclusion:
Therefore, student can sit for the final exam when he/she has either one card.
04
Operator Precedence
4.1 Precedence of Operator
● Expressions with higher-precedence operators are
evaluated first
05
Algorithm Development for
Selection Control Structure
5.1 Selection Control Structure
● The selection structure tests a condition, then executes one
sequence of statements instead of another, depending on
whether the condition is true or false.
● A condition is any variable or expression that returns a
Boolean value (TRUE or FALSE).
● The variations of selection structure are:
○ One-way selection (Single-alternative selection structure)
○ Two-way selection (Dual-alternative selection structure)
○ Multiway selection
■ If-else-if
■ Several if
○ Nested selection
5.1 Selection Control Structure
● Explanation:
○ The condition is
evaluated first condition
Yes
○ If the condition is
evaluated to TRUE, Statement1
then statement1 is No
executed
5.2 One-Way Selection: Example
Start Flowchart
● Example:
○ Determine whether any Read number
positive number given is
an even number.
number % 2 Yes
== 0
● Pseudocode:
Show “Even”
Start
No
Read number
if (number % 2 == 0)
End
Show “Even”
endIf Expected Output:
End If the input value for variable number is 6:
Output displayed: Even
5.3 Two-Way Selection
● Syntax form:
if (condition)
statement1 Flowchart
else
statement2
endIf
No Yes
condition
● Explanation:
Statement2 Statement1
○ If the condition is evaluated to TRUE,
then statement1 is executed, and
statement2 is skipped.
○ If the condition is evaluated to FALSE,
then statement2 is executed, and
statement1 is skipped.
5.3 Two-Way Selection: Example
● Example:
Flowchart Start
○ Determine whether any
positive number given is
Read number
either an even number or
an odd number.
Yes number % No
● Pseudocode: 2 == 0
● Pseudocode:
Start
Read number
If (number % 2 == 0)
Print “Divisible by 2”, newline
Else If (number % 3 == 0)
Print “Divisible by 3”, newline
Else If (number % 5 == 0)
Print “Divisible by 5”, newline
Expected Output, for the following input values:
Else
Print “NOT divisible by 2 or 3 or 5” 1. Input value 10 2. Input value 6 3. Input value 9
EndIf 10 6 9
End Divisible by 2 Divisible by 2 Divisible by 3
5.5 Multi-Way Selection (Several if)
Flowchart
● Syntax form:
if (condition1)
statement1 Yes Statement
endIf condition1
1
if (condition2)
No
statement2
endIf Yes Statement
condition2
if (condition3) 2
statement3 No
endIf
if (condition4) Yes Statement
condition3
3
statement4
endIf No
Yes Statement
condition4
● Explanation: 4
● Pseudocode:
Start
Read number
If (number % 2 == 0)
Print “Divisible by 2”, newline
EndIf
If (number % 3 == 0)
Print “Divisible by 3”, newline
EndIf Expected Output, for the following input values:
● Syntax form:
if (condition1) Flowchart
if (condition2)
statement1
Else
statement2
endIf
Else if (condition3)
if (condition4)
statement3
Else
statement4
endIf
endIf
5.6 Nested Selection (Nested if): Example
● Pseudocode:
Start
Display “Enter Service’s code (B/T):”
● Example: Read code
○ To calculate charges based on the services Display “Enter membership (M-Member):”
code and type of membership as shown in Read category
table. if (code == 'B')
price = 120.00
if (category == ‘M')
disc = 0.2 * price
Discount
else
SPA Services Type of Membership disc = 0.1 * price
Price (RM)
Code endif
Member Non-
Else if (code == 'T')
(M) Member
price = 45.00
Traditional Body if (category == ‘M')
Massage 120.00 20% 10% disc = 0.05 * price
(B) else
Foot Massage (T) 45.00 5% - disc = 0.0
endif
Endif
charge = price – disc
Display “Charges: RM”, charge
End
5.6 Nested Selection (Nested if): Example
Charges : RM42.75
Thanks!
Do you have any
questions?