Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Control Structure

Similar presentations


Presentation on theme: "Chapter 5: Control Structure"— Presentation transcript:

1 Chapter 5: Control Structure
Part 1: Selection Control Structure

2 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

3 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:

4 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.

5 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: One way selection Two way selection Multiple selections

6 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

7 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

8 One Way Selection: Example (cont.)
Example of flowchart using one way selection: 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.

9 Tracing an Algorithm Using One Way Selection
Given the following pseudocode: Test the above pseudocode with the following data: 20 17 41 63 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 Display “The total is ”, sum End

10 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 : Expected screen: no1 no2 Sum 20 17

11 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.

12 Problem Solving using One Way Selection (cont.)
Expected screen: Expected Screen 1 when the user enters even number and expected Screen 2 when the user enter NOT an even number. 19 Screen Screen 2

13 Problem Solving using One Way Selection (cont.)
Pseudocode 1: Flowchart 1 Pseudocode 2: Flowchart 2 Start Read number If (number is even) Display number, “ is an even number” EndIf End Start Read number If (number is divisible by 2) Display number, “ is an even number” EndIf End

14 Problem Solving using One Way Selection (cont.)
Pseudocode 3: Flowchart 3 Start Read number If (number % 2 == 0) Display number, “ is an even number” EndIf End Exercise 5A

15 Problem Solving using One Way Selection (cont.)
Flowchart for Pseudocode 1

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

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

18 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: Expected screen: Detailed pseudocode: Detailed flowchart Information Data Process

19 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

20 Two Ways Selection (cont.)
The following flowchart shows the use of two ways selection. 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.

21 Tracing an Algorithm using Two Ways Selection
Given the following pseudocode: Trace the above pseudocode with the following data: 23, 12, 505 Draw a flowchart Start Display “Enter a number: ” Read no1 If (no1 % 2 == 0) Display Else Display “******” Display newline Display “&&&&&&” EndIF Display “The number entered = ”, no1 End

22 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 : The expected screen is shown below: no1 23

23 Tracing an Algorithm using Two Ways Selection (cont.)
FALSE TRUE Flowchart 1

24 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.

25 Problem Solving Using Two Ways Selection (cont.)
Expected screen: A basic algorithm Start Read 2 numbers Find the biggest Display the biggest End

26 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

27 Problem Solving Using Two Ways Selection (cont.)
The flowchart:

28 Exercise 5B Task: Calculate the difference between two numbers.
Problem definition: Expected screen: Detailed pseudocode: Detailed flowchart Information Data Process

29 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.

30 Multiple Ways Selection (cont.)
The following formats refer to nested if statement: Format 1: If (condition1) :: If (condition2) EndIF Else Statement(s) Inner If statement is in between If … Else

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

32 Multiple Ways Selection (cont.)
Format 3: If (condition1) If (condition2) :: EndIF Else If (condition3) Inner If statement is in between If … Else and also in between Else … EndIF

33 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 Pseudocode Flowchart

34 Multiple Ways Selection (cont.)
Example 2: If (a > b) If (a == c) Display “bbb” EndIF Pseudocode Flowchart

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

36 Tracing for Multiple Ways Selection
Example: Given the following pseudocode, trace the pseudocode using the following test data: i) ii) 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 Display “2.1b” End

37 Tracing for Multiple Ways Selection (cont.)
Answer: i) Data are The location in the computer memory as follows: An expected screen: a b 100 54

38 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

39 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” type  “negative” EndIF Display “The number is ”, type End

40 Problem Solving using Multiple Ways Selection (cont.)
Flowchart:

41 Exercise 5C Analyze the following problems and write pseudocode followed by flowchart. Display the following menu: Ask the user to enter a choice and perform the operation that corresponds with the choice. Choice Operation + Calculate addition of 3 numbers - Substract 1st number with the 2nd number * Multiply 3 numbers / Divide 1st number with the 2nd number

42 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.

43 Logical Operator AND and OR (cont.)
The syntax to use logical operators is as follows: Using AND logical operator: Using OR logical operator: If (condition1 AND condition2 AND …) //block1 Else //block2 EndIF If (condition1 OR condition2 OR …) //block1 Else //block2 EndIF

44 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 block2 True block1

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

46 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

47 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

48 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

49 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 biggest = no3 EndIF Display “The biggest number = ”, biggest End

50 Any Question


Download ppt "Chapter 5: Control Structure"

Similar presentations


Ads by Google