Topic 4: Algorithm Design For Selection Control Structure
Topic 4: Algorithm Design For Selection Control Structure
Topic 4: Algorithm Design For Selection Control Structure
If it is raining…
– Example: resultant
5 + 7 = 12
5 and 7 are
operands
Operators
• Mathematical Operators
Example
Action Operator
Operation Resultant
Addition + 3+5 8
Subtraction - 7-4 3
Multiplication * 8*5 40
Division / 9/4 2
Modulus division % 9%4 1
Operators
• Relational Operators
Operator Computer Example Example
Symbol operation resultant
Equal to == 5 == 7 False
Less than < 5<7 True
Greater than > 5>7 False
Less than or <= 5<=7 True
equal to
Greater than >= 5>=7 False
or equal to
Not equal to <> 5<>7 True
Operators
• Relational Operators
– 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 :
operator
Case 1:
– age > 5 AND age ≤ 10
– age > 5 OR age ≤ 10
Case 2:
– age < 5 AND age ≥ 10
– age < 5 OR age ≥ 10
Boolean Expression and
Boolean Value
• ! (NOT) Operator
• Expression:
• Example:
Boolean Expression and
Boolean Value
• && (AND) Operator
• Expression:
Boolean Expression and
Boolean Value
• Example:
Boolean Expression and
Boolean Value
• || (OR) Operator
• Expression:
Boolean Expression and
Boolean Value
• Example:
Problem Example
Situation:
• An Identity Card or a Student Card is required for a student to sit for
the final exam.
• When the student has an identity card, student can sit for the final
exam.
• When the student has a Student Card, student can sit for the final
exam.
Analysis:
Expression is written as Identity Card OR Student Card
When both operand is True, resultant is True
When both operand is False, resultant is False
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.
Problem Example
Situation:
• An Identity Card and a Student Card are required for a student to sit for the
final exam.
Analysis:
Expression is written as Identity Card AND Student Card
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.
Precedence of Operator
• Expressions with higher-precedence operators
are evaluated first
Precedence of Operator: Exercise
Example:
• Solve the following expression
a) 2/3*4^2
b) 10 + 6 / 4 – 14 % 3 * (7 – 3)
c) (10 >= 10) && (20 < 20)
d) 65 == 65 && 65 >= 70 || 90 != 105
Precedence of Operator: Exercise
• Solution
a) 3-2*4^2 -29
b) 10 + 6 / 4 – 14 % 3 * (7 – 3) 3
c) (10 >= 10) && (20 < 20) false
d) 65 == 65 && 65 >= 70 || 90 != 105 true
SELECTION
Selection
• 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
o If-else-if
o Several if
Nested selection
Selection
• Selection structure, with the use of relational operators, can
do:
– Numeric comparison
• Example: num1 > 10
– Character comparison
• Example: if (grade == ‘A’)
– String comparison
• Example: Month != “February”
ONE WAY SELECTION
One-Way Selection
• Syntax form:
if (condition)
statement1
endIf
• Explanation:
– The condition is evaluated first Flowchart
Yes
condition
Statement1
No
One-Way Selection: Example
• Pseudocode: Start
Start
Read number Read number
if (number % 2 == 0)
Show “Even” number % 2 Yes
== 0
endIf
End Show “Even”
No
End
Expected Output:
If the input value for variable number is 6:
Output displayed: Even
Ones-Way Selection: Exercise 1
Problem
• Pseudocode
START
1. Declare grade
4. Check grade
IF grade < 0 OR grade > 100
Print " The grade is not valid"
CGPA Status
More than 1.80 Passed
TWO WAY SELECTION
Two-Way Selection
• Syntax form:
if (condition) Flowchart
statement1
else
statement2
endIf
No Yes
condition
• Explanation:
– If the condition is evaluated to TRUE, then Statement2 Statement1
statement1 is executed, and statement2 is
skipped.
– If the condition is evaluated to FALSE, then
statement2 is executed, and statement1 is
skipped.
Two-Way Selection: Example
Start
Read number
Read number
If (number % 2 == 0) No Yes
number % 2
Show “Even” == 0
Else
Show “Even” Show “Odd”
Show “Odd”
EndIf
End End
Expected Output:
If the input value for variable number is 6:
Output displayed: Even
If the input value for variable number is 7:
Output displayed: Odd
Two-Way Selection: Exercise 1
Problem
4. Check grade
IF grade < 0 OR grade > 100
Print “The grade is not valid”
ELSE
Print “The grade is” , grade
CGPA Status
1.80 – 4.00 Passed
Otherwise Failed
MULTI WAY SELECTION
Multi-Way Selection (if-else-if)
• Syntax form:
if (condition1)
Flowchart
statement1
Else if (condition2)
statement2
Yes
Else if (condition3)
condition1 Statement1
statement3
Else
No
statement4
EndIf Yes
condition2 Statement2
• Explanation:
– If the condition1 is evaluated to TRUE, No
then statement1 is executed, and
Yes
statement2, statement3,
condition3 Statement3
statement4 are skipped.
– Statement2 will be executed if the
No
condition1 is evaluated to FALSE, and
only if condition2 is evaluated to TRUE.
Statement4
Multi-Way Selection (if-else-if): Example
Print “NOT
divisible by 2
Expected Output, for the following input values: or 3 or 5”
• Syntax form:
if (condition1) Flowchart
statement1
endIf
Yes
if (condition2) condition1 Statement1
statement2
endIf No
if (condition3) Yes
condition2 Statement2
statement3
endIf
No
if (condition4)
statement4 Yes
condition3 Statement3
endIf
No
• Explanation:
– Any statement with a TRUE condition will Yes
condition4 Statement4
be executed, statement with a FALSE
condition will be skipped.
No
Multi-Way Selection (Several if): Example
Example: To determine whether any positive number entered is:
divisible by 2, or Flowchart
divisible by 3, or
divisible by 5. Start
Start
Read number Pseudocode Read number
If (number % 2 == 0)
Print “Divisible by 2”, newline
Yes
EndIf number % Print “Divisible
2 == 0 by 2”, newline
If (number % 3 == 0)
Print “Divisible by 3”, newline No
EndIf Yes
number % Print “Divisible
If (number % 5 == 0) 3 == 0 by 3”, newline
Print “Divisible by 5”, newline
EndIf No
No
CGPA Status
>= 3.50 Dean’s List
1.80 – 3.49 Passed
< 1.80 Failed
NESTED SELECTION
Nested Selection (Nested if)
• Syntax form:
Flowchart
if (condition1)
if (condition2)
statement1
Else condition1
No
condition3
No
statement3
Else
statement4
endIf
endIf
Nested Selection (Nested if): Example
Example: To calculate charges based on the services code and type of membership as shown in table.
Start
Get code
Get category
code == No code == No
'B' ‘T'
Yes
Yes
price = 120.00 price = 45.00
Yes No Yes No
code == code ==
‘M' ‘M'
disc = 0.2 * disc = 0.1 * disc = 0.05 *
disc = 0.0
price price price
End
Nested Selection (Nested if): Exercise
Problem