Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structure Senior Lecturer

Similar presentations


Presentation on theme: "Control Structure Senior Lecturer"— Presentation transcript:

1 Control Structure Senior Lecturer
Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): (b):

2 Previously…

3 Examine the following 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: New-price ← price – 20% x price OR New-price ← price – 30% x price 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

4 Examine the following problem
Solution You just received SPM result. You got a good result. You have many options to further your study. 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.

5 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

6 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 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 condition statement(s) EndIF this style of writing a program is called indentation the purpose is to make the program easy to read and to ease debugging process.

7 If Statement: One Way Selection
condition Representation Flowchart of One Way Selection

8 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

9 Condition Examples of conditions :
There are few special words can be used in a condition like even, odd, negative, positive or divisible. Examples: num1 < num2 age is greater or equals to 40 grade equals to ‘A’ grade == ‘A’ grade is ‘A’ status is “excellent” if ( number is odd ) if ( year is divisible by 4 ) If ( number is positive ) 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.

10 Tracing Algorithm Using One Way Selection
Example 1 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 % 2 == 0 ) add no2 to sum Display “ The total is “ , sum End Test the above pseudocode with the following data : i ii iii

11 Example 1 Expected Output: The total is 20 The total is 0
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 % 2 == 0 ) add no2 to sum Display “ The total is “,sum End Example 1 Input Data: i ii iii no1 no2 no1 is divisible by 2 sum=no1+sum no2 % 2 == 0 sum=no2+sum sum 20 17 20/2 √ sum=20=0 17 % 2 ≠ 0 41 63 41/2 × 63 % 2 ≠ 0 18 24 18/2 √ sum=18+0 24 % 2 ==0 sum=18+24 42 Expected Output: The total is 20 The total is 0 The total is 42

12 Tracing Algorithm Using One Way Selection
Example2 Start Display “Enter 3 numbers : “ Initialize sum1, sum2, sum3 with zero Read no1, no2, no3 If (no1 is divisible by 2 ) set sum1 with no1 EndIF If ( no2 % 2 == 0 ) set sum2 with no2 If ( no3 is even ) set sum3 with no3 sum = sum1 + sum2 + sum3 Display “ The total is “ , sum End Test the above pseudocode with the following data : i ii iii

13 Example 2 Expected Output: The total is 60 Input Data:
i ii iii no1 no2 no3 no1 is divisible by 2 set sum1 with num1 no2%2==0 set sum2 with num2 no3 is even set num3 with no3 sum1 sum2 sum3 sum = sum1+sum2+sum3 20 40 17 20/2 √ sum1=20 40%2==0 √ sum2=40 17 = even x = = 60

14 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 ) Statement1(s) Else Statement2(s) EndIF

15 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

16 Multiple Ways Selection
Simple if statement If (condition) Statement(s) ElseIf (condition) . Else EndIF If (condition) Statement(s) ElseIf (condition) . EndIF

17 Multiple Ways Selection
Nested if statement

18 Multiple Ways Selection
Nested if statement nested if statement is the scenario where there is if statement inside another if statement

19 Multiple Ways Selection
Nested if statement nested if statement is the scenario where there is if statement inside another if statement

20

21 Multiple Ways Selection

22 Multiple Ways Selection

23

24 EXERCISES


Download ppt "Control Structure Senior Lecturer"

Similar presentations


Ads by Google