Unit 3

1,057 views

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,057
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
29
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Unit 3

  1. 1. F1001 PROGRAMMING FUNDAMENTALS UNIT 3CONTROL STRUCTURE Sequential Control Structure Selection Control Structure Decision Control Structure 30
  2. 2. F1001 PROGRAMMING FUNDAMENTALSBLOCKS OF CODE• Every programming language consists of thousand of statements.• Thus, to simplify the execution of the programming language, all the statements in the programming language is divided into blocks of code that have specific functions.• Statement in a block of code is arranged sequentially in a program.• A block of code is a group of statements that performs one particular task.• The block is separated in several ways, like curly bracket “{“ and “}”, Begin and End statement and others.• Function is a block of code that has been assigned a name.• Function is used as reference and execution for a block of code.• Combination of blocks of code will produce a perfect program to be executed in a computer.• Since the program comes from the different blocks of code, so, the flow needs to be controlled.• Example of blocks of code: void welcome ( ) { BLOCK OF BLOCK printf (“****************************”); printf (“********WELCOME **********”); CODE SEPARATO printf (“****************************”); R }PROGRAM FLOW• Program flow in computer is controlled by the control structure.• Control structure is a logical structure that controls the flow of instruction to be executed by computer.• Control structure uses single entry and single exit; meaning it has only one beginning and one ending.• There are three types of control structures: 1. Sequential control structure 2. Selection / decision control structure a) If……endif b) If……else c) Nested if 3. Looping control structure a) For b) While c) Do……while 31
  3. 3. F1001 PROGRAMMING FUNDAMENTALSSequential Control Structure In this control, every step will be executed one by one from top to bottom. Every box in control structure is a process. Every process is done sequentially. Format:  Pseudocode: START Start Flowchart Statement A Statement A Statement B Statement B End END Example 1:  Compute the total overtime wages of an employee.  Problem analysis: Input: o Hours o Basic_salary o OT_rate Process: o Overtime equals OT_rate times Hours o Salary equals Basic_salary plus Overtime Output: o Salary  Algorithm: 1. Enter Hours, Basic_salary, OT_rate 2. Calculate overtime using formula: Overtime = OT_rate * Hours 3. Calculate salary using formula: Salary = Basic_salary + Overtime 4. Output Salary 32
  4. 4. F1001 PROGRAMMING FUNDAMENTALS  Pseudocode: START Input Hours, Basic_salary, OT_rate Overtime = OT_rate * Hours Salary = Basic_salary + Overtime Output Salary END  Flowchart: START Input Hours, Basic_salary, OT_rate Overtime = OT_rate * Hours Salary = Basic_salary + Overtime Output Salary END Example 2:  Problem: Mathematical operation: Get two numbers, then do adding, subtracting, multiplying and dividing operations.  Problem analysis: Input: o number_1, number_2 Process: o Add 2 numbers: Sum = number_1 + number_2 o Minus 2 numbers: Subtract = number_1 – number_2 o Multiply 2 numbers: 33
  5. 5. F1001 PROGRAMMING FUNDAMENTALS Multiple = number_1 * number_2 o Division 2 numbers: Divide = number_1 / number_2 Output: o Sum, Subtract, Multiple and Divide Algorithm: 1. Enter 2 numbers 2. Add 2 numbers Sum = number_1 + number_2 3. Minus 2 numbers Subtract = number_1 – number_2 4. Multiply 2 numbers Multiple = number_1 * number_2 5. Division of 2 numbers Divide = number_1 / number_2 6. Display Sum, Subtract, Multiple and Divide Flowchart: START Input number_1, number_2 Sum = number_1 + number_2 Subtract = number_1 – number_2 Multiple = number_1 * number_2 Divide = number_1 / number_2 Output Sum, Subtract, Multiple, Divide END 34
  6. 6. F1001 PROGRAMMING FUNDAMENTALS  Pseudocode: START Input number_1, number_2 Sum = number_1 + number_2 Subtract = number_1 - number_2 Multiple = = number_1 * number_2 Divide = number_1 / number_2 Output Sum, Subtract, Multiple, Divide ENDSelection / Decision Control Structure This type of control structure is usually used in structured programming This control structure will execute an instruction based on result of a condition or comparison. A condition will result either TRUE or FALSE. If the condition result is true, the control program will execute the instruction within the TRUE loop operation. Otherwise, it will execute the next instruction or the instruction within the FALSE loop operation. Example 1:  Condition: A person can obtain a license when he/ she is above 21 years old Decision: If true then she / he is qualified to have driving license. If not he / she is not qualified to have a driving license.  Below is a flowchart of control structure: 35
  7. 7. F1001 PROGRAMMING FUNDAMENTALS START True Condition Input age False Statement that will be executed if condition True Statement that will be is true If age > 21 executed if condition is not true False Output “Qualified” Output “Not Qualified” END Type of selection / decision control structure: 1. If……endif 2. If……else 3. Nested if 1. IF…….ENDIF  Rules: If (condition) Instruction (do this instruction if condition is true) Endif If condition is not true, no instruction will be executed 36
  8. 8. F1001 PROGRAMMING FUNDAMENTALS Pseudocode: If (condition) START True statement Endif True Conditi Statement Flowchart:on False END Example 1:  Workers who work on shift 3 will receive additional Bonus RM50, where basic salary is entered by workers.  Problem analysis: Input: 1. Shift 2. Basic_salary Process: Bonus equals RM 50 If Shift equals to 3: Salary equals Bonus plus Basic_salary Output: Salary  Algorithm: 1. Enter Basic_salary, Shift 2. Bonus equals to RM 50 3. Check workers Shift 3.1 If Shift equals to 3 Salary= Basic_salary + Bonus 4. Display Salary 37
  9. 9. F1001 PROGRAMMING FUNDAMENTALS Flowchart: START Input Shift, Basic_salary Bonus = 50 True If Shift = Salary = Basic_salary + Bonus 3 False Output Salary END Pseudocode: START Input Shift, Basic_salary Bonus = 50 If (Shift ==3) Salary = Basic_salary + Bonus End if Output Salary END 38
  10. 10. F1001 PROGRAMMING FUNDAMENTALS2. IF…….ELSE type  A selection of control structure is used to select between two options  Pseudocode:  Rules: If (condition) If (condition) True statement True statement Else Else False statement False statement Endif Endif  Flowchart: START False True Conditi on Statement 2 Statement 1 END  Example 1: o Prepare a problem analysis, algorithm, flowchart and pseudocode to identify whether a student is qualified to further her / his studies in any local university using his / her SPM grade equal to 1. o Problem analysis: Input: Grade Process: If Grade is equal to 1 Output “Qualified to further study”. If not Output “Not qualified to further study”. Output: Display message qualified or not 39
  11. 11. F1001 PROGRAMMING FUNDAMENTALSo Algorithm: 1. Enter Grade 2. Check Grade 1.1 If Grade = 1 1.1.1 Output “Qualified to further study” 1.2 If not 1.2.1 Output “Not qualified to further study”o Flowchart: START Input Grade False True If Grade Output “Not qualified Output “Qualified to further study” to further study” ENDo Pseudocode: START Input Grade If (Grade==1) Output “Qualified to further study” Else Output “Not qualified to further study” Endif END 40
  12. 12. F1001 PROGRAMMING FUNDAMENTALS Example 2: o Problem: Prepare the problem analysis, algorithm, flowchart and pseudocode to find subtraction between two numbers that users enter. o Problem analysis: Input: num1, num2 Process: If num1 greater than num2 Result = num1 – num2 If not Result = num2 – num1 Output: Result o Algorithm: 1. Enter num1, num2 2. Compare the 2 numbers 2.1 If num1 greater than num2 2.1.1 Result = num1 – num2 2.2 If not 2.2.1 Result = num2 – num1 3. Display Result o Flowchart: START Input num1, num2 False True If num1 > num2Result = num2 – num1 Result = num1 – num2 Output Result END 41
  13. 13. F1001 PROGRAMMING FUNDAMENTALS o Pseudocode: START Input num1, num2 If num1 > num2 Result = num1 – num2 Else Result = num2 – num1 Endif Output Result END3. NESTED IF  There are 3 types: 1. Type 1: If (condition1) If (condition2) If (condition3) True statement Endif Endif Endif 2. Type 2: If (condition1) If (condition2) If (condition3) Statement that will be executed if condition1, condition2 and condition3 are true Else Statement that will be executed if condition1, and condition2 are true but condition2 is false Endif 42
  14. 14. F1001 PROGRAMMING FUNDAMENTALS Else Statement that will be executed if condition1 is true but condition2 and condition3 is false Endif Else Statement that will be executed if condition1 is false Endif 3. Type 3 If (condition1) Statement that will be executed if condition 1 is true Else If (condition 2) Statement that will be executed if condition2 is true but condition1 is false Else If (condition3) Statement that will be executed if condition3 is true but condition1 and condition2 are false Else Statement that will be executed if condition1, condition2 and condition3 are false Endif Endif End if Example Type 1: o Problem: To determine whether a candidate is qualified or not to get a scholarship based on his / her study years, guardian’s salary and student CGPA. If the study year is more than 1, student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500, student will be considered for a scholarship. o Problem analysis: Input: CGPA, Year, Salary. Process: 43
  15. 15. F1001 PROGRAMMING FUNDAMENTALS 1. Check if the student application’s can be considered or not for a scholarship. 1.1 If Year greater than 1 1.1.1 If CGPA greater than or equal to 3.00 1.1.1.1 If Salary is less than or equal to RM500 Output “Your application is under consideration”. Output: Student status o Algorithm: 1. Enter CGPA, Salary and Year 2. Check if the student application’s can be considered for a scholarship 2.11 If year > 1 2.1.1 If CGPA >= 3.00 2.1.1.1 If salary <= RM500 Output “Your application is under consideration” 3.1 Display status o Flowchart: START Input Year, CGPA, SalaryFalse True If Year > 1 False If CGPA True >= 3.00 Output “Your False If Salary True application is under <= 500 consideration” END 44
  16. 16. F1001 PROGRAMMING FUNDAMENTALS o Pseudocode: START Input Year, CGPA, Salary If Year >1 If CGPA >= 3.00 If Salary <= RM500 Output “Your application is under consideration” Endif Endif Endif END Example Type 2: o Problem: To determine whether a candidate is qualified or not to get a scholarship based on his / her study years, guardian’s salary and student CGPA. If the study year is more than 1, student’s CGPA is not less than 3.00 and guardian’s salary is below than RM500, student will be considered for a scholarship. If the student is not qualified the message “Not success” will be displayed. o Problem analysis: Input: CGPA, Year, Salary Process: 1. Check if a student can be considered for a scholarship 1.1 If Year > 1 1.1.1 If CGPA >= 3.00 1.1.1.1 If Gaji <= 500 Output “You application is under consideration” 1.1.1.2 If not Output “Not success” 1.1.2 If not Output “Not success” 1.2 If not Output “Not success” Output: Student status 45
  17. 17. F1001 PROGRAMMING FUNDAMENTALS o Algorithm: 1. Enter CGPA, Year, Salary 2. Check if a student can be considered for a scholarship 2.1 If Year > 1 2.1.1 If CGPA >= 3.00 2.1.1.1 If Salary <= RM500 Output “Your application is under consideration”. 2.1.1.2 If not Output “Not success” 2.1.2 If not Output “Not success” 2.2 If not Output “Not success” 3. Display status o Flowchart: START Input Year, CGPA, Salary False True If Year > 1Output “Not False If CGPA True success” >= 3.00 Output “Your Output “Not False If Salary True application is under success” <= 500 consideration” Output “Not success” END 46
  18. 18. F1001 PROGRAMMING FUNDAMENTALS o Pseudocode: Input CGPA, Salary, Year If (year > 1) If (CGPA >= 3.00) If (salary <= RM500) Output “Your application is under consideration” Else Output ”Not success” Endif Else Output ”Not success” Endif Else Output ”Not success” Endif Example Type 3: o Problem: Education status is determined based on the GPA achievement under the following scheme: GPA Status 3.50-4.00 Dean List 2.00-3.49 Pass 1.80-1.99 Conditional Pass 0.00-1.79 Fail o Problem analysis: Input: GPA Process: 1. If (GPA < 0.00 AND GPA > 4.00) Output “Invalid data” 2. If not 2.1 If (GPA >=3.5 AND GPA <= 4.00) Output “Dean List” 2.2 If not 2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass” 2.2.2 If not 2.2.2.1 If (GPA >= 1.80 AND GPA< 2.00) Output “Conditional Pass” 2.2.2.2 If not Output “Fail” Output: Student education status 47
  19. 19. F1001 PROGRAMMING FUNDAMENTALSo Algorithm: 1. Enter student GPA 2. Compare student’s GPA to determine his/ her education status. 2.1 If (GPA < 0.00 AND GPA > 4.00) Output “Invalid data” 2.2 If not 2.2.1 If (GPA >=3.50 AND GPA <= 4.00) Output “Dean List” 2.2.2 If not 2.2.2.1 If (GPA >= 2.00 AND GPA < 3.50) Output “Pass” 2.2.2.2 If not 2.2.2.2.1If (GPA >= 1.80 AND GPA < 2.00) Output “Conditional Pass” 2.2.2.2.2If not Output “Fail” 3. Print statuso Flowchart: 48
  20. 20. F1001 PROGRAMMING FUNDAMENTALS START Input GPA True False If GPA < 0.00 && GPA > 4.00 True False If GPA >= 3.50 &&Output “Invalid GPA <= 4.00 data” Output “Dean True False If GPA >= 2.00 && List” GPA < 3.50 True False If GPA >= 1.80 && Output “Pass” GPA < 2.00 Output Output “Fail” “Conditional Pass” END o Pseudocode: START Input GPA If ((GPA < 0.00) AND (GPA > 4.00)) Output "Invalid Data" Else If ((GPA >= 3.50) AND (GPA <= 4.00)) Output "Dean List" Else If ((GPA >=2.00) AND (GPA < 3.50)) Output "Pass" Else 49
  21. 21. F1001 PROGRAMMING FUNDAMENTALS If ((GPA >= 1.80) AND (GPA < 2.00)) Output "Conditional Pass” Else Output "Fail" Endif Endif Endif Endif END  Usage of Logical Operator: Symbol Symbol Example Result Description(Pseudocode) (C Language) AND && (1 > 3) && (10 < 20) FALSE Both sides of the condition must be true OR || (1 > 3) || (10 < 20) TRUE Either one of the condition must be true NOT ! ! (10 < 20) FALSE Change the operation either from true to false or vise versa o From the above table, the logical operator are AND, OR and NOT. o Example using of AND operator: If ((a < b) && (c > d)) printf(“Print a and c”); o If condition (a < b) is true AND condition (c > d) is true then “Print a and c” will be displayed. If one of the condition is not true then “Print a and c” will not be displayed. o Example usage of OR operator: If ((sales > 5000) || (hoursworked> 81)) bonus = 500; else bonus = 0; 50
  22. 22. F1001 PROGRAMMING FUNDAMENTALS o If the sales are more than 5000 OR working hours are more than 81, bonus RM500 will be given. If either condition is not fulfilled, still the amount of RM500 will still be given as bonus. o If both conditions are false, then bonus will not be given.Looping Control Structure A programming control structure that allows a series of instructions to be executed more than once. The similar statement is repeated several times until the conditions are fulfilled. Three are three types of loop: o For o While o Do…while For While Do…while For (initialize; condition; counter) While (condition) Do True statement if condition is True statement True statement fulfilled Endwhile While (condition) Endfor o Initialize value: a value to start a loop. o Counter: to increase or decrease the initialize value. o Rules for the condition: - If the condition is true / fulfilled, the process will be performed. - Then, the program loops back and recheck the condition, if the condition is true / fulfilled, repeat the process. - When the condition is false, then exit the loop.  Format for Do …… while loop:  Format for the For and While loops: START START Initialize Initialize Statement A Test False END True condition Test condition True False Example of loops usage: A Statement END 51
  23. 23. F1001 PROGRAMMING FUNDAMENTALSo Problem: Prepare the problem analysis, algorithm, flowchart and pseudocode for the average of 5 numbers. Data will be entered by user.o Problem analysis: Input: 5 numbers Process: The process of adding numbers will repeat until the condition to exit the loop is met. Output: Average of 5 numberso Algorithm: 1. Initialize Counter=0; Average = 0; Total = 0 2. Input number 3. Add Total using formula: Total = Total + number 4. Add Counter using formula: Counter = Counter + 1 5. Compare whether Counter is greater than 5 If yes , go to step 6 If not, go to step 2 6. Calculate Average of numbers using formula; Average = Total/5 7. Display Averageo Pseudocode: For loop While loop Do…while loop START START START no = 0 no = 0 no = 0 Total = 0 Total = 0 Total = 0 Avg = 0 Avg = 0 Avg = 0 For (no=0; no<=5; no++) While (no <= 5) Do { Input Num Input Num Input Num Total = Total + Num Total = Total + Num Total = Total + Num Endfor no = no + 1 no = no + 1 Avg = Total/5 Endwhile } While (no <= 5) Output Avg Avg = Total/5 Avg = Total/5 END Output Avg Output Avg END END o Flowchart for For and While loops: 52
  24. 24. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Total = 0 Avg = 0 For / False While no Avg = Total/5 True Input Num Output Avg Total = Total + Num END no = no + 1o Flowchart for Do……while loop: 53
  25. 25. F1001 PROGRAMMING FUNDAMENTALS START Note: no: a variable to control loop no = 0 structure whether to continue or exit from the loop Total= 0 no + 1: a counter and it is very Avg = 0 important. Without a counter, the loop will be infinite Input Num Total = Total + Num: a variable to compute the value Total = Total + Num no = no + 1True False no <= 5 Avg = Total/5 Output Avg ENDo Example: To compute salary of 20 employees Algorithm for For and While loops: 1. Initialize Counter = 0, Salary = 0; 2. Compare whether Counter is greater than 20 or not If yes , out from the loop If not , go to step 3 3. Enter Basic_salary, Claim, O_time 4. Calculate EPF: EPF = Basic_salary * 0.09 5. Calculate Salary using this formula: Salary = Basic_salary + Claim + O_time – EPF 6. Display Salary 7. Add Counter using the formula: Flowchart for For and While loops: Counter = Counter + 1 8. Back to step 2 54
  26. 26. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Salary = 0 For / False END While no True Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09Salary = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 Pseudocode for For loop: START no = 0 Salary = 0 For (no = 0, no <=20, no ++) Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary Endfor END Pseudocode for While loop: 55
  27. 27. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Salary = 0 While no <=20 Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Gaji = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 Endwhile ENDAlgorithm for Do……while loop:1. Initialize Counter = 0, Salary = 0;2. Input Basic_salary, Claim, O_time3. Calculate EPF EPF = Basic_salary * 0.094. Calculate Salary using this formula: Salary = Basic_salary + Claim + O_time – EPF5. Add Counter using this formula: Counter = Counter + 16. Compare whether the Counter is greater than 20 or not If yes , out of loop If not, go to step 27. Display SalaryFlowchart for Do…..while loop: 56
  28. 28. F1001 PROGRAMMING FUNDAMENTALS START no = 0 Salary = 0 Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 True False no <= 5 END Pseudocode for Do…..while loop: START no = 0 Salary = 0 Do { Input Basic_salary, Claim, O_time EPF = Basic_salary * 0.09 Salary = Basic_salary + Claim + O_time – EPF Output Salary no = no + 1 } While (no <= 20)o Dummy Data or Sentinel Value END 57
  29. 29. F1001 PROGRAMMING FUNDAMENTALS- Value used to end the program. Dummy data must be different from the data to be entered.- Example: START Input name While name != “XXX” Dummy data Output name Input mark Output mark Input name Endwhile END 58

×