Download presentation
Presentation is loading. Please wait.
1
Lecture 4A Repetition Richard Gesick
2
Overview Iteration 3 kinds of loops for while do while Infinite Loops
3
Example: Computing Velocity over a Range of Time.
Sequential structures are not well suited for computing the same quantities repeatedly. Flow charts and pseudocode are useful to describe repetition.
4
Looping In computing, we often need to perform the same operations on multiple items. Typically, these tasks follow this pattern: initialize values (set total to 0) process items one at a time (add price to total) report results (report total) The flow of control that programmers use to complete jobs with this pattern is called looping, or repetition.
5
Iteration One thing that computers do well is repeat commands
Programmers use loops to accomplish this 3 kinds of loops in C++ for loop while loop do while loop
6
Criteria for loops Usually have some initial condition
Starting a counter Beginning in a certain state Must have a test to continue Must make progress towards finishing
7
“I will not pour Clorox in the fish tank”
Loops in Everyday Life Bad children are told to write sentences on the board “I will not pour Clorox in the fish tank” Have to write this sentence either A certain number of times Until the teacher is happy As many as you can during break
8
The for Loop Ideal when you know the number of iterations to perform before the loop begins Examples: Find the sum of 5 numbers Find the maximum of 20 numbers Print the odd numbers from 1 to 10
9
The for loop Good when you know exactly how many times you need to execute something Has format: for (<initialization>; <test to continue>; <increment>) { // everything in here is what is repeated // over and over again } Initialization is where the counter is given a starting value The test determines whether or not to continue The increment can be any amount, including negative, and occurs after the loop statements execute
10
More for Loop Syntax Notes:
for ( initialization; loop condition; loop update ) { // loop body } Notes: semicolons separate terms in the loop header no semicolon follows the loop header curly braces are required only if more than one statement is in the loop body
11
for Loop Flow of Control
12
for Loop Flow of Control
The initialization statement is executed (once only). The loop condition is evaluated. If the condition is true, the loop body is executed. The loop update statement is executed, and the loop condition is reevaluated (#2). And so on, until the condition is false.
13
Satisfying the Teacher
Example: 1000 sentences? No problem… int counter; for (counter = 1; counter <= 1000; counter++) { cout<<“I will not pour Clorox…”<<endl; } // Remember, counter++ is the same as // counter = counter + 1
14
“But I want them numbered!”
No problem… int counter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; }
15
Why this works 1 counter int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } 1
16
Why this works 1 counter true int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } true 1
17
Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 1 I will not pour Clorox in the Fish Tank 1
18
Why this works 2 counter int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } 2
19
Why this works 2 counter true int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } true 2
20
Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 2 I will not pour Clorox in the Fish Tank 2
21
Why this works 3 counter int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } 3
22
Why this works 3 counter true int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } true 3
23
Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 3 I will not pour Clorox in the Fish Tank 3
24
Why this works 4 counter int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } 4
25
When will it end? We see that this will go on for a while
It’s a little more interesting later around 1000
26
Why this works 999 counter true int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } true 999
27
Why this works counter int counter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 999 I will not pour Clorox in the Fish Tank 999
28
Why this works 1000 counter int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } 1000
29
Why this works 1000 counter true for last time int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } true for last time 1000
30
Why this works (are we finished?)
counter int counter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 1000 I will not pour Clorox in the Fish Tank 1000
31
Why this works 1001 counter int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } 1001
32
Why this works 1001 … counter false int counter;
for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } // Jump down here and continue … false 1001
33
Final Output 1 I will not pour Clorox in the fish tank. 2 I will not pour Clorox in the fish tank. 3 I will not pour Clorox in the fish tank. 4 I will not pour Clorox in the fish tank I will not pour Clorox in the fish tank I will not pour Clorox in the fish tank.
34
Another Example of Repetition
int num; for ( num = 1 ; num <= 3 ; num++ ) { cout<<num << “ Potato”<<endl; }
35
num ? int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT
36
num 1 int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT
37
1 num int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; true OUTPUT
38
num 1 int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT 1Potato
39
num 2 int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT 1Potato
40
2 num int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; true OUTPUT 1Potato
41
num 2 int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT 1Potato 2Potato
42
num 3 int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT 1Potato 2Potato
43
3 num int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; true OUTPUT 1Potato 2Potato
44
num 3 int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT 1Potato 2Potato 3Potato
45
num 4 int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; OUTPUT 1Potato 2Potato 3Potato
46
4 num int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; false OUTPUT 1Potato 2Potato 3Potato
47
4 num int num; for ( num = 1 ; num <= 3 ; num++ ) cout<<num << “ Potato”<<endl; false When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement.
48
The output was: 1Potato 2Potato 3Potato
49
int count ; for ( count = 4 ; count > 0 ; count-- ) { cout<<count<<endl; } cout<<“Done”<<endl; OUTPUT: 4 3 2 1 Done
50
The while Loop The while loop is designed for repeating a set of operations on data items when we don't know how many data items there will be. We will get some signal when we have reached the end of the items to process. The end of data items could be indicated by a special input value called a sentinel value or by reaching the end of a file Receiving the signal is an event; we call this event-controlled looping
51
The while loop Good for when you don’t know how many times to repeat
Teacher says “Write until I’m happy” Has format: while (<boolean value>) { // stuff to repeat over and over }
52
The while Statement Syntax: while (boolean_expression) statement;
… statement_n; } Examples while (!isspace(ch)) cin.get(ch); //statement repeated until a // space character is read while (x>y) { //statement block executed //while x>0 ++cl; --x;
54
Operation of the while Loop
If the condition evaluates to true, the loop body is executed, then the condition is re-evaluated. As long as the condition evaluates to true, we continue to repeat the loop body. The loop body must "update the loop condition"; that is, it must perform some operation that eventually will cause the loop condition to evaluate to false Typically, the loop update will be an attempt to read the next input value, in order to detect the sentinel value or the end of the file.
55
Loop Characteristics All loops have the following three characteristics: initialization test update
56
Count-controlled Loop
int count ; count = 4; // initialize loop variable while (count > 0) // test expression { cout<<count<<endl; // repeated action count -- ; // update loop variable } cout<<“Done”<<endl;
57
Count-controlled Loop
int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT
58
Count-controlled Loop
4 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT
59
Count-controlled Loop
4 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; TRUE OUTPUT
60
Count-controlled Loop
4 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4
61
Count-controlled Loop
3 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4
62
Count-controlled Loop
3 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; TRUE OUTPUT 4
63
Count-controlled Loop
3 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4 3
64
Count-controlled Loop
2 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4 3
65
Count-controlled Loop
2 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; TRUE OUTPUT 4 3
66
Count-controlled Loop
2 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4 3 2
67
Count-controlled Loop
1 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4 3 2
68
Count-controlled Loop
1 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; TRUE OUTPUT 4 3 2
69
Count-controlled Loop
1 int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4 3 2 1
70
Count-controlled Loop
int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4 3 2 1
71
Count-controlled Loop
int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; FALSE OUTPUT 4 3 2 1
72
Count-controlled Loop
int count ; count = 4; while (count > 0) { cout<<count<<endl; count -- ; } cout<<“Done”<<endl; OUTPUT 4 3 2 1 Done
73
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; }
74
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; }
75
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; } Output: 1 I will not pour Clorox in the fish tank
76
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; }
77
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; }
78
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; } Output: 1 I will not pour Clorox in the fish tank
79
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; }
80
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; }
81
Example: Re-Writing 1-1000 (using a while loop)
counter 1 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; } Output: 1 I will not pour Clorox in the fish tank
82
} Infinite Loops This loop isn’t making a lot of progress!
Loops that repeat forever are called infinite loops Apparently “lock up” Output: 1 I will not pour Clorox in the fish tank . } Continue forever
83
Problem Solved… 1 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; }
84
Problem Solved… 1 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; }
85
Problem Solved… 1 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } Output: 1 I will not pour Clorox in the fish tank
86
Problem Solved… 2 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } // Remember, counter++ is the same as // counter = counter + 1
87
Example: Re-Writing 1-1000 (using a while loop)
counter 2 int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; }
88
Problem Solved… 2 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } 2
89
Problem Solved… 2 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } Output: 2 I will not pour Clorox in the fish tank 2
90
Problem Solved… 3 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } 3
91
How does it end? counter int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } 999
92
How does it end? counter int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } 999
93
How does it end? counter int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } 999
94
Problem Solved… 999 counter
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } Output: 999 I will not pour Clorox in the fish tank 999
95
How does it end? counter int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } 1000
96
How does it end? counter int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } 1000
97
How does it end? 1000 counter now false
int counter = 1; while (counter < 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } // So we never print out // 1000 I will not pour Clorox in the fishtank now false 1000
98
Another Problem Solved
counter int counter = 1; while (counter <= 1000) { cout<<counter<< “ I will not…”<<endl; counter++; } now true 1000
99
Example bool teacherHappy = false; int lineNumber = 1; while (!teacherHappy) { cout<<lineNumber << “ I will not…”<<endl; lineNumber++; teacherHappy = attitudeFunction ( ); } // assume attitudeFunction can change // teacherHappy
100
The do-while loop Similar to while loop
Must execute at least one time (test is at bottom) Has format: do { }while (<boolean value>);
101
Example (count from 1 to 3)
counter int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3);
102
Example (count from 1 to 3)
counter int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3);
103
Example (count from 1 to 3)
counter 1 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3);
104
Example (count from 1 to 3)
counter 1 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3); Output: 1
105
Example (count from 1 to 3)
counter 1 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3);
106
Example (count from 1 to 3)
counter 2 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3);
107
Example (count from 1 to 3)
counter 2 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3); Output: 2
108
Example (count from 1 to 3)
counter 2 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3);
109
Example (count from 1 to 3)
counter 3 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3); // Note: counter is now 3, but we still have // to finish out the loop – it doesn’t skip
110
Example (count from 1 to 3)
counter 3 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3); Output: 3
111
Example (count from 1 to 3)
counter 3 int counter = 0; do { counter++; cout<<counter<<endl; } while (counter < 3); now false, so loop is finished
112
Summary for loops good for when you know how many times you want to repeat while and do-while good for when you don’t All loops must finish, or they become infinite loops All loops must have a test to continue, or they become infinite loops
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.