0% found this document useful (0 votes)
87 views50 pages

Part 1: Selection Control Structure

This document discusses selection control structures in programming. It covers one-way and two-way selection structures using if statements. One-way selection executes statements if a condition is true, while two-way selection executes one block of statements if the condition is true and a different block if it is false. The document provides examples of pseudocode and flowcharts to illustrate these concepts and how to trace an algorithm using selection structures.

Uploaded by

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

Part 1: Selection Control Structure

This document discusses selection control structures in programming. It covers one-way and two-way selection structures using if statements. One-way selection executes statements if a condition is true, while two-way selection executes one block of statements if the condition is true and a different block if it is false. The document provides examples of pseudocode and flowcharts to illustrate these concepts and how to trace an algorithm using selection structures.

Uploaded by

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

Chapter 5:

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

Display “The total is ”, sum


End

• Test the above pseudocode with the following data:


i. 20 17
ii. 41 63
Tracing an Algorithm Using One Way
Selection (cont.)
• We can draw the events in computer memory as follows when the data is
20 and 17 :
no1 no2 Sum
20 17
0
20

• 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

number is even TRUE

number, “ is an even number”

FALSE

End
Problem Solving using One Way Selection (cont.)
Flowchart for Pseudocode 2 Start

number

number is
TRUE
divisible by 2

number, “ is an even number”

FALSE

End
Problem Solving using One Way Selection (cont.)
Flowchart for Pseudocode 3 Start

number

number % 2 == 0 TRUE

number, “ is an even number”

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.

FALSE a == b TRUE  When the value of variable a is


equal to value of variable b, then
Calculate r Calculate x
the condition is true. The computer
only executes four statements.
Calculate s Calculate y Otherwise, the computer only
executes three statements.

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

• The expected screen is shown below:


Tracing an Algorithm using Two Ways Selection
(cont.) Start

• Flowchart 1
“Enter a number”
no1

TRUE
FALSE no1 % 2 == 0 FALSE
TRUE

******
@@@@
&&&&&&

“The number entered = ”


no1

End
Problem Solving Using Two Ways Selection

• Task: Display the biggest number between two numbers.


• Problem definition:
Information The biggest
Data Two numbers
Process If the first number is greater than the second number, then the biggest
is the first number. Otherwise, the bigger is the second number.
Problem Solving Using Two Ways Selection
(cont.)
• Expected screen:

• 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

FALSE no1 > no2 TRUE

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:

If (a > b) NO a>b YES


If (a == c)
Display “bbb”
EndIF
EndIF
NO a == c YES

“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

number > 0 YES type  “zero”


NO

type  “negative” type  “positive”

Display “The number is”


type

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

• Using OR logical operator:


If (condition1 OR condition2 OR …)
//block1
Else
//block2
EndIF
Logical Operator AND and OR (cont.)
• The following table are called truth table. It is used to determine the value
when the testing implements the logical operator AND or OR.
• Below is a truth table for logical operator AND:
Condition1 Condition2 Condition1 AND Condition2 Executable block

False False False block2


False True False block2
True False False block2
True True True block1
Logical Operator AND and OR (cont.)
• Below is a truth table for logical operator OR:
Condition1 Condition2 Condition1 OR Condition2 Executable block

False False False block2


False True True block1
True False True block1
True True True block1
Logical Operator AND and OR (cont.)
• Example 1: Ask the user to enter a letter. Determine whether the letter is
a consonant or vowel.
• Problem definition:
Required
Consonant or vowel
information
Data to be entered A letter
Logical Operator AND and OR (cont.)
• Pseudocode:
Start
Read letter
If (letter == ‘a’ OR letter == ‘A’ OR
letter == ‘e’ OR letter == ‘E’ OR
letter == ‘i’ OR letter == ‘I’ OR
letter == ‘o’ OR letter == ‘O’ OR
letter == ‘u’ OR letter == ‘U’ OR )
Display “vowel”
Else
Display “consonant”
EndIF
End
Logical Operator AND and OR (cont.)
• Example 2: You are given three numbers. Determine and display the
biggest number.
• Problem definition:
Required
The biggest
information
Data to be entered 3 numbers
Logical Operator AND and OR (cont.)
• Pseudocode:

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

You might also like