Download presentation
Presentation is loading. Please wait.
1
C++ Iterative Constructs
Three constructs while statement for statement do-while statement
2
While Syntax
3
While Semantics
4
Computing an Average int listSize = 4; int numberProcessed = 0;
double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
5
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
6
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
7
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum
8
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum
9
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value --
10
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value 1
11
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
12
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
13
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
14
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value --
15
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 5
16
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 1 value 5
17
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5
18
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 6 value 5
19
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value --
20
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 3
21
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 6 value 3
22
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
23
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
24
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value --
25
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 1
26
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 9 value 1
27
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1
28
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1
29
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 average 2.5
30
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 average 2.5
31
Stays in stream until extracted
Suppose input contains: Execution Trace Stays in stream until extracted int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
32
Power of Two Table const int TableSize = 20; int i = 0;
long Entry = 1; cout << "i" << "\t\t" << "2 ** i" << endl; while (i < TableSize) { cout << i << "\t\t" << Entry << endl; Entry = 2 * Entry; ++i; }
33
Better Way of Averaging
The value of the input operation corresponds to true only if a successful extraction was made int numberProcessed = 0; double sum = 0; double value; while ( cin >> value ) { sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; What if list is empty?
34
Even Better Way of Averaging
int numberProcessed = 0; double sum = 0; double value; while ( cin >> value ) { sum += value; ++numberProcessed; } if ( numberProcessed > 0 ) { double average = sum / numberProcessed ; cout << "Average: " << average << endl; else { cout << "No list to average" << endl;
35
The For Statement Syntax for (ForInit ; ForExpression; PostExpression)
Action Example for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; }
37
Execution Trace i for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl;
38
Execution Trace i for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl;
39
Execution Trace i for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
40
Execution Trace i for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
41
Execution Trace i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
42
Execution Trace i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl;
43
Execution Trace i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
44
Execution Trace i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
45
Execution Trace i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
46
Execution Trace i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1
47
Execution Trace i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2
48
Execution Trace i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2
49
Execution Trace i 3 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2
50
Execution Trace i 3 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2
51
Execution Trace i 3 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 all done
52
Table Revisiting const int TableSize = 20; long Entry = 1;
cout << "i" << "\t\t" << "2**i" << endl; for (int i = 0; i <= TableSize; ++i) { cout << i << "\t\t" << Entry << endl; Entry *= 2; }
53
Table Revisiting const int TableSize = 20; long Entry = 1;
cout << "i" << "\t\t" << "2**i" << endl; for (int i = 0; i < TableSize; ++i) { cout << i << "\t\t" << Entry << endl; Entry = 2 * Entry; } cout << "i is" << i << endl; // illegal The scope of i is limited to the loop!
54
int Counter1 = 0; int Counter2 = 0; int Counter3 = 0; int Counter4 = 0; int Counter5 = 0; ++Counter1; for (int i = 1; i <= 10; ++i) { ++Counter2; for (int j = 1; j <= 20; ++j) { Counter3; } ++Counter4; } ++Counter5; cout << Counter1 << " " << Counter2 << " " << Counter3 << " " << Counter4 << " " << Counter5 << endl;
55
For Into While Observation The for statement is equivalent to {
ForInit; while (ForExpression) { Action; PostExpression; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.