07 CSC415 Chapter 5 Selection
07 CSC415 Chapter 5 Selection
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
*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
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
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.
*
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