# Chapter 5: Control Structure - WordPress.com

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 Calculate p

Calculate q FALSE Display p = , p Display q = , q When the value of variable a is greater than the value of variable b then the condition is true. The computer will executes three statements. Otherwise the computer does nothing. Tracing an Algorithm Using One Way

Selection Given the following pseudocode: Start 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 20 17 Sum 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: Informati Display number and the message that mention the on 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.)

Start Pseudocode 1: Read number If (number is even) Display number, is an even number EndIf End Start Pseudocode 2: Read number If (number is divisible by 2) Display number, is an even Flowchart 2 number EndIf End

Flowchart 1 Problem Solving using One Way Selection (cont.) Start Pseudocode 3: Read number If (number % 2 == 0) Display number, is an even 3 number EndIf End Flowchart 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 divisible by 2 TRUE 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:

Informatio n 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 If (condition) first choice. The format for two ways

Statement1(s) solution Elseis: Statement2(s) EndIf Two Ways Selection (cont.) The following flowchart shows the use of two ways selection. FALSE a == b TRUE Calculate r Calculate x

Calculate s Calculate y s, r Calculate z x, y, z When the value of variable a is equal to value of variable b, then the condition is true. The computer only executes four statements. Otherwise, the computer only executes three statements. 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:

Informatio n 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: Informatio n 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) :: EndIF Else Statement(s) EndIF Inner If statement is in between If Else Multiple Ways Selection (cont.) Format 2:If (condition1)

Statement(s) Else If (condition2) :: EndIF EndIF Inner If statement is in between Else EndIF Multiple Ways Selection (cont.) Format If (condition1) 3: If (condition2) :: EndIF Else

If (condition3) :: EndIF EndIF Inner If statement is in between If Else and also in between Else EndIF Multiple Ways Selection (cont.) Examples of multiple ways selection are given below. Example 1: If (a > b) Display aaa Else If (a > 100) Display bbb EndIF

EndIF NO NO a > 100 a>b YES Pseudocode Flowchart YES aaa Multiple Ways Selection (cont.)

Example 2: If (a > b) If (a == c) Display bbb EndIF EndIF NO a>b NO YES a == c YES

bbb Pseudocode Flowchart Multiple Ways Selection (cont.) Example 3: If (a > b) If (a == 10) Display aaa EndIF Else If (b < c) Display bbb Else Display ccc EndIF EndIF

Pseudocode NO NO ccc bb YES YES NO a == 10

bbb YES aaa 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 information Type of a number (positive, negative or zero)

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 NO type negative number > 0

number == 0 YES type positive Display The number is type End YES type zero 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: Condition 1 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:

Condition 1 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 information Consonant or vowel Data to be entered A letter Logical Operator AND and OR (cont.)

Pseudocode: Start Read letter If (letter == a OR letter == e OR letter == i OR letter == o OR letter == u OR Display vowel Else Display consonant EndIF End letter == A OR letter == E OR

letter == I OR letter == O OR letter == U OR ) Logical Operator AND and OR (cont.) Example 2: You are given three numbers. Determine and display the biggest number. Problem definition: Required information The biggest 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

## Recently Viewed Presentations

• It's important to remember that not everyone has studied this issue as well as you and there might be confusion, misunderstanding, etc. You mush be patient and calmly answer their questions, explaing the nuances of the issue and understanding that...
• Title: Slide 1 Author: Glenn Last modified by: GICF Created Date: 2/14/2012 3:11:20 AM Document presentation format: On-screen Show (4:3) Other titles
• VicHealth funded. Case Study: Corangamite. 1 in 4 . young people feel they don't belong. "We Know . Y. our . N. ame, B. ut . N. ot . Y. our . S. tory" 2500 people engaged. Generations connected across...
• Art of Multiprocessor Programming© Herlihy-Shavit 2007. Concurrency. enq(x) Challenge: what if the queue is empty or full? y=deq() tail. head. Concurrent enqueue and dequeue calls should in principle be able to proceed without interference, but it gets tricky when the...
• ATTITUDE FORMATION AND CHANGE: LOW CONSUMER EFFORT ... Classical Conditioning Group Exercise In groups of 3-5, describe one specific example of how classical conditioning could be used by marketers. Create scenario with UR, US, CS and CR.
• Sterols Role in Nature Sterols are found in plants as both free sterols, as sterol esters (of fatty acids) and as sterol glycosides Serve as components of membranes Work with phospolipids to create membranes which are only permeable to certain...
• Parts of an angle. An angle is formed by two rays that have a common endpoint (vertex).One ray is the initial side and the other is the terminal side.We typically will draw angles in the coordinate plane with the initial...
• It's an Intervention! ... This great, fuzzy area of human ingenuity is what Jean Paul Sartre calls "bad faith." When spelled out the techniques use to maintain bad faith look morally atrocious and appallingly silly. That is because they are...