Topic 4: Algorithm Design For Selection Control Structure

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 55

Topic 4

ALGORITHM DESIGN FOR


SELECTION CONTROL
STRUCTURE
Course Outline
• Analysis of Problems Requiring Selection
Structure
• Boolean Values, Relational Operators, and
Expressions
• Logical Operators
• Operator Precedence
• Algorithm Development for Selection Control
Structure (Pseudo-code and Flowchart)
Selection
Analogy
Making Decision:
You need to choose to
make hot milo OR cold
milo???
If it is raining…

Selection I will use umbrella so that I don’t get wet.

If it is raining…

Analogy • I will take my umbrella (If the condition is true)


• I will not take my umbrella (If the condition is not true)
Analysis of Problems Requiring Selection
Structure
• To determine whether any positive number given
is an even number
• To determine whether any positive number given
is either an even number or an odd number.
• To determine whether any positive number
entered is:
– divisible by 2, or
– divisible by 3, or
– divisible by 5.
Operators
• Operators are the data connectors within expression
and equations.
• They tell the computer:
• Ways to process data.
• Types of processing needs to be done.
• THREE types of operators used in calculation and
problem solving include:
1. Mathematical
2. Relational
3. Logical
Operators
• Operand & Resultant
• Data that connects and processes by the operator is
called the operands.
• Meanwhile, when the operation is completed the
answer that results is known as Resultant.
• Data type of operand and the resultant depend on the
operator. operator

– 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

Balance < 500 (TRUE) resultant

Balance and 500 are


operands
Operators
• Logical Operators

Operator Description Example Example


operation resultant
! NOT NOT true False

&& AND True AND True True

|| OR True OR False True


Boolean Expression
• Every decision a computer program makes
involves evaluating a Boolean expression.

– Boolean Expression: An expression whose value


can be only true (1) or false (0).
Simple Boolean Expression
• Two numbers (operands) are compared using
a single relational operator.

• Each produce Boolean expression (true or false


result)
Compound Boolean Expression
• The Boolean operator && (meaning AND)
refers to the mathematical term conjunction.

• The Boolean operator || (meaning OR) refers


to the mathematical term disjunction.

• The Boolean operator ! (meaning NOT) is


sometimes referred to as negation.
Compound Boolean Expression
Conjunction vs Disjunction

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

– If the condition is evaluated to TRUE, then


statement1 is executed

Yes
condition

Statement1
No
One-Way Selection: Example

• Example: Determine whether any


positive number given is an even number. Flowchart

• 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

To determine an invalid grade if the grade is out


of range (0-100)
One-Way Selection: Exercise 1

• Pseudocode

START
1. Declare grade

2. Prompt "Please enter a grade: "


3. Get grade

4. Check grade
IF grade < 0 OR grade > 100
Print " The grade is not valid"

5. Print “Thank You!”


END
One-Way Selection: Exercise 1

Draw the flowchart for the previous


pseudocode.
One-Way Selection: Exercise 2
 Problem

Write the complete pseudocode and flowchart that


receive CGPA from user, and display academic
status based on CGPA below.

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

• Example: Determine whether any


positive number given is either an even Flowchart
number or an odd number.
• Pseudocode: Start

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

To determine an invalid grade if the grade is out


of range (0-100) and output the grade if the
grade is within the range.
Two-Way Selection: Exercise 1
• Pseudocode
START
1. Declare grade

2. Prompt “Please enter a grade: “


3. Get grade

4. Check grade
IF grade < 0 OR grade > 100
Print “The grade is not valid”
ELSE
Print “The grade is” , grade

5. Print “Thank You!”


END
Two-Way Selection: Exercise 1

Draw the flowchart for the previous


pseudocode.
Two-Way Selection: Exercise 2
 Problem

Write the complete pseudocode and flowchart that


receive CGPA from user, and display academic
status based on CGPA below.

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

Example: To determine whether any positive number entered is:


 divisible by 2, or Start
 divisible by 3, or Flowchart
 divisible by 5.
Read number
Start
Read number Pseudocode
If (number % 2 == 0) number % Yes Print “Divisible
Print “Divisible by 2”, newline 2 == 0 by 2”, newline
Else If (number % 3 == 0) No
Print “Divisible by 3”, newline
Yes Print “Divisible
Else If (number % 5 == 0) number %
3 == 0 by 3”, newline
Print “Divisible by 5”, newline
Else No
Print “NOT divisible by 2 or 3 or 5” number % Yes Print “Divisible
EndIf 5 == 0 by 5”, newline
End No

Print “NOT
divisible by 2
Expected Output, for the following input values: or 3 or 5”

1. Input value  10 2. Input value  6 3. Input value  9


End
10 6 9
Divisible by 2 Divisible by 2 Divisible by 3
Multi-Way Selection (Several if)

• 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

End number % Yes Print “Divisible


5 == 0 by 5”, newlin

No

Expected Output, for the following input values: End

1. Input value  10 2. Input value  6 3. Input value  9


10 6 9
Divisible by 2 Divisible by 2 Divisible by 3
Divisible by 5 Divisible by 3
Multi-Way Selection : Exercise
 Problem

Write the complete pseudocode and flowchart that receive CGPA


from user, and display academic status based on CGPA below.

CGPA Status
>= 3.50 Dean’s List
1.80 – 3.49 Passed
< 1.80 Failed
NESTED SELECTION
Nested Selection (Nested if)

 Situations where action is depends on two or


more different conditions.

• Syntax form:
Flowchart
if (condition1)
if (condition2)
statement1
Else condition1
No
condition3
No

statement2 Yes Yes


endIf Yes No Yes No
condition2 condition4
Else if (condition3)
if (condition4) statement1 statement2 statement3 statement4

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.

SPA Services Price (RM) Discount


Code Start
Type of Membership Display “Enter Service’s code (B/T): “
Member Non-Member Read code
(M) Display “Enter membership (M-Member): ”
Read category
Traditional Body 120.00 20% 10%
Massage if (code == 'B') PSEUDOCODE
(B) price = 120.00
if (category == ‘M')
Foot Massage 45.00 5% - disc = 0.2 * price
(T) else
disc = 0.1 * price
endif
Else if (code == 'T')
Expected Output for the following input value: price = 45.00
 Service’s code  T if (category == ‘M')
disc = 0.05 * price
 Membership  M else
disc = 0.0
endif
Enter Service’s code (B/T): T Endif
Enter membership (M-Member): M OUTPUT charge = price – disc
Display “Charges: RM”, charge
Charges : RM42.75 End 51
Nested Selection (Nested if): Example

Start

FLOWCHART Display “Enter Service’s


code (B/T): ”

Get code

Display “Enter Membership


(M-Member): ”

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

charge = price - disc

Display “Charges: RM”, charge

End
Nested Selection (Nested if): Exercise
 Problem

Find the total museum admission fee based on nationality and


age as in the following table.

Total Fee = Fee x Quantity


Nationality Age Fee
< 60 10.00
1) Malaysian
≥ 60 5.00
< 60 25.00
2) Non-Malaysian
≥ 60 15.00
Hint : Prepare the algorithm (flowchart)
Summary
This topic explains
• The selection mechanism to solve a problem.
• There are three (3) operators in selection problem solving (Mathematical,
Relational and Logical).
• Every selection involves evaluating a Boolean expression (true / false).
• The selection structure tests a condition, then executes one sequence of
statements depending on whether the condition is true or false.
• The are variations of selection structure are:
– One-way selection
– Two-way selection
– Multiway selection
– Nested selection
References
– Farrell, Joyce, Programming Logic and Design Comprehensive,
2nd edition, Course Technology 2002.

– Liang, Y.D., Introduction to Programming with C++, 2nd edition,


Pearson Higher Education, 2010.

– N Mohamad, M Puteh. Problem Solving with C++, UPENA, 2006.

– Sprankle, M., & Hubbard, J. (2009). Problem Solving &


Programming Concepts (8th ed.). Upper Saddle River, NJ: Pearson
Education.

You might also like