Control Structure Siti Nurbaya Ismail Senior Lecturer Faculty
Control Structure Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): sitinurbaya@kedah. uitm. edu. my (b): https: //sitinur 151. wordpress. com Selection 1
Previously… Sequential Control Structure
Examine the following problem Problem Solution 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. The formulas to calculate the new price is basically the same. But the discount rate is different for each size of shoe. Here, the shoes size determines the discount rate, the new price will have to be calculated differently for each shoes size. The formulas can be written as: If size S, New‐price ← price – 20% x price OR If size M, New‐price ← price – 30% x price OR If size L, 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 shoes sizes whether it is L, M or S
Examine the following problem Problem You just received SPM result. You got a good result. You have many options to further your study. Solution 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.
Selection Control Structure • used when we have a choice to do activities based on certain conditions • shows decision‐making capabilities of the computer • is implemented in an algorithm using the If statement or case statement * If statement * shows how to implement it in a pseudocode and a flowchart • variations of selection control structure are: * One way selection * Two ways selection * Multiple selections 5
If Statement: One Way Selection • used when there is only one choice • If the condition is true then the task is performed • If the condition is false, then no processing will take place IF condition statement(s) End. IF • this style of writing a program is called indentation • the purpose is to make the program easy to read and to ease debugging process. • a situation where a computer has to do comparison at least between two values and makes a decision • if the condition is true then certain action will be taken (statement 1 until statement n will be executed) • if the condition is false, no action will be done
If Statement: One Way Selection condition Representation Flowchart of One Way Selection
Condition • A condition is a comparison at least between two values or variables. • To make a condition (comparison between two values), the following relational operator can be used. Relational operator > < == =< => != Meaning Greater than Less than Equals to, is Less or equals Greater or equals Not equal
Condition • Examples of conditions : i. iii. iv. v. vi. num 1 < num 2 age is greater or equals to 40 grade equals to ‘A’ grade == ‘A’ grade is ‘A’ status is “excellent” Note: When we use single character data as in above (iii) (iv) and (v), use single quotes (‘ ‘) and use double quotes( “ “ ) for a string (vi). Condition used in above (iii), (iv) and (iv) are the same. • There are few special words can be used in a condition like even, odd, negative, positive or divisible. • Examples: vii. if ( number is odd ) viii. if ( year is divisible by 4 ) ix. If ( number is positive )
If Statement: One Way Selection • Text book • Page 96
Tracing Algorithm Using One Way Selection Example 1 Start Display “Enter 2 numbers : “ Read no 1, no 2 Initialize sum with 0 If ( no 1 is divisible by 2 ) add no 1 to sum End. IF If ( no 2 % 2 == 0 ) add no 2 to sum End. IF Display “ The total is “ , End Test the above pseudocode with the following data : sum i. 20 17 ii. 41 63 iii. 18 24
Example 1 Input Data: i. 20 17 no 1 no 2 20 17 ii. 41 63 iii. 18 no 1 is divisible by 2 sum=no 1+sum 24 no 2 % 2 == 0 sum=no 2+sum 0 20/2 √ sum=20+0 Start Display “Enter 2 numbers : “ Read no 1, no 2 Initialize sum with 0 If ( no 1 is divisible by 2 ) add no 1 to sum End. IF If ( no 2 % 2 == 0 ) add no 2 to sum End. IF Display “ The total is “, sum End 20 17 % 2 ≠ 0 41 63 0 41/2 × 0 63 % 2 ≠ 0 18 Expected Output: 0 24 18/2 √ sum=18+0 18 24 % 2 ==0 sum=18+24 42 i. The total is 20 ii. The total is 0 iii. The total is 42
Tracing Algorithm Using One Way Selection Example 2 Start Display “Enter 3 numbers : “ Initialize sum 1, sum 2, sum 3 with zero Read no 1, no 2, no 3 If (no 1 is divisible by 2 set sum 1 with no 1 End. IF If ( no 2 % 2 == 0 ) set sum 2 with no 2 End. IF If ( no 3 is even ) set sum 3 with no 3 End. IF Test the above pseudocode with the following data : ) sum = sum 1 + sum 2 + sum 3 Display “ The total is End “ , sum i. 20 40 17 ii. 41 63 12 iii. 19 63 35
Expected Output: i. The total is 60 Example 2 Input Data: i. 20 40 17 no 1 no 2 no 3 20 40 17 ii. 41 63 12 no 1 is divisible by 2 set sum 1 with num 1 iii. 19 63 35 no 2%2==0 no 3 is even set sum 2 with num 2 set num 3 with no 3 20/2 √ sum 1=20 sum 1 sum 2 sum 3 sum = sum 1+sum 2+sum 3 0 0 20 40%2==0 √ sum 2=40 20 40 17 = even x =20+40+0 = 60
Problem Solving Using One Way Selection • Text book • Page 107: Example 5(c) if (number % 2 != 1) • Page 110: Example 5(d)
If Statement: Two Ways Selection • Have two choices to choose from • However, only one choice can be executed at one time • either to execute the first choice and ignore the second choice or process only the second choice and ignore the first one If ( condition ) Statement 1(s) Else Statement 2(s) End. IF
Tracing Algorithm Using Two Ways Selection • Page 115: Example 5 (e)
Problem Solving Using Two Ways Selection • Text book • Page 121: Example 5 (f): Display biggest number between 2 number Start Display” Enter two number” Read no 1, no 2 if (no 1 > no 2) max = no 1 else max = no 2 end. IF Display “Max number is “ max End
EXERCISES Two Ways Selection Problem Solving Using Page 213: Exercise 5 C Question 1: d & e & f & g pseudocode
If Statement: Multiple Ways Selection • In solving a problem, sometimes there are situation that has more than two choices. • This problem can be solve using multiple ways selection. • There are two types of multiple ways selection • Simple if statement • Nested if statement • nested if statement is the scenario where there is if statement inside another if statement
If Statement: Multiple Ways Selection • Simple if statement If (condition) Statement(s) Else. If (condition) Statement(s). . . Else Statement(s) End. IF
If Statement: Multiple Ways Selection • Nested if statement
If Statement: Multiple Ways Selection • Nested if statement
If Statement: Multiple Ways Selection • Nested if statement
If Statement: Multiple Ways Selection Example 1 Flowchart representation
If Statement: Multiple Ways Selection Example 2 Flowchart representation
If Statement: Multiple Ways Selection Example 3 Flowchart representation
Tracing Algorithm Using Multiple Ways Selection • Text book • Page 149: Example 5 (m) • Page 152: Example 5 (n) • Page 156: Example 5 (o)
EXERCISES Page 205 Question 1 & 2 Tracing Algorithm Using Multiple Ways Selection
Problem Solving Using Multiple Ways Selection • Text book • • Page 159: Example 5 (p) Page 172: Example 5 (q) Page 181: Example 5 (r) Page 185: Example 5 (t)
- Slides: 30