Problem solving and programming control structures.pptx

fizzamansoor3 23 views 179 slides Aug 29, 2025
Slide 1
Slide 1 of 179
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145
Slide 146
146
Slide 147
147
Slide 148
148
Slide 149
149
Slide 150
150
Slide 151
151
Slide 152
152
Slide 153
153
Slide 154
154
Slide 155
155
Slide 156
156
Slide 157
157
Slide 158
158
Slide 159
159
Slide 160
160
Slide 161
161
Slide 162
162
Slide 163
163
Slide 164
164
Slide 165
165
Slide 166
166
Slide 167
167
Slide 168
168
Slide 169
169
Slide 170
170
Slide 171
171
Slide 172
172
Slide 173
173
Slide 174
174
Slide 175
175
Slide 176
176
Slide 177
177
Slide 178
178
Slide 179
179

About This Presentation

Problem Solving, pseudocode and flowchart


Slide Content

Programming Approach in Problem Solving

1. 1 Approach in Problem Solving 1.1.1 Input Process Output (IPO) Analysis

Learning Outcome Identify input, process and output from a given problem

Input Process Output (IPO) Analysis The IPO is used to analyze problems and develop algorithms Used to organize and summarize the results of a problem analysis It shows where in the solution the processing takes place

Input Process Output (IPO) Analysis Input Processing Output Processing items: Algorithm It can also be represent using IPO chart

Input Process Output (IPO) Analysis Identify Input : Process : Output : To do the IPO Analysis , start with: Output (Display the results) Input (Read the data) Process (Perform the computation)

Input Process Output (IPO) Analysis Output should answer the following question: What does the user want to see printed on the printer, displayed on the screen, or stored in a file?

Input Process Output (IPO) Analysis Input should answer the following question: What information will the computer need to know to print, display, or store the output items?

Input Process Output (IPO) Analysis Processing item: An intermediate value that the algorithm uses when processing the input into the output

Analyze the Problem What is it you are trying to accomplish? What outcome are you trying to arrive at? List the Inputs and Outputs Often you work backwards from the Output List the Outputs, and then figure out what Inputs you need in order to arrive at the Outputs

follows Example 1 Problem statement: Calculate the area of a rectangle We can summarize the information contained in the problem statement as Analyze the Problem height width

Problem Analysis: Identify : INPUT : PROCESS : OUTPUT : To do the IPO Analysis, start with: – Output – Input – Process Analyze the Problem

Analyze the Problem Problem Analysis: Input: width, height Process: area = width x height Output: area of rectangle

Input width height Analyze the Problem Processing Processing items: area = width x height Algorithm : Output area of rectangle The Problem Analysis , can also be represent using IPO (Input, Processing, Output) Chart

Analyze the Problem Example 2 Problem statement: Find the average of three numbers input by user We can summarize the information contained in the problem statement as follows

Problem Analysis: Identify : INPUT : PROCESS : OUTPUT : To do the IPO Analysis, start with: – Output – Input – Process Analyze the Problem

Analyze the Problem Problem Analysis: Input: number1, number2, number3 Process: average = (number1 + number2 + number3) / 3 Output: average

Input number1 number2 number3 Analyze the Problem Processing Processing items: average = (number1 + number2 + number3) / 3 Algorithm : Output average The Problem Analysis , can also be represent using IPO (Input, Processing, Output) Chart

Analyze the Problem Example 3 Problem statement: Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo We can summarize the information contained in the problem statement as follows

Problem Analysis: Identify : INPUT : PROCESS : OUTPUT : To do the IPO Analysis, start with: – Output – Input – Process Analyze the Problem

Analyze the Problem Problem Analysis: Input: Number of kilos of apples Cost of apples per kilo Process: Total cost = Number of kilos of apples × Cost of apples per kilo Output: Total cost of apples

Analyze the Problem The Problem Analysis , can also be represent using IPO (Input, Processing, Output) Chart Input Number of kilos of apples Cost of apples per kilo Processing Processing items: Total cost = Number of kilos of apples × Cost of apples per kilo Algorithm : Output Total cost of apples

Summary What have you learned ? Approaches in problem solving Identify Input Process Output (IPO) from a problem statement

Approach in Problem Solving 1.1.2 Algorithm 1. 1.3 Pseudocode

Learning Outcome Define algorithm Solve problem using pseudocode

ALGORITHM The algorithm is the abstract idea of solving a problem. An algorithm is a step- by-step instructions that will transform the input into the output It can be represent using pseudocode or flow chart

From Algorithms to Programs Problem C++ Program Problem Analysis Algorithm

Algorithm in everyday’s life How to make a mug of hot coffee: Start Boil water Prepare a mug Put a tea spoon of coffee & sugar Pour hot water Stir End

can be described using three control structures (Bohm and Jacopini- 1966); A Sequence o is a series of statements that execute one after another A Selection – (branch) o statement is used to determine which of two different statements to execute depending on certain conditions A Looping – (repetition) stateme n t is used to repeat statements while certain conditions are met ALGORITHM

PLANNING THE ALGORITHM Transfer to pseudo code or flow chart a. Must start with a start b. Must close with an end 1. Do Problem Analysis Identify: INPUT : PROCESS : OUTPUT :

TECHNIQUES TO REPRESENT THE ALGORITHM Similar to everyday English Flow chart Graphical representation of an algorithm Special- purpose symbols connected by arrows (flow lines) 1.Pseudocod e 2. Flow chart Artificial , informal language used to develop algorithms Pseudocode

TECHNIQUES TO REPRESENT THE ALGORITHM Pseudocode Start: Input radius Calculate circle area circle area = 3.142 * radius * radius Print the circle area End: 1. Example of Pseudocode & Flow chart Calculate the area of a circle radius circle area = 3.142 * radius * radius circle area Flow chart start end

1. Pseudocode - Pseudocode Format start statement 1 statement 2 end TECHNIQUES TO REPRESENT THE ALGORITHM 1 3 2

Algorithm Design : Pseudocode Example 1 Problem statement: Calculate the area of a rectangle Remember! Do Problem Analysis Transfer to pseudo code or flow chart

Algorithm Design : Pseudocode 1. Problem Analysis: Input: width, height Process: area = width x height Output: area of rectangle

Algorithm Design : Pseudocode 2. Pseudocode start read width and height calculate area area of rectangle = width * height print area of rectangle end

IPO Chart with Pseudocode Input width height Processing Processing items: area = width x height Algorithm - Pseudocode: Start Read width and height Calculate area area of rectangle = width * height Print area End Output area of rectangle

Algorithm Design : Pseudocode Example 2 Problem statement: Find the average of three numbers input by user Remember! Do Problem Analysis Transfer to pseudo code or flow chart

Algorithm Design : Pseudocode 1. Problem Analysis: Input: number1, number2, number3 Process: average = (number1 + number2 + number3) / 3 Output: average

Algorithm Design : Pseudocode start input number1, number2, number3 calculate average average = (number1 + number2 + number3) / 3 print average end 2. Pseudocode

2/10/12 IPO Chart with Pseudocode Input number1 number2 number3 Processing Processing items: average = (number1 + number2 + number3) / 3 Algorithm - Pseudocode: Start Input number1, number2, number3 Calculate average average = (number1 + number2 + number3) / 3 Print average End Output average

Algorithm Design : Pseudocode Example 3 Problem statement: Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo Remember! Do Problem Analysis Transfer to pseudo code or flow chart

Algorithm Design : Pseudocode 1. Problem Analysis: Input: Number of kilos of apples Cost of apples per kilo Process: Total cost = Number of kilos of apples × Cost of apples per kilo Output: Total cost of apples

02/10/12 Algorithm Design : Pseudocode 2. Pseudocode start Read Number of kilos of apples and Cost of apples per kilo Calculate Total cost Total cost = Number of kilos of apples x Cost of apples per kilo Print Total cost of apples end

2/10/12 Input Number of kilos of apples Cost of apples per kilo Processing Total cost = Number of kilos of apples × Cost of apples per kilo Algorithm - Pseudocode: Start Read Number of kilos of apples and Cost of apples per kilo Calculate Total Cost Total cost = Number of kilos of apples × Cost of apples per kilo Print Total cost of apples End Output Total cost of apples IPO Chart with Pseudocode

Summary What have you learned ? Approaches in problem solving Planning the algorithm Steps in building pseudocode Problem solving using pseudocode

Approach in Problem Solving Flow Chart

Learning Outcome Solve problem using flow chart

TECHNIQUES TO REPRESENT THE ALGORITHM Statement 1 Statement 2 2. Flow Chart - Flow Chart Format start The statement refers to any input, output & process involved end

02/10/12 FLOW CHART - SYMBOLS Graphic Symbol Name Meaning Terminal indicates the beginning and end Symbol (oval) points of an algorithm Process Symbol (rectangle) shows an instruction other than input, output or selection Input-Output Symbol (parallelogram) shows an input or output operation

02/10/12 FLOW CHART - SYMBOLS Graphic Symbol Name Meaning Disk Storage Input- Output Symbol (cylinder) indicates input from or output to disk storage Printer Output Symbol shows hardcopy printer output (diamond) Selection Symbol shows a selection process for two- way selection

FLOW CHART - SYMBOLS Graphic Symbol Name Meaning Flow Lines (arrow) indicates the logical sequence of execution steps in the algorithm Off- Page Connector provides continuation of a logical path on another page On- Page Connector (circle) provides continuation of a logical path at another point in the same page

Algorithm Design : Flow Chart Example 1 Problem statement: Calculate the area of a rectangle Remember! Do Problem Analysis Transfer to pseudo code or flow chart

Algorithm Design : Flow Chart 1. Problem Analysis: Input: width, height Process: area = width x height Output: area of rectangle

Algorithm Design : Flow Chart 2. Flow Chart start width, height area = width * height area end

2/10/12 IPO Chart with Flow Chart Input width height Processing Processing items: area = width x height Output area of rectangle Algorithm – Flow Chart: start width, height area = width * height area end

Algorithm Design : Flow Chart Example 2 Problem statement: Find the average of three numbers input by user Remember! Do Problem Analysis Transfer to pseudo code or flow chart

Algorithm Design : Flow Chart 1. Problem Analysis: Input: number1, number2, number3 Process: average = (number1 + number2 + number3) / 3 Output: average

Algorithm Design : Flow Chart 2. Flow Chart start number1, number2, number3 average = (number1 + number2 + number3) / 3 average end

02/10/12 IPO Chart with Flow Chart Input number1 number2 number3 Processing Processing items: average = (num1 + num2 + num3) / 3 Output average Algorithm – Flow Chart: start number1, number2, number3 average = (number1 + number2 + number3) / 3 average end

Algorithm Design : Flow Chart Example 3 Problem statement: Determine the total cost of apples, given the number of kilos of apples purchased and the cost of apples per kilo Remember! Do Problem Analysis Transfer to pseudo code or flow chart

Algorithm Design : Flow Chart 1. Problem Analysis: Input: Number of kilos of apples Cost of apples per kilo Process: Total cost = Number of kilos of apples × Cost of apples per kilo Output: Total cost of apples

Algorithm Design : Flow Chart Number of kilos of apples, Cost of apples per kilo Total cost = Number of kilos of apples x Cost of apples per kilo Total cost of apples 2. Flow Chart start end

2/10/12 IPO Chart with Flow Chart Input Number of kilos of apples Cost of apples per kilo Processing Total cost = Number of kilos of apples × Cost of apples per kilo Algorithm – Flow Chart: Output Total cost of apples end start Number of kilos of apples, Cost of apples per kilo Total cost = Number of kilos of apples x Cost of apples per kilo Total cost of apples

Summary What have you learned ? Approaches in problem solving Steps in building flow chart Problem solving using flow chart

Revision What have you learned ? What is IPO analysis? What is algorithm? What are the steps in planning your algorithm? Differentiate between pseudocode and flowchart.

Approach in Problem Solving 1.2 Control Structure 1.2.1 Sequence

SEQUENCE Control Structures SELECTION LOOPING

Sequence Learning Outcome At the end of the lesson, students should be able to : Understand basic problem solving techniques. Use the sequence structured in problem solving. Develop flowchart through the process of top-down.

Sequence The simplest programs consist just sequence of statements : no loops, no selections amongst alternative actions, no use of subroutines.

Sequence 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. first instruction last instruction.

Sequence

Sequence Planning the Algorithm Record the algorithm using IPO analysis / IPO chart. Problem Analysis: Input: sales Process: bonus = sales * (0.05) Output: bonus

Algorithm Design : Pseudocode Pseudocode - tool programmers use to help them plan an algorithm - consist of short, English- like statements start input sales bonus = sales * (0.05) print bonus end

Algorithm Design : Flow Chart Flow Chart - Use standardized symbols to show the steps the computer need to take to accomplish the program’s goal start end sales bonus = sales * (0.05 ) bonus

Sequence

8.2.5.1 Sequence Planning the Algorithm Record the algorithm using IPO analysis / IPO chart. Problem Analysis: Input: width, length, price of tile Process: area = width * length total of price = area * price of tile Output: total of price

Algorithm Design : Pseudocode Pseudocode - tool programmers use to help them plan an algorithm - consist of short, English- like statements start input width, length, price of tile area = width * length total price of tile = area * price of tile print total price of tile end

Algorithm Design : Flow Chart start end width length price of tile area = width * length total price of tile = area * price of tile Total price of tile Flow Chart - Use standardized symbols to show the steps the computer need to take to accomplish the program’s goal

Sequence

8.2.5.1 Sequence Planning the Algorithm Record the algorithm using IPO analysis / IPO chart. Problem Analysis: Input: hour Process: Total overtime payment = hour * 5.00 Output: total overtime payment

Algorithm Design : Pseudocode Pseudocode - tool programmers use to help them plan an algorithm - consist of short, English- like statements start input hour total overtime payment = hour * 5.00 print total overtime payment end

Algorithm Design : Flow Chart start hour Total overtime payment = hour * 5.00 Total overtime payment end Flow Chart - Use standardized symbols to show the steps the computer need to take to accomplish the program’s goal

Summary Instruction in sequence programming are executed sequentially one by one First step in the problem- solving process it to analyze the problem Planning the algorithm is using IPO analysis / IPO chart Programmer uses the pseudocode as a guide when coding the algorithm Flow chart uses standardized symbols to show the steps the computer needs to take to accomplish the program’s goal

Approach in Problem Solving 1.2 Control Structure 1.2.2 Selection

Learning Outcome At the end of this topic, students should be able to: explain the purpose of selection control structure. apply selection control structure in problem solving.

Control Structure : Selection SELECTION Analogy You need to choose to make “Coffee O” or “Milk Coffee”

Control Structure : Selection SEQUENC E Control Structures SELECTION LOOPING

Control Structure : Selection What is Selection Structure? The selection structure allows instructions to be executed non-sequentially. It allows the comparison of two expressions, and based on the comparison, to select certain course of action.

Control Structure : Selection if- else Types of selection structure nested if- else switch if-else-if

Selection  (1) if-else If- else structure’s form if (expression) statement_1 else statement_2

Selection  (1) if-else In this form, the expression is first evaluated. If it evaluates to non- zero (TRUE), statement_1 is executed. Otherwise, statement_2 is executed. Either statement_1 or statement_2 is executed but not BOTH.

Selection  (1) if-else Pseudocode format: start if (expression) statement_1 else statement_2 end

Flow chart format: Selection  (1) if-else

Selection  (1) if-else Example 1: Print “Excellent!” when user enter marks greater than and equal to 80, else print “Sorry, try again”. Remember to plan your algorithm! 1.Do IPO Analysis Transfer to Pseudocode or Flow Chart

Selection  (1) if-else Example 1: Print “Excellent!” when user enter marks greater than and equal to 80 , else print “Sorry, try again”.

Selection  (1) if-else IPO Analysis : Input: marks Process: if (marks >= 80) print “Excellent” else print “Sorry, try again” Output: “Excellent!” or “Sorry, try again”

Selection  (1) if-else Pseudocode start read marks if (marks >= 80) print “Excellent!” else print “Sorry, try again” end

end Selection  (1) if-else Flow chart start marks marks >= 80 true “Excellent!” “Sorry, try again” false

Selection  (1) if-else Example 2: A high school poetry competition is open only for students above 15 years old. Display “Eligible” if the students meet the requirement, else display “Not eligible” if otherwise.

Selection  (1) if-else Example 2: A high school poetry competition is open only for students above 15 years old . Display “Eligible” if the students meet the requirement, else display “Not eligible” if otherwise.

Selection  (1) if-else IPO Analysis : Input: age Process: if (age > 15) print “Eligible” else print “Not eligible” Output: “Eligible” or “Not eligible”

Selection  (1) if-else Pseudocode start read age if (age > 15) print “ Eligible” else print “ Not eligible ” end

end Selection  (1) if-else Flow chart start age age > 15 true “Eligible” “Not eligible” false

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

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

Selection  (1) if-else IPO Analysis : Input: x,y Process: if (x > y) print “x is bigger than y” else print “x is smaller than y” Output: “x is bigger than y” OR “x is smaller than y”

Selection  (1) if-else Pseudocode start read x,y if (x > y) print “ x is bigger than y” else print “ x is smaller than y ” end

end Selection  (1) if-else Flow chart start x,y x > y true “x is bigger than y” “x is smaller than y” false

Selection  (2) nested if- else if- else Types of selection structure nested if- else switch if-else-if

Selection  (2) if- else …IF The if- else structure can also be nested to any depth.

Selection  (2) 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_3) statement_3; else statement_4;

In this nested form, expression_1 is evaluated. If it evaluates to non- zero (TRUE), statement_1 is executed. If not, control goes to the second if, where expression_2 is evaluated. If it evaluates to non- zero (TRUE), statement_2 is executed. If not, control goes to the third if, where expression_3 is evaluated. If it evaluates to non- zero (TRUE), statement_3 is 02 / 1 e / 1 x 2 e cu t e d. Selection  (2) if- else…If

Selection  (2) if- else…If If not, statement_4 is executed. Rules  Only ONE of the statements is executed.

Selection  (2) if- else…If Example 1: if student’s grade is greater than or equal to 80 Print “A” else if student’s grade is greater than or equal to 60 Print “B” else if student’s grade is greater than or equal to 50 Print “C” else Print “Failed”

Selection  (2) if- else…If IPO Analysis : Input: grade Process: if (grade >= 80) print “A” else if (grade >= 60) print “B” else if (grade >= 50) print “C” else print “Failed” Output: “A” or “B” or “C” or “Failed”

Selection  (2) if- else…If Pseudocode start read grade if (grade >= 80) print “A” else if (grade >= 60) print “B” else if (grade >= 50) print “C” else print “Failed” end

Selection  (2) if- else…If end Flow chart start grade true “A” “B” “C” grade >= 80 false grade >= 60 false grade >= 50 false “Failed” true true

Selection  (2) if- else…If Example 2: if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0"

Selection  (2) if- else…If IPO Analysis : Input: x Process: if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0“ Output: “ x is positive“ or "x is negative“ or “ x is 0”

Selection  (2) if- else…If Pseudo code start read x if (x > 0) display "x is positive" else if (x < 0) display "x is negative" else display "x is 0" end

Selection  (2) if- else…If Flow chart start x true "x is positive” “x is negative” (x > 0) false (x < 0) false “ x is ” end true

Selection  (3) Nested If else if- else Types of selection structure nested if- else switch if-else-i f

Selection  (3) Nested If else A nested if-else statement is a conditional structure where one if or else block contains another if-else statement. This allows for more complex decision-making by testing multiple conditions in a hierarchical manner.

if (condition1) { // Code block if condition1 is true if (condition2) { // Code block if condition2 is true } else { // Code block if condition2 is false } } else { // Code block if condition1 is false } Selection  (3) Nested If else

Selection  ( 4 ) Switch if- else Types of selection structure nested if- else switch if-else-i f

Selection  ( 4 ) Switch Switch case statements are to check several possible constant values for an expression. Switch form: switch (expression) { case constant1: group of statements1 break case constant2: group of statements2 break . . . default: default group of statements }

Selection  ( 4 ) Switch switch evaluates expression and checks if it is equivalent to constant1 , if it is, it executes group of statements1 until it finds the break statement.

Selection  ( 4 ) Switch If expression was not equal to constant1 it will be checked against constant2 . If it is equal to this, it will execute statements2.

Selection  ( 4 ) Switch If the value of expression DID NOT match any of the previously specified constants (you can include as many case labels as values you want to check), the program will execute the statements included after the default .

Selection - switch switch Flow chart

Selection  (4) Switch Example 1: x 1 2 Other value Print X is 1 X is 2 Value of x unknown

Selection  (4) Switch IPO Analysis : Input: x Process: switch (x) { case 1: print "x is 1“ break case 2: print "x is 2“ break default: Print "value of x unknown” } Output: "x is 1" or "x is 2" or "value of x unknown”

Selection  (4) Switch Pseudocode start read x switch (x) { case 1: print "x is 1“ break case 2: print "x is 2" break default: print "value of x unknown” } end

b) Flow chart iv) Selection - switch

SUMMARY if- else Types of selection structure nested if- switch else * Use an appropriate selection control structure in problem solving

Approach in Problem Solving 1.2 Control Structure 1.2.3 Looping

SEQUENC E Control Structures SELECTION LOOPING

Learning Outcome At the end of this topic, students should be able to: explain the purpose of looping control structure. apply looping control structure in problem solving.

What is looping control structure? The looping (or repetition) structure allows a sequence of instructions to be executed repeatedly until a certain condition is reached. The looping structure has three forms: o while o do..while o for

The essentials of looping (repetition) The loop counter (loop control variable ): Counts the number of repetitions in the counter- controlled loop Controls the execution of loop The loop counter requires the following to be known in advance: Name of a loop counter Initial value of the loop counter Increment (or decrement ) by which to modify the loop counter in each traversal Condition to test the final value of loop counter

The looping control structure: 1. The while Construct

The while Construct The while construct has the general form: while (expression) statement; where the expression is first evaluated. If it is true (not zero), the *statement(s) is executed, else if it false (zero), the statement is bypassed. * note : statement(s); which can be a block of statements – more than 1 statement

Applying Algorithm in while Construct Pseudocode start initialize counter while (expression) statement(s) counter increment end 1 2 4 3 * note : please refer to slide #4 for numbering details

Applying Algorithm in while Construct Flow Chart start initialize counter while (expression) True statement(s) counter increment False end

The Counter Table for while Construct counter expression input process output counter increment initial value of counter condition to test IPO analysis increment to modify the loop process output i = i + 1 ample of while counter table: i i <= 3 input

Example 1: while Construct Problem Statement: Calculate the area of a rectangle for 3 times. width H eight Remember to plan your algorithm. Do Problem Analysis. Transfer to Pseudocode or Flow Chart.

i i <= 3 area i = i + 1 while counter table width,height area = width * height Example 1: while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area

i i <= 3 width,height area = width * height area i = i + 1 1 T 2 2 T 3 3 T 4 Loop terminate Example 1: while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area while counter table

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

Flow Chart Example 1: while Construct start (i <= 3) True width, height i = i + 1 i = 1 False end area = width * height area

The looping control structure: 2. The do..while Construct

The do..while Construct The do..while construct has the general form: do statement while (expression); where the expression is last evaluated, after the statement(s) is executed. This means the statement(s) in the do..while will be executed at least once .

Applying Algorithm in do..while Construct Pseudocode start initialize counter do statement(s) counter increment while (expression) end 1 2 4 3 * note : please refer to slide #4 for numbering details

Applying Algorithm in do..while Construct Flow Chart start True initialize counter statement(s) counter increment while (expression) False end

The Counter Table for do..while Construct counter input process output counter increment expression initial value of counter condition to test IPO analysis increment to modify the loop output i = i + 1 i <= 3 ample of do..while counter table: i input process

Example 2: do..while Construct Problem Statement: Calculate the area of a rectangle for 3 times. width H eight Remember to plan your algorithm. Do Problem Analysis. Transfer to Pseudocode or Flow Chart.

i width,height i = i + 1 i <= 3 do..while counter table area = width * height area Example 2: do..while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area

02/10/12 i width,height area = width * height area i = i + 1 i <= 3 1 2 T 2 3 T 3 4 F Loop terminate Example 2: do..while Construct Problem Analysis: Input: width, height Process: area = width * height Output: area do..while counter table

02/10/12 Pseudocode start Example 2: do..while Construct initialize counter i = 1 do input width, height calculate area of rectangle area = width * height print area counter increment i = i + 1 repeat while (i <= 3) end IPO analysis initial value of counter condition to test increment to modify the loop

02/10 /12 Flow Chart Example 2: do..while Construct start (i <= 3) True False end i = 1 width, height area = width * height area i = i + 1

02/10/12 3. The for Construct The looping control structure:

* n o 2 t / e 1 : s / 1 t a 2 t e me n t ( s ); which can be a block of statements – more than 1 statement The for Construct The for construct has the general form: for (initialization; expression; incrementation) statement; where the initialization refers to the initial value of a loop counter. the expression determines whether the loop should be continued; if it is true (not zero), the *statement(s) is executed, else if it false (zero), the for loop is terminated. The incrementation increments the loop counter.

* note : please refer to slide #4 for numbering details 02/10/12 Applying Algorithm in for Construct Pseudocode 2 1 4 3 start for (initialize counter; expression; counter increment) statement(s); end

start for (initialize counter; expression; counter increment) statement(s); end After one- time initialization, the expression is first evaluated. If it is false , the for loop is terminated; if it is true the statement(s) in the loop is executed. And then the incrementation is performed. The expression condition is again evaluated, until 02 / 1 t h / e 1 2 e x p r e ss io n becomes false. The for loop is executed as follows:

Applying Algorithm in for Construct Flow Chart start True statement(s) counter increment initialize counter False (expression) end

The Counter Table for for Construct counter expression input process output counter increment initial value of counter condition to test IPO analysis increment to modify the loop process output i = i + 1 ample of for counter table: i i <= 3 input

Example 3: for Construct Problem Statement: Calculate the area of a rectangle for 3 times. width height Remember to plan your algorithm. Do Problem Analysis. Transfer to Pseudocode or Flow Chart.

i i <= 3 area i = i + 1 for counter table width,height area = width * height Example 3: for Construct Problem Analysis: Input: width, height Process: area = width * height Output: area

i i <= 3 width,height area = width * height area i = i + 1 1 T 2 2 T 3 3 T 4 4 F Loop terminate Example 3: for Construct Problem Analysis: Input: width, height Process: area = width * height Output: area for counter table

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

Flow Chart Example 3: for Construct start (i <= 3) True width, height i = i + 1 i = 1 False end area = width * height area

Accumulating in Problem Solving Accumulating or summing is a task a program must often perform. Accumulating means summing a group of numbers : where a variable is added to another variable, which holds the value of the sum or total. The expression for accumulating is as follow: m = m + su su num total = total + marks 2 1 2 1

Solving? How to apply Accumulating in Problem Example 4: Accumulating in Sequence Structure Problem Statement: Calculate the average of three numbers. Problem Analysis: Input: num1, num2, num3 Process: total = num1 + num2 + num3 average = total / 3 or average = (num1 + num2 + num3) / 3 Output: average Accumulating

How to apply Accumulating in Looping? Example 5: Accumulating in Looping Structure Problem Statement: Calculate the average of three numbers. Problem Analysis: Accumulating Input: num Process: total = total + num average = total / 3 Output: average

Example 5: Accumulating in Looping Structure Problem Analysis: Input: num Process: total = total + num average = total / 3 Output: average Using accumulating in while construct ( while counter table) total i i <= 3 num total = total + num i = i + 1 average = total / 3 average 1 T 70 70 2 2 T 80 150 3 3 T 90 240 4 02 /1 / 4 1 2 F Loop terminate average = 240 / 3 80

Pseudocode for Accumulating in while initial value of total initial value of counter condition to test IPO analysis increment to modify the loop Repeat step 3 – 6 until expression is false IPO analysis Construct: start initialize total total = initialize counter i = 1 while (i <= 3) input num accumulate total total = total + num counter increment i = i + 1 repeat until i > 3 calculate average average = total / 3 print average end

Flow Chart for Accumulating in while Construct: (i <= 3) True num end total = total + num i = i + 1 start total = i = 1 False average = total / 3 average

Summary What have you learned? The three forms of looping control structure: while do..while for The loop counter requirement. Apply looping control structure in problem solving. Remember to plan your algorithm. Do Problem Analysis. Transfer to Pseudocode or Flow Chart. Applying accumulating in looping.
Tags