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

## 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:

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

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

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

Examples of coding the use of sequential control structure cin>> sales; (S1) bonus = sales * (5/100); (S2) cout<<“Bonus: “<> sales; (S1) bonus = sales * (5/100); (S2) cout<<“Bonus: “< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3870259/slides/slide_4.jpg", "name": "Examples of coding the use of sequential control structure cin>> sales; (S1) bonus = sales * (5/100); (S2) cout<< Bonus: <> sales; (S1) bonus = sales * (5/100); (S2) cout<< Bonus: <

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

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

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

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

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

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

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 "<>width; cout <<" Enter length : "; cin>>length; area=width * length; Total_price= area * price_of_tile; cout<<"Total Price is : RM "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3870259/slides/slide_11.jpg", "name": "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 <>width; cout << Enter length : ; cin>>length; area=width * length; Total_price= area * price_of_tile; cout<< Total Price is : RM <>width; cout << Enter length : ; cin>>length; area=width * length; Total_price= area * price_of_tile; cout<< Total Price is : RM <>width; cout << Enter length : ; cin>>length; area=width * length; Total_price= area * price_of_tile; cout<< Total Price is : RM <

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

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

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 : "<>weight; cout <<" Enter height : "; cin>>height; bmi=weight / (height*height); cout<<“Your BMI is : "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3870259/slides/slide_14.jpg", "name": "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 : <>weight; cout << Enter height : ; cin>>height; bmi=weight / (height*height); cout<< Your BMI is : <>weight; cout << Enter height : ; cin>>height; bmi=weight / (height*height); cout<< Your BMI is : <>weight; cout << Enter height : ; cin>>height; bmi=weight / (height*height); cout<< Your BMI is : <

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

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

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

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

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

8.4.2 Selection 20 if if…else nested if

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

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

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

▫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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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”.

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

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

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

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 :

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

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

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

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

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

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

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

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

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

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

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

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:

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

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

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

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: "<> width; cout <<" Enter height : "; cin>> height; area=width * height; cout <<"Rectangle area: "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3870259/slides/slide_65.jpg", "name": "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: <> width; cout << Enter height : ; cin>> height; area=width * height; cout << Rectangle area: <> width; cout << Enter height : ; cin>> height; area=width * height; cout << Rectangle area: <> width; cout << Enter height : ; cin>> height; area=width * height; cout << Rectangle area: <

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

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)

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

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

{ 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 : "<> width; cout <<" Enter height : "; cin>> height; area=width * height; cout <<"Area of rectangle : "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3870259/slides/slide_70.jpg", "name": "{ 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 : <> width; cout << Enter height : ; cin>> height; area=width * height; cout << Area of rectangle : <> width; cout << Enter height : ; cin>> height; area=width * height; cout << Area of rectangle : <> width; cout << Enter height : ; cin>> height; area=width * height; cout << Area of rectangle : <

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

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

{ int i=1; int mark; int sum=0; while(i<=3) { cout <<" Enter mark “<> mark; sum=sum + mark; i= i +1; // i++ } cout <<“Sum : "<> mark; sum=sum + mark; i= i +1; // i++ } cout <<“Sum : "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3870259/slides/slide_73.jpg", "name": "{ int i=1; int mark; int sum=0; while(i<=3) { cout << Enter mark <> mark; sum=sum + mark; i= i +1; // i++ } cout << Sum : <> mark; sum=sum + mark; i= i +1; // i++ } cout << Sum : <> mark; sum=sum + mark; i= i +1; // i++ } cout << Sum : <> mark; sum=sum + mark; i= i +1; // i++ } cout << Sum : <

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

{ int i,mark; int sum=0; for (i=1;i<=3;i++) { cout <<" Enter mark “<> mark; sum=sum + mark; } cout <<“Sum : "<> mark; sum=sum + mark; } cout <<“Sum : "< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3870259/slides/slide_75.jpg", "name": "{ int i,mark; int sum=0; for (i=1;i<=3;i++) { cout << Enter mark <> mark; sum=sum + mark; } cout << Sum : <> mark; sum=sum + mark; } cout << Sum : <> mark; sum=sum + mark; } cout << Sum : <> mark; sum=sum + mark; } cout << Sum : <

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

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.

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