Part 1: Selection Control Structure
Part 1: Selection Control Structure
Control Structure
Part 1: Selection Control Structure
Lesson Outcome
• At the end of this chapter, students should be able to:
• Use control structure in problem solving
• Apply types of selection control structure on the given problems
• Understand how computer executes an algorithm using selection control structure
• Understand how to trace an algorithm
• Write an algorithm using selection control structure to solve a problem
Condition
• A condition or testing is a comparison between at least two values.
• It can also test variables that can have either TRUE or FALSE (YES or
NO) as the answer.
• To make a condition or comparison between two values or variables, the
following relational operator can be used:
Condition (cont.)
Relational operator Meaning
> Greater than
< Less than
== Equals to, is
≤ Less or equals
≥ Greater or equals
≠ Not equal
• There are a few special words that can be used in a condition like even,
odd, negative, positive or divisible.
If Statement
• A selection control structure is used when we have a choice to carry out
actions based on certain conditions.
• This control structure is implemented in an algorithm using the If statement
and case statement.
• Various of selection control structure are:
i. One way selection
ii. Two way selection
iii. Multiple selections
One Way Selection
• Is used when we have only one choice.
• If the condition is true then the task is performed. If the condition is false,
then no processing will take place.
• The format is as follows:
If condition
statement(s)
EndIf
One Way Selection: Example
• Example of pseudocode using one way selection:
If (a greater than b)
calculate p
calculate q
Display “p = ”, p
Display “q = ”, q
EndIF
One Way Selection: Example (cont.)
• Example of flowchart
using one way selection:
a>b TRUE
When the value of
variable a is greater than
Calculate p the value of variable b
then the condition is true.
The computer will
Calculate q
FALSE
executes three statements.
Otherwise the computer
Display “p = ”, p does nothing.
Display “q = ”, q
Tracing an Algorithm Using One Way
• Given the following pseudocode: Start
Selection
Display “Enter 2 numbers: ”
Read no1, no2
Initialize sum with 0
If (no1 is divisible by 2)
add no1 to sum
EndIF
If (no2 is divisible by 2)
add no2 to sum
EndIF
• Expected screen:
Problem Solving using One Way
Selection
• Task: Ask the user to enter a number. Determine whether the number is an
even number. If yes, display the number and a message to mention that the
number is even number.
• Problem definition:
Information Display number and the message that mention the number is even
number when it is an even number. Otherwise do nothing.
Data One number
Process Check whether the number is even or not. If yes, display the number
and the message that mention the number is even number. Otherwise
do nothing.
Problem Solving using One Way Selection
(cont.)
• Expected screen:
19
Screen 1 Screen 2
• Expected Screen 1 when the user enters even number and expected Screen
2 when the user enter NOT an even number.
Problem Solving using One Way Selection
(cont.)
• Pseudocode 1: Start
Read number
If (number is even) Flowchart 1
Display number, “ is an even number”
EndIf
End
Start
• Pseudocode 2: Read number
If (number is divisible by 2)
Flowchart 2 Display number, “ is an even number”
EndIf
End
Problem Solving using One Way Selection
(cont.)
• Pseudocode 3: Start
Read number
If (number % 2 == 0) Flowchart 3
Display number, “ is an even number”
EndIf
End
Exercise 5A
Problem Solving using One Way Selection (cont.)
Flowchart for Pseudocode 1 Start
number
FALSE
End
Problem Solving using One Way Selection (cont.)
Flowchart for Pseudocode 2 Start
number
number is
TRUE
divisible by 2
FALSE
End
Problem Solving using One Way Selection (cont.)
Flowchart for Pseudocode 3 Start
number
number % 2 == 0 TRUE
FALSE
End
Exercise 5A
• Task: Ask the user to enter two numbers. If the first number can be
divided with the second number, display both numbers and the result.
• Problem definition:
Information
Data
Process
• Expected screen:
• Detailed pseudocode:
• Detailed flowchart
Two Ways Selection
• In this type of selection, the computer has two choices to choose from.
• However, it can only execute one choice at one time, either to execute the
first choice and ignore the second choice or process only the second
choice and ignore the first choice.
• The format for two ways solution is: If (condition)
Statement1(s)
Else
Statement2(s)
EndIf
Two Ways Selection (cont.)
• The following flowchart shows the use of two ways selection.
s, r Calculate z
x, y, z
Tracing an Algorithm using Two Ways
Selection
• Given the following pseudocode:
Start
Display “Enter a number: ”
Read no1
If (no1 % 2 == 0)
Display “@@@@”
Else
Display “******”
Display newline
Display “&&&&&&”
EndIF
Display “The number entered = ”, no1
End
• Trace the above pseudocode with the following data: 23, 12, 505
• Draw a flowchart
Tracing an Algorithm using Two Ways Selection (cont.)
• The location of variable no1 in the computer memory can be visualized as
follows when the data is 23 :
no1
23
• Flowchart 1
“Enter a number”
no1
TRUE
FALSE no1 % 2 == 0 FALSE
TRUE
******
@@@@
&&&&&&
End
Problem Solving Using Two Ways Selection
• A basic algorithm
Start
Read 2 numbers
Find the biggest
Display the biggest
End
Problem Solving Using Two Ways Selection
(cont.)
• A detailed algorithm:
Start
Display “Enter 2 numbers: ”
Read no1, no2
If (no1 > no2)
max no1
Else
max no2
EndIF
Display “The biggest = ”, max
End
Problem Solving Using Two Ways Selection
(cont.) Start
• The flowchart:
“Enter two numbers”
no1, no2
max no1
max no2
“The biggest = ”
max
End
Exercise 5B
• Task: Calculate the difference between two numbers.
• Problem definition:
Information
Data
Process
• Expected screen:
• Detailed pseudocode:
• Detailed flowchart
Multiple Ways Selection
• In solving a problem, sometimes we face a situation that has more than
two choices.
• We can solve this problem using nested if statement, where there is if
statement inside another if statement.
Multiple Ways Selection (cont.)
• The following formats refer to nested if statement:
• Format 1:
If (condition1)
::
If (condition2)
:: Inner If statement is in between If …
EndIF Else
Else
Statement(s)
EndIF
Multiple Ways Selection (cont.)
• Format 2:
If (condition1)
Statement(s)
Else Inner If statement is in between Else …
If (condition2)
::
EndIF
EndIF
EndIF
Multiple Ways Selection (cont.)
• Format 3: If (condition1)
If (condition2)
::
EndIF Inner If statement is in between If …
Else and also in between Else …
Else EndIF
If (condition3)
::
EndIF
EndIF
Multiple Ways Selection (cont.)
• Examples of multiple ways selection are given below.
• Example 1:
If (a > b)
Display “aaa”
Else
If (a > 100) NO a>b YES
Display “bbb”
EndIF
EndIF
NO a > 100 YES “aaa”
Pseudocode
Flowchart
Multiple Ways Selection (cont.)
• Example 2:
“bbb”
Pseudocode
Flowchart
Multiple Ways Selection (cont.)
• Example 3:
If (a > b)
If (a == 10) NO a>b YES
Display “aaa”
EndIF
Else
If (b < c)
NO b<c YES NO a == 10 YES
Display “bbb”
Else
Display “ccc”
EndIF “ccc” “bbb” “aaa”
EndIF
Pseudocode Flowchart
Tracing for Multiple Ways Selection
• Example: Given the following pseudocode, trace the pseudocode using the
following test data:
i) 100 54 ii) 5 10 iii) 7 7
Start
Display “Enter values a and b : ”
Read a, b
If (a ≥ b)
Display “2.1a”
If (b > 10)
Display “2.1.1a”
Else
Display “2.1.1b”
EndIF
Else
Display “2.1b”
EndIF
End
Tracing for Multiple Ways Selection
(cont.)
• Answer: i) Data are 100 54
• The location in the computer memory as follows:
a b
100 54
• An expected screen:
Problem Solving using Multiple Ways
Selection
• Example: Ask the user to enter a number. Determine whether the number
is positive, negative or zero.
• Problem definition:
Required
Type of a number (positive, negative or zero)
information
Data to be entered A number
Problem Solving using Multiple Ways Selection
(cont.)
• Pseudocode:
Start
Display “Enter a number : ”
Read number
If (number == 0)
type “zero”
Else
//number is either negative or positive
If (number > 0)
type “positive”
Else
type “negative”
EndIF
EndIF
Display “The number is ”, type
End
Problem Solving using Multiple Ways Selection
(cont.) Start
• Flowchart:
Display “Enter a number”
number
NO number == 0 YES
End
Exercise 5C
• Analyze the following problems and write pseudocode followed by flowchart.
• Display the following menu:
Choice Operation
+ Calculate addition of 3 numbers
- Substract 1st number with the 2nd number
* Multiply 3 numbers
/ Divide 1st number with the 2nd number
• Ask the user to enter a choice and perform the operation that corresponds with the choice.
Logical Operator AND and OR
• We can combine more than one comparisons if it suitable.
• To combine two or more comparisons, we use a logical operator AND or
OR in if statement.
Logical Operator AND and OR (cont.)
• The syntax to use logical operators is as follows:
• Using AND logical operator:
If (condition1 AND condition2 AND …)
//block1
Else
//block2
EndIF
Start
Read no1, no2, no3
If (no1 > no2 AND no1 > no3)
biggest = no1
Else
If(no2 > no3)
biggest = no2
Else
biggest = no3
EndIF
EndIF
Display “The biggest number = ”, biggest
End
Any Question