0% found this document useful (0 votes)
30 views

Chap 04 - Algorithm Design For Selection Control Structure

The document discusses selection control structures in algorithms. It covers boolean values, relational operators, logical operators and different types of selection structures like one-way, two-way and multi-way selection. Examples are provided to demonstrate how conditions are evaluated and statements are executed based on true or false results.

Uploaded by

Hilman Syahiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Chap 04 - Algorithm Design For Selection Control Structure

The document discusses selection control structures in algorithms. It covers boolean values, relational operators, logical operators and different types of selection structures like one-way, two-way and multi-way selection. Examples are provided to demonstrate how conditions are evaluated and statements are executed based on true or false results.

Uploaded by

Hilman Syahiran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Topic 4 :

Algorithm Design For


Selection Control
Structure
CSC121 – Introduction to Algorithm Design and Development

Nor Hasnul Azirah Abdul Hamid


Table of Contents

01 Analysis of Problems 04 Operator Precedence


Requiring Selection Structure

02 Boolean Values, Relational 05 Algorithm Development for


Operators, and Expressions 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 if using with numbers:


○ 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.
02
Boolean Values, Relational Operators,
and Expressions
2.1 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:
2.1 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.

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

Modulus Division % 9%4 1


2.1.2 Relational Operators
Computer Example Example
Operator
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 or
>= 5 >= 7 False
equal to

Not equal to <> 5 <> 7 True


2.1.2 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 :
2.1.3 Logical Operators

Example Example
Operator Description
operation resultant
! NOT NOT true False

&& AND True AND True True

|| OR True OR False True


2.2 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).
2.2.1 Simple Boolean Expression

● Two numbers (operands) are compared using a single


relational operator.
● Each produce Boolean expression (true or false result)
2.2.2 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.
2.2.2 Compound Boolean Expression

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

● 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”
5.2 One-Way Selection
● Syntax form:
if (condition)
statement1
endIf Flowchart

● 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

Start Show Show


“Even” “Odd”
Read number
If (number % 2 == 0)
Show “Even” End
Else
Expected Output:
Show “Odd”
 If the input value for variable number is 6:
EndIf  Output displayed: Even
End  If the input value for variable number is 7:
 Output displayed: Odd
5.4 Multi-Way Selection (if-else-if)
Flowchart
● Syntax form:
if (condition1)
statement1
Yes
Else if (condition2) condition1
Statement
1
statement2
Else if (condition3) No
statement3 Yes Statement
Else condition2
2
statement4
No
EndIf
Yes Statement
condition3
3
● Explanation:
No
○ If the condition1 is evaluated to TRUE, then
statement1 is executed, and statement2, Statement
statement3, statement4 are skipped. 4
○ Statement2 will be executed if the condition1
is evaluated to FALSE, and only if condition2
is evaluated to TRUE.
5.4 Multi-Way Selection (if-else-if): Example
● Example: Flowchart
○ To determine whether any positive number
entered is:
■ divisible by 2, or
■ divisible by 3, or
■ divisible by 5.

● 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

○ Any statement with a TRUE condition will be


No
executed, statement with a FALSE condition
will be skipped.
5.5 Multi-Way Selection (Several if): Example
● Example: Flowchart
○ To determine whether any positive number
entered is:
■ divisible by 2, or
■ divisible by 3, or
■ divisible by 5.

● 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:

If (number % 5 == 0) 1. Input value  10 2. Input value  6 3. Input value  9


Print “Divisible by 5”, newline
10 6 9
EndIf Divisible by 2 Divisible by 2 Divisible by 3
End Divisible by 5 Divisible by 3
5.6 Nested Selection (Nested if)

● Situations where action is depends on two or more different conditions.

● 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

Expected Output for the following input


value:
 Service’s code  T
 Membership  M

Enter Service’s code (B/T): T


Enter membership (M-Member): M

Charges : RM42.75
Thanks!
Do you have any
questions?

You might also like