Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 8 :PROGRAMMING 8.4 Use of control structure Learning Outcome : Write a program segment by using appropriate control structure.

Similar presentations


Presentation on theme: "Topic 8 :PROGRAMMING 8.4 Use of control structure Learning Outcome : Write a program segment by using appropriate control structure."— Presentation transcript:

1 Topic 8 :PROGRAMMING 8.4 Use of control structure Learning Outcome : Write a program segment by using appropriate control structure

2 HOW TO BUILD A CODING USING A SEQUENCE CONTROL STRUCTURE 8.4.1 Sequence

3 Design a program that solves this problem. Generate a set of input test values. Perform a design walkthrough to verify your design. 8.4.1 Sequence

4 Examples of coding the use of sequential control structure cin>> sales; (S1) bonus = sales * (5/100); (S2) cout<<“Bonus: “<<bonus; (S3) cin>> sales; (S1) bonus = sales * (5/100); (S2) cout<<“Bonus: “<<bonus; (S3) Statement1, S1 is executed first, then statements, S2, an so on. Statement1, S1 is executed first, then statements, S2, an so on. 8.4.1 Sequence

5 Coding the use of sequential control structure Instruction in sequence programming are executed sequentially one by one The sequence structure directs the computer to process the instructions, one after another, in the order listed in the program. 8.4.1 Sequence

6 Statement1, S1Statement2, S2StatementN, SN The simplest programs consist just sequence of statements : ▫ no loops, no selections amongst alternative actions, no use of subroutines. Start End 8.4.1 Sequence

7 Example 1 Examples of programming the use of sequential control structure Tile Limited wants a program that allow its sales clerk to enter width and length of area (feet), and also price of square foot of tile. The program will display the total price of tile based on the area inserted by user. 8.4.1 Sequence

8 Examples of programming the use of sequential control structure STEP 1 Identify the control structure based on the problem statement given: Control Structure : Sequence Identify the control structure based on the problem statement given: Control Structure : Sequence 8.4.1 Sequence

9 Examples of programming the use of sequential control structure STEP 2 Identify The Problem & the Formula to be used : Problem : Calculate total of price of tiles based on length and width of area Calculate area Formula : area = width X length Calculate total of price Formula : total _price = area X price _of_tile Identify The Problem & the Formula to be used : Problem : Calculate total of price of tiles based on length and width of area Calculate area Formula : area = width X length Calculate total of price Formula : total _price = area X price _of_tile 8.4.1 Sequence

10 Convert from the Flow Chart to a C + + Program: STEP 3 8.4.1 Sequence

11 start area = width * length total_price = area * price_of_tile end width length total_ price cout <<" Enter width : "; cin>>width; cout <<" Enter length : "; cin>>length; area=width * length; Total_price= area * price_of_tile; cout<<"Total Price is : RM "<<total_price; cout <<" Enter width : "; cin>>width; cout <<" Enter length : "; cin>>length; area=width * length; Total_price= area * price_of_tile; cout<<"Total Price is : RM "<<total_price; 8.4.1 Sequence

12 Example 2 Calculate the Body Mass Index Examples of the use of sequential control structure STEP 1 Identify the control structure based on the problem statement given: Control Structure : Sequence Identify the control structure based on the problem statement given: Control Structure : Sequence 8.4.1 Sequence

13 Examples of programming the use of sequential control structure STEP 2 Identify The Problem & the Formula to be used : Problem : Calculate the Body Mass Index based on weight and height Formula : bmi= weight / (height *height) Identify The Problem & the Formula to be used : Problem : Calculate the Body Mass Index based on weight and height Formula : bmi= weight / (height *height) 8.4.1 Sequence

14 Step 3: From Pseudocode to Programming start Read weight, height, Calculate bmi bmi = weight / (height*height) Print bmi end start Read weight, height, Calculate bmi bmi = weight / (height*height) Print bmi end cout <<" Enter weight : "; cin>>weight; cout <<" Enter height : "; cin>>height; bmi=weight / (height*height); cout<<“Your BMI is : "<<bmi; cout <<" Enter weight : "; cin>>weight; cout <<" Enter height : "; cin>>height; bmi=weight / (height*height); cout<<“Your BMI is : "<<bmi; S1,S2 S3 S4

15 Example 3 APM Industry management asks you to create a program that calculate the total overtime payment for staff if overtime rate payment was given RM 5.00 per hour. Example 3 APM Industry management asks you to create a program that calculate the total overtime payment for staff if overtime rate payment was given RM 5.00 per hour. Examples of the use of sequential control structure 8.4.1 Sequence

16 Examples of the use of sequential control structure STEP 1 Identify the control structure based on the problem statement given: Control Structure : Sequence Identify the control structure based on the problem statement given: Control Structure : Sequence 8.4.1 Sequence

17 Examples of programming the use of sequential control structure STEP 2 Identify The Problem & the Formula to be used : Problem : Calculate the total overtime payment for staff if overtime rate payment was given RM 5.00 per hour. Formula: Totalovertime payment = hour * 5.00 Identify The Problem & the Formula to be used : Problem : Calculate the total overtime payment for staff if overtime rate payment was given RM 5.00 per hour. Formula: Totalovertime payment = hour * 5.00 8.4.1 Sequence

18 start Total overtime payment = hour * 5.00 end hour Total overtime payment cin>> hour; total_overtime_payment = hour * 5.00; cout<<“Total Overtime Payment: “<< total_overtime_payment; cin>> hour; total_overtime_payment = hour * 5.00; cout<<“Total Overtime Payment: “<< total_overtime_payment; STEP 3 Convert from the Flow Chart to a C + + Program: S1 S2 S3

19 Summary  To develop simple programs that are easy to use sequence control structure  Specifying the order in which statements are to be executed in a computer program is called program control.  A sequence structured is used to execute sequentially one by one  To develop simple programs that are easy to use sequence control structure  Specifying the order in which statements are to be executed in a computer program is called program control.  A sequence structured is used to execute sequentially one by one 8.4.1 Sequence

20 8.4.2 Selection 20 if if…else nested if

21 8.4.2Selection The selection structure allows instructions to be executed in a non- sequenctial fashion It compares two expression, and based on the comparison, it takes a certain course of action. 21

22 There are three (3) selection structure: 1.The if statement 2.The if..else 3.The nested if statement 22 8.4.2Selection

23 Selection - i) Selection - if if Selection structure ▫Choose among alternative courses of action ▫The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is: if (expression) statement; ▫Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” 23 8.4.2Selection

24 ▫If the condition is true  Print statement executed, program continues to next statement ▫If the condition is false  Print statement ignored, program continues ▫Indenting makes programs easier to read  C++ ignores whitespace characters (tabs, spaces, etc.) 24

25 i)Selection - if  Translation into C++ Example 1: ◦ If student’s grade is greater than or equal to 60, Print “Passed” if ( grade >= 60 ) cout << "Passed";  Diamond symbol (decision symbol) ◦ Indicates decision is to be made ◦ Contains an expression that can be true or false  Test condition, follow path  if structure ◦ Single-entry/single-exit 25

26 i) Selection Structure - if Flowchart of pseudocode statement 26 true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero - true

27 i)Selection - if Example 2: if (hours > 40) hours = 40 + 2 * (hours - 40) print hours 27

28 i)Selection - if 28 if (hours > 40) hours = 40 + 2 * (hours - 40); cout << hours; hours>40 hours = 40 + 2 * (hours - 40) false true hours

29 i)Selection - if Example 3 : if (job_code == ‘1’) { car_allowance = 200.00; housing_allowance = 800.00; } 29

30 ii)Selection – if-else  if ◦ Performs action if condition true  if/else ◦ Different actions if conditions true or false  Pseudo code if student’s grade is greater than or equal to 60 print “Passed” else print “Failed”  C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed"; 30

31 ii)Selection – if-else The if-else selection structure allows the programmer to specify that a different choice is to be performed when the condition is true than when the condition is false. 31

32 ii)Selection – if-else An if - else statement has the form: if (expression) statement_1; else statement_2; 32

33 ii)Selection – if-else Either statement_1 is executed or statement_2, but not both 33 expression statement 1 false true statement 2

34 ii)Selection - if-else Example 1 : If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed” 34

35 ii)Selection - if-else 35 grade>=60 “Passed” “Failed” true false a) Flow chart

36 ii)Selection - if-else if (grade >= 60) cout << “Passed”; else cout << “Failed”; 36 b) C++ Code

37 ii)Selection - if-else Example 2 : a) Pseudocode if (job_code == ‘1’) print “rate = 5.00” else print “rate = 7.00” 37

38 ii)Selection - if-else 38 job_code== ‘1’ “rate = 5.00” “rate = 7.00” true false b) Flow chart

39 ii)Selection - if-else 39 if (job_code == ‘1’) cout << “rate = 5.00”; else cout << “rate = 7.00”; c) C++ Code

40 Example 3: If x is greater than y, display “x is bigger than y” else display “x is smaller than y”. 40 ii)Selection - if-else

41 a) Pseudocode 41 start x=3,y=4 if (x > y) print “x is bigger than y” else print “x is smaller than y” end start x=3,y=4 if (x > y) print “x is bigger than y” else print “x is smaller than y” end

42 ii)Selection - if-else b) Flow chart 42 start end “x is bigger than y” x > y true “x is smaller than y” false x =3, y=4

43 ii)Selection - if-else c) C++ program as an example: //include this file for cout #include int main() { // define two integers int x = 3; int y = 4; //print out a message telling which is bigger if (x > y) { cout << "x is bigger than y" << endl; } else { cout << "x is smaller than y" << endl; } return 0; } 43

44 ii)Selection - if-else Example 4 : a) Pseudocode if (radius < 0.0) print “Cannot have a negative radius” else print “Area of the circle is “<<3.1416 * radius*radius” 44 Calculate area of circle when radius is greater than 0.0 and display it as output. If not, display “Cannot have a negative radius”.

45 ii)Selection - if-else 45 radius<0.0 Cannot have a negative radius A rea of the circle is 3.1416 * radius*radius true false b) Flow chart end radius start

46 ii)Selection - if-else 46 c) C++ Code #include int main() { double radius; //get user input cout<<"Please enter the radius : "; cin>>radius; //act on user input if(radius < 0.0) cout<<"Cannot have a negative radius"<<endl; else cout<<"The area of the circle is "<<3.1416 * radius * radius<<endl; system("PAUSE"); return 0; }

47 iii)Selection – nested if An if-else statement can be nested to any depth. Nested if-else structure test for multiple cases by placing if-else selection structure inside if-else selection structure 47

48 iii)Selection – nested if –else if The nested if-else if structured takes the general form: if (expression_1) statement_1; else if (expression_2) statement_2; … else if (expression_n) statement_n; else last_statement; 48

49 iii)Selection – nested if- else if if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F” 49 Example 1 :

50 iii)Selection – nested if –else if if ( grade >= 90 ) // 90 and above cout = 80 ) // 80-89 cout = 70 ) // 70-79 cout = 60 ) // 60-69 cout << "D"; else // less than 60 cout << "F"; 50 c) C++ Code

51 iii)Selection – nested if –else if a) Pseudocode 51 start read x if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0" end start read x if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0" end

52 iii)Selection – nested if–else if b) Flow chart 52 start end x (x > 0) true "x is positive” false (x < 0) “x is negative” false “ x is 0 ” true

53 iii)Selection – nested if –else if #include int main() { int x; cout<<"Please enter : "; cin >> x; { if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0"; } system("PAUSE"); return 0; } 53 c) C++ Code

54 Summary A selection structured is used to choose among alternatives courses of action The if selection structure executes an indicated action only when the condition is true The if-else selection structure specifies separate actions to be executed when the condition is true and when the condition is false 54

55 Topic 8 PROGRAMMING 8.4 Use of control structure 8.4.3 Repetition Learning Outcome Write simple program using the looping control structure

56 HOW TO BUILD A CODING USING A REPETITION/ LOOPING CONTROL STRUCTURE looping The looping control structure: while 1. The while Construct

57 while Example 1: while Construct Previous Lesson: Remember to plan your algorithm. Do Problem Analysis. Transfer to Pseudocode or Flow Chart.

58 Example 1 Examples of programming the use of looping control structure-while Problem Statement: Calculate the area of a rectangle for 3 times using while-loop Problem Statement: Calculate the area of a rectangle for 3 times using while-loop width height

59 Examples of programming the use of looping control structure STEP 1 Identify the control structure based on the problem statement given: Control Structure : looping Identify the control structure based on the problem statement given: Control Structure : looping

60 Examples of programming the use of sequential control structure STEP 2 Identify The Problem & the Formula to be used : Problem : Calculate the area of rectangle 3 times Formula : area = width * height Identify The Problem & the Formula to be used : Problem : Calculate the area of rectangle 3 times Formula : area = width * height

61 InputProcessOutput width height area = width x heightarea STEP 3 Problem Analysis: Identify The Input, Process and Output: Problem Analysis: Identify The Input, Process and Output:

62 Pseudocode start 1. initialize counter i = 1 2. while (i <= 3) 3. read width, height 4. calculate area of rectangle area = width * height 5. print area 6. counter increment i = i + 1 7. repeat step 2 end IPO analysis initial value of counter condition to test increment to modify the loop Repeat step 2 – 6 until expression is false STEP 4 : Planning The Algorithm: while

63 Convert from TheAlgorithm to a C + + Program: Convert from TheAlgorithm to a C + + Program: STEP 5

64 Looping - while while( i <=3) int i=1; { } /*Create the braces because statements block are more than one, otherwise it will create an infinity result output*/ Initial value of control variable Loop-continuation condition Process items inside looping are based on condition given i = i +1; counter increment

65 float width, height, area; int i=1; while(i<=3) { cout <<" Enter width : "; cin>> width; cout <<" Enter height : "; cin>> height; area=width * height; cout <<"Rectangle area: "<<area; cout <<endl; i= i +1; } float width, height, area; int i=1; while(i<=3) { cout <<" Enter width : "; cin>> width; cout <<" Enter height : "; cin>> height; area=width * height; cout <<"Rectangle area: "<<area; cout <<endl; i= i +1; } Segment of C++ Program (while) Initial value of control variable Loop-continuation condition counter increment Process items inside looping are based on condition given

66 for Example 3. The for Construct looping The looping control structure: Problem Statement: Calculate the area of a rectangle for 3 times using for-loop Problem Statement: Calculate the area of a rectangle for 3 times using for-loop

67 Looping - for It has the following standard form. for (initialization; condition; increment) statement block; i)initialization – used to initialize any variable(s) that may need to be initialized. ii)condition – determines whether the loop execution should continue. If the value of the expression is TRUE (non-zero), the statement block will be executed, otherwise the loop will be terminated. iii)increment – typically increments (or decrements) the loop index - performed every time through the loop iteration – used to increment any variable(s) that may need to be incremented. (i)(ii) (iii)

68 Looping - for for (i =1; i <=3; i=i+1) int i; for (initialization; condition; increment) statement block; { } /*Create the braces because statements block are more than one, otherwise it will create an infinity result output */ (i)(ii) (iii) (i)(ii) (iii) Process items inside looping are based on condition given

69 Pseudocode start 1.initialize counter i = 1 2. for (i <= 3) 3. read width, height 4. calculate area of rectangle area = width * height 5. print area 6. counter increment i = i + 1 7. repeat step 2 end IPO analysis initial value of counter condition to test increment to modify the loop Repeat step 2 – 6 until expression is false for Example 3: for Construct

70 { float width, height, area; int i; for (i=1; i<=3; i=i+1) { cout <<" Enter width : "; cin>> width; cout <<" Enter height : "; cin>> height; area=width * height; cout <<"Area of rectangle : "<<area; cout <<endl; } { float width, height, area; int i; for (i=1; i<=3; i=i+1) { cout <<" Enter width : "; cin>> width; cout <<" Enter height : "; cin>> height; area=width * height; cout <<"Area of rectangle : "<<area; cout <<endl; } Segment C++ Program (for- loop) i) Initial value of control variable ii) Loop-continuation condition iii) Counter increment (i)(ii)(iii) Process items inside looping are based on condition given

71 Question: Problem : Create a program that display the sum of the three marks entered by user. Examples of programming The use of looping control structure -while -for

72 Planning The Algorithm: Pseudocode while- loop Planning The Algorithm: Pseudocode while- loop start 1. initialize counter i = 1 initialize sum=0 2. while (i <= 3) 3. read mark 4. calculate sum sum = sum + mark 6. counter increment i = i + 1 // i++ 7. repeat step 2 8. end while 9. print sum end initial value of counter increment to modify the loop condition to test initial value of accumulator sum

73 { int i=1; int mark; int sum=0; while(i<=3) { cout <<" Enter mark “<<i<< " : "; cin>> mark; sum=sum + mark; i= i +1; // i++ } cout <<“Sum : "<<sum<<endl; system("PAUSE"); return 0; } { int i=1; int mark; int sum=0; while(i<=3) { cout <<" Enter mark “<<i<< " : "; cin>> mark; sum=sum + mark; i= i +1; // i++ } cout <<“Sum : "<<sum<<endl; system("PAUSE"); return 0; } Simple C++ Program while loop

74 start 1. initialize sum=0 initialize counter i = 1 2. for (i <= 3) 3. read mark 4. calculate sum sum = sum + mark 5. counter increment i = i + 1 //i++ 6. repeat step 2 end for 7. print sum end initial value of counter condition to test increment to modify the loop Repeat step 2 – 5 until expression is false Planning The Algorithm: Pseudocode for-loop Planning The Algorithm: Pseudocode for-loop initial value of accumulators

75 { int i,mark; int sum=0; for (i=1;i<=3;i++) { cout <<" Enter mark “<<i<< " : "; cin>> mark; sum=sum + mark; } cout <<“Sum : "<<sum<<endl; system("PAUSE"); return 0; } { int i,mark; int sum=0; for (i=1;i<=3;i++) { cout <<" Enter mark “<<i<< " : "; cin>> mark; sum=sum + mark; } cout <<“Sum : "<<sum<<endl; system("PAUSE"); return 0; } Simple C++ Program for-loop

76 Summary  The repetition structure, also called looping allows a programmer to repeat one or more program instructions either a specified number of times or until some condition, referred to as the loop condition, is met.  Two types of repetition structure are while and for loop  The repetition structure, also called looping allows a programmer to repeat one or more program instructions either a specified number of times or until some condition, referred to as the loop condition, is met.  Two types of repetition structure are while and for loop

77 Summary  The while loop is called a pretest, or top-driven loop because it evaluates the loop condition before processing any of the statements within the loop.  The for loop allows you to code the loop in a more convenient and compact manner by implements a counter-controlled while loop.  The while loop is called a pretest, or top-driven loop because it evaluates the loop condition before processing any of the statements within the loop.  The for loop allows you to code the loop in a more convenient and compact manner by implements a counter-controlled while loop.

78 Summary  A counter is a numeric variable used for counting something, and accumulators is a numeric variable used for accumulating (adding together) something.  Counters and accumulators should be initialized, usually to 0, and updated.  The initialization task is typically done outside of the repetition structure and the updating task is done inside the repetition structure.  A counter is a numeric variable used for counting something, and accumulators is a numeric variable used for accumulating (adding together) something.  Counters and accumulators should be initialized, usually to 0, and updated.  The initialization task is typically done outside of the repetition structure and the updating task is done inside the repetition structure.


Download ppt "Topic 8 :PROGRAMMING 8.4 Use of control structure Learning Outcome : Write a program segment by using appropriate control structure."

Similar presentations


Ads by Google