
Control Structures in Computer Programming
Explore the concept of control structures in programming, including decision-making capabilities, algorithm implementation, and variations like one-way selection, two-way selection, and multiple selections. Learn about if statements, indentation, and the use of control structures for making choices in programming. Discover how to calculate discounts based on shoe sizes and explore study options based on personal interests and qualifications.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Control Structure Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): sitinurbaya@kedah.uitm.edu.my (b): https://sitinur151.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 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.
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. num1 < num2 ii. age is greater or equals to 40 iii. grade equals to A iv. grade == A v. grade is A vi. status is excellent There are few special words can be used in a condition like even, odd, negative, positive or divisible. Examples: vii. viii. ix. 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.
If Statement: One Way Selection Text book Page 96
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 EndIF Display The total is , sum End Test the above pseudocode with the following data : i. 20 17 ii. 41 63 iii. 18 24
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 EndIF Display The total is ,sum End Example 1 Input Data: i. 20 17 ii. 41 63 iii. 18 24 no1 no2 no1 is divisible by 2 sum=no1+sum no2 % 2 == 0 sum=no2+sum sum 20 17 0 20/2 sum=20+0 20 17 % 2 0 Expected Output: 41 63 0 41/2 0 i. ii. iii. The total is 20 The total is 0 The total is 42 63 % 2 0 0 18 24 18/2 sum=18+0 18 24 % 2 ==0 sum=18+24 42
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 EndIF If ( no3 is even ) set sum3 with no3 EndIF Test the above pseudocode with the following data : i. 20 40 17 ii. 41 63 12 iii. 19 63 35 sum = sum1 + sum2 + sum3 Display The total is , sum End
Expected Output: i. The total is 60 Example 2 Input Data: i. 20 40 17 ii. 41 63 12 iii. 19 63 35 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 0 0 0 0 20/2 sum1=20 20 20 40%2==0 sum2=40 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 ) Statement1(s) Else Statement2(s) EndIF
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 End Display Enter two number Read no1, no2 if (no1 > no2) max = no1 else max = no2 endIF Display Max number is max
EXERCISES EXERCISES Problem Solving Using Two Ways Selection Page 213: Exercise 5C 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) ElseIf (condition) Statement(s) ElseIf (condition) Statement(s) . . . Else Statement(s) EndIF
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 EXERCISES Tracing Algorithm Using Multiple Ways Selection Page 205 Question 1 & 2
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)