0% found this document useful (0 votes)
3 views49 pages

07 CSC415 Chapter 5 Selection

This document covers the fundamentals of selection control structures in algorithms, explaining how decisions are made based on conditions. It details various types of selection structures, including one-way, two-way, and multiple selection, along with examples and pseudocode. Additionally, it discusses the use of logical operators to combine conditions in problem-solving scenarios.

Uploaded by

2022450318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views49 pages

07 CSC415 Chapter 5 Selection

This document covers the fundamentals of selection control structures in algorithms, explaining how decisions are made based on conditions. It details various types of selection structures, including one-way, two-way, and multiple selection, along with examples and pseudocode. Additionally, it discusses the use of logical operators to combine conditions in problem-solving scenarios.

Uploaded by

2022450318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

* Algorithm Fundamentals

Chapter 5: Selection Control Structure


*
*Overview
*This lesson covers the following topics:
•Implement using the Control Structure in problem solving
•Apply types of selection control structure on the given
problems
•Understand how computer executes an algorithm uses
selection control structure
•Understand how to trace an algorithm
•Write an algorithm to solve a problem
*
*Selection Control Structure
*It is used when a problem prompts us with choices. The
selection control structure shows decision‐making capabilities of
the computer.
*Look at the following two problems. It cannot be solved only
using the three simple statements (input, process, output). We
need to solve it using selection control structure.

i. Kedai Kasut I offers discount to celebrate hari raya. 50% discount is


offered for L size shoes. 30% discount for M size and 20% for S size.
ii. You just received SPM result. You got a good result. You have many
options to further your study.
*
Case 1

Kedai Kasut I offers discount to celebrate hari raya. 50% discount is offered for L size shoes.
30% discount for M size and 20% for S size.

For this case, the formula to calculate the new price is basically the same. But the discount rate
is different for each size of shoe. Here, the shoe size determines the discount rate.
In other words, the new price will have to be calculated differently for each shoe size. The
formulas can be written as:
new-price ← price - 20% x price
OR
new-price ← price - 30% x price
OR
new-price ← price - 50% x price
The three different formulas demonstrate that there are three choices to select when
calculating the new price and the three choices are determined by the shoe sizes (L, M or S).
*
*Case 2
You just received SPM result. You got a good result.
You have many options to further your study
*For this problem, you have many choices depending
on your interest and credentials. If you want to be an
engineer, you should choose any engineering courses.
If you want to be an expertise in computer science,
take any computer science courses. If you want to be
a chef, it is advisable to choose culinary discipline,
and so on.
*
What is a condition?
*A condition is a statement that compares two values. These
values are tested or evaluated and it gives two possible answer:
TRUE or FALSE. A condition is constructed using relational
operators.
Relational Operators Meaning

> Greater than


< Less than
= Equals to ( is )
<= Less or equals
>= Greater or equals
!= Not equal
*
What is a condition? (cont.)
*Examples of conditions : The condition gives two answers: TRUE or FALSE
based on the value of num1 and num2. If num1 is
i. num1 < num2 less than num2, the answer is TRUE. If num1 is
not less than num2, the answer is FALSE.
ii. age is greater or equals to 40
iii.grade equals to ‘A’ The rest of the examples follow the same
pattern.
iv.grade = ‘A’
v. status = “excellent”
*
What is a condition? (cont.)
*There are few special words that can be used in a
condition like even, odd, negative, positive or
divisible.

*Examples are:
i. Number is odd
ii. Year is divisible by 4
iii. Number is positive
*
How to Implement Selection Control
Structure?
*In an algorithm, a selection control structure is
implemented using the If statement together with a
condition(s). It can also use Case statement.
*An if statement needs a condition that is tested for
TRUE or FALSE. The computer will take action
according to the result of the test.
*
How to Implement Selection Control Structure?
(cont.)
*Examples of if statements :
i. If (num1 < num2)
ii. If (age is greater or equals to 40)
iii.If (grade equals to ‘A’)
iv.If (grade = ‘A’)
v. If (status = “excellent”)
*
*Variations of selection control structures are:
❑One way selection
❑Two ways selection
❑Multiple selection
*
One Way Selection
*One way selection is used when a problem prompts
with only one choice.
*The format is as follows:
If condition
* statement(s)
endIF

*If the condition is true then the statement(s) is performed.


If the condition is false, then no statement is processed.
*
One Way Selection (cont.)
False True
Condition

Statement 1

….

Statement N
*
One Way Selection (cont.)
Example 1: Given pseudocode and the flowchart.

If (color = 1)
Display “Yellow“
endIf
If color is 1, then the statement, Display
“Yellow“ is performed. If color is not 1,
then no statement is processed at this
point.
It is worth noting that it is the condition
that gives power to the computer to decide
on which action to take.
*
One Way Selection (cont.)
Example 2: Given a pseudocode and the equivalent flowchart.
If ( a > b )
Calculate p
Calculate q
Display “ P = “ , p
Display “ Q = “ , q
endIF
*
One Way Selection -Tracing
Given the following pseudocode:
*
One Way Selection – Tracing (cont.)
*
Discussion: One Way Selection – Tracing
The computer executes the pseodocode statement by statement (top down)
• //1 statement is executed, the message enter 2 numbers : will be
displayed on the computer screen.
• //2 is executed, the computer assigns variables no1, no2 with value 20 and
17.
• //3 is executed, the computer assigns 0 to variable sum.
• //4 is executed, the computer checks value of variable no1. If the value of
variable no1 can be divided by 2 without any remainder, then the
statement //4.1 will be executed. It makes the variable sum change to 20.
• //5 is executed, the computer divides value of variable no2 with 2. The
remainder is not equals to 0. So the statement //5.1 will be ignored.
• //6 statement is executed, the message the total is, followed by the
content of variable sum (which is 20) will be displayed on the computer
screen.
*
Two Ways Selection
*Two ways selection is used when a problem prompts with two choices.
*The format is as follows:

* If condition
statement1(s)
else
statement2(s)
endIF
* If the condition is TRUE then the statement1(s) is performed. Ignore the second
statement. If the condition is FALSE, then statement2(s) is processed. Ignore the
first statement(s).
*
Two Ways Selection (cont.)
*
Two Ways Selection (cont.)
Example 1 – Given the pseudocode and the equivalent flowchart.
If (color = 1)
Display “Yellow“
else
Display “Black“
endIf

If color is 1, then the statement, Display


“Yellow“ is performed. Ignore statement
Display “Black“. If color is not 1, then
Display “Black“ is performed. Ignore
Display “Yellow“.
*
Two Ways Selection (cont.)
Example 2–Flowchart with multiple statements.
*
Two Way Selection - Tracing
*
Two Way Selection - Tracing

Note: Similar execution pattern will be


carried out for data values 12 and 505. But
the block of statement(s) executed depends
on the condition whether it is True of False.
*
Two Way Selection - Tracing
*
Two Ways Selection – Problem Solving
*
Two Ways Selection – Problem Solving
*
Two Ways Selection – Problem Solving
*
Multiple Ways Selection
This control structure is used when a problem
prompts with more than two choices. There are
other ways to write if statement for multiple ways
selection, but nested if format is used here.

nested if means an if statement is inserted in


another if statement
*
Format 1
Example
If (condition1)
::
If ( condition2 )
::
endIF

endIF
*
Format 2
Example
If (condition1)
::
If ( condition2 )
::
endIF

Else
Statement(s)
endIF
*
Format 3
Example
If (condition1)
Statement(s)

Else
If ( condition2 )
::
endIF
endIF
*
Format 4 Example
If (condition1)
If ( condition2 )
::
endIF
Else
If ( condition3)
::
endIF
endIF
*
Format 1
If (a > b)
Display
“aaa”
If ( a > c )
Display “ccc”
endIF
Else
Display “bbb”
endIF
*
Format 2
If (a > b)
Display
“aaa”

Else If ( a > c )
::
endIF
endIF
*
Format 3
If (condition1)
If ( condition2 )
::
endIF
Else
If ( condition3)
::
endIF
endIF
*
Multi Ways Selection - Tracing
*
Multi Ways Selection - Tracing
*
Multi Ways Selection - Tracing
*
Logical Operator AND and OR
❑We can combine more than one comparisons if
it is suitable.
❑Sometimes the solution will be simpler and
shorter when we combine many comparisons.
❑To combine two or more comparisons, we use a
logical operator AND or OR in if statement. See
the following examples.
*
Logical Operator AND and OR (cont.)
*
Logical Operator AND and OR (cont.)
*
Logical Operator AND and OR (cont.)
*
Logical Operator AND and OR (cont.)
*
*Case 1:
*A student is in thedean’s list when his/her CGPA is at least 3.50 for
each semester. Assume that the student has four semesters to finish a
program. Determine whether the student is in the dean’s list or not.

Problem definition:
Information: message DEAN’S LIST or NOT DEAN’S LIST
Data: cgpa for 5 semesters.

A student’s cgpa must be at least 3.5 for each semester, meaning that
the cgpa is at least 3.5 for ALL semesters.
*

The algorithm is rewritten using if statement


with four conditions. It makes the solution
simpler and shorter. The conditions are
evaluated from left to right. The AND has
higher precedence than the OR.
*
Case 2:
A worker is offering a personal loan at RM10,000.00. The condition to
get the loan is to fulfill one of the following criteria :
i. Gross salary is less than RM2000.00
ii. Net salary is at least RM1000.00

Determine whether a worker is qualified or not.

Problem definition:
Information: a message QUALIFIED or not QUALIFIED
Data: salary, net salary

To make a worker qualified to get the loan, he/she must fulfill one of
the condition.
*
*
The End

You might also like