Repetition and Loop Statements Chapter 5

advertisement
Repetition and Loop Statements
Chapter 5
Why iterate?

Example:
Algorithm for searching a letter w in a string that consists of n letters
1. Extract first letter w0
1.1 if(w == w0) return; // w was found
2. Extract first letter w1
1.1 if(w == w1) return; // w was found
…
n. Extract first letter wn
1.1 if(w == wn) return; // w was found
n+1.// w was not found
 n steps are needed (n if statements)
 All these steps do practically the same task: extract a letter and check for its
occurrence
 Number of used statements rises with increasing string length!
 Program must be changed if the number n changes!
2
Why iterate?



Use the computer's speed to do the same
task faster than if done by hand.
Avoid writing the same statements over
and over again.
Programs can be made general enough to
handle other types of data.
3
Repetitive control structures
– Because many algorithms require many
iterations over the same statements.
• To average 100 numbers, we would need
300 plus statements.
• Or we could use a statement that has the
ability to repeat a collection of statements
4
5.1 Counting Loops and the
while Statement
– General form of the while statement:
while ( loop-test )
{
iterative-part
}
– When a while loop executes, the loop-test is evaluated.
If true (non-zero), the iterative part is executed and the
loop-test is reevaluated. This process continues until the
loop test is false.
– While is a pre test loop
5
Collections of statements are
delimited with { and }

Example: Average of 100 numbers
while there is another number, do the following
{
cout << "Enter number: ";
cin >> number;
sum = sum + number;
}
average = sum / 100;
6
Example: counting loop

Display pay for 7 employees:
countEmp = 0;
while(countEmp < 7) {
cout << “Hours: “;
cin >> hours;
cout << “Rate: “;
cin >> rate;
pay = hours*rate;
cout << “Weekly pay is: “ << pay << endl;
countEmp = countEmp + 1;
}
7
Steps for counting loops


1. Initialization of the control variable
2. Test of the while condition
– false:  exit loop (go to Step 6)
– true:  execute body (Step 3)




3. Execution of iterative part
4. Update of control variable (e.g. increment)
5. Go to step 2
6. First statement after the while loop
8
Flowchart view of while loop
False
countEmp < 7
True
Get data
Compute pay
Iterative
Part
Display pay
Inc countEmp by 1
9
Infinite loops
– Infinite loop: a loop that never terminates.
Termination occurs if, for example, the while
condition becomes false.
– Infinite loops are usually not desirable.
– Here is an example of an infinite loop:
10
Infinite loops
int counter = 1;
int j = 0;
int n = 3;
while(counter <= n)
{
j = j + 1;
cout << counter << endl;
}
cout << "Do we ever get here?" << endl;
There is no step that brings us to termination.
11
Infinite loops

Another example:
while(true) {
statement(s)-1; // while body
}
statement(s)-2; // these are never executed (unless a break is used in the body)
Other extreme: No loop at all
while(false) {
statement(s)-1; // these are never executed
}
statement(s)-2;
 (compare: if(false) doSomething;)

Infinite loops are a big source of (logic) faults in real programs !!

12
5.2 Accumulating a Sum or
Product in a Loop



Lets look at the idea of adding together a
group of numbers
Many lines of the same statements
Sample code without a loop
13
Sum 100 values the hard way
int sum = 0;
cout << "Enter number: ";
// <-Repeat these three
cin >> number;
// <- statements for each
sum = sum + number;
// <- number in the set
cout << "Enter number: ";
with a while loop: only 8 lines !!!
cin >> number;
sum = sum + number;
int sum, iterations;
/*
sum = iterations = 0;
...291 statements deleted ...
while(iterations < 100) {
*/
cout << "Enter number: ";
cout << "Enter number: ";
cin >> number;
cin >> number;
sum = sum + number;
sum = sum + number;
iterations = iterations + 1;
average = sum / 100;
}
average = sum/100;
 more than 300 lines of code
14
Compound Assignment
Operators

Increment
variable++; is equivalent to variable = variable + 1;

Decrement
variable--; is equivalent to variable = variable – 1;

Short hand notation
totalPay += pay;
is equivalent to
totalPay = totalPay + pay;

In general
variable op= expression;
is equivalent to
variable = variable op expression;
15
ComputePay.cpp
// FILE: ComputePay.cpp
// COMPUTES THE PAYROLL FOR A COMPANY
// Number of Employees is variable
#include <iostream>
#include "money.h"
int main ()
{
int numberEmp;
int countEmp;
float hours;
money rate;
money pay;
money totalPay;
16
ComputePay.cpp
// Get number of employees from user.
cout << "Enter number of employees: ";
cin >> numberEmp;
// Compute each employee's pay and add it to
// the payroll.
totalPay = 0.0;
countEmp = 0;
17
ComputePay.cpp
while (countEmp < numberEmp)
{
cout << "Hours: ";
cin >> hours;
cout << "Rate : $";
cin >> rate;
pay = hours * rate;
cout << "Pay is " << pay << endl << endl;
totalPay += pay;
countEmp++;
}
18
ComputePay.cpp
cout << "Total payroll is " << totalPay <<
endl;
cout << "All employees processed." << endl;
return 0;
}
19
ComputePay.cpp
Program Output
Enter number of Employees: 3
Hours: 50
Rate: $5.25
Pay is: $262.5
Hours: 6
Rate: $5
Pay is: $30
Hours: 1.5
Rate: $7
Pay is: $105
Total payroll is $397.5
All employees processed.
20
Break Statement

The break statement can be used in order to
terminate the loop under program control and
without relying on the test condition

Example:
// read numbers until 0 is typed in
while(true) {
cin >> number;
cout << number << endl;
if(number == 0)
break;
}
21
Extraction operator and While
loop


The extraction operator may be used in the test
condition of a while loop
Example
while(cin >> number)
cout << number << endl;

Semantics: If there in any input, the “cin>>”
returns a non-zero value (true), else the returned
value is 0 (false)
– Terminating the loop is OS-dependent
– E.g. usage of the escape sequence Ctrl+d in Unix
22
5.3 The for Statement
– The for loop is similar to the other C++ looping
construct the while loop.
– The for loop forces us to write, as part of the
for loop, an initializing statement, the loop-test,
and a statement that is automatically repeated
for each iteration.
– Pre test loop.
23
Example for loop
– This is a for-loop version of a counter-controlled loop :
– Scope of the loop control variable:
for(int counter = 1; counter <= 5; counter = counter+1)
{
cout << counter << " ";
}
• Output: ________________________________?
24
General form of a for loop
for( initial statement ; loop-test ; repeated statement)
{
iterative-part
}
Steps:
1.
2.
3.
4.
5.
6.
Execute initial statement
Evaluate loop test:
2.1 false: Go to Step 6
2.2 true: Go to Step 3
Execute iterative-part
Execute repeated statement
Go to step 2
First Statement after the loop
25
Flowchart for a for loop
Initial statement
False
True
countEmp < 7
Get Data
Compute Pay
Display Pay
Increase countEmp by 1
26
Other In/De-crementing
Operators
–
–
–
–
Increment:
++x;
x++;
Decrement:
--x;
--x;
In expressions:
a) Post-increment (-decrement)
n = m++;
n = m--;
b) Pre-increment (decrement)
n = ++m;
n = --m;
Avoid them in complex expressions, since compilers optimize code using
commutativity:
x = 5;
i = 2;
y = i *x + ++i ;
(y may be 13 or 18)
27
More Examples

Displaying a String Letter by Letter
string name;
cout << “Enter your name: “ ;
cin >> name;
for(int pos = 0; pos < name.length(); pos++)
cout << name.at(pos) << endl;

Displaying a table of Celsius/Fahrenheit pairs
cout << Celsius” << “ Fahrenheit” << endl;
for(int celsius = 10; celsius >= -5; celsius -=5)
cout << setw(5) << celsius << setw(15) << ,,1.8*celsius + 32.0 << endl;
Manipulator: setw(x) - set width to x - used to format output as a table.
28
5.4 Conditional Loops

In many programming situations, you will not be
able to determine the exact number of loop
repetitions

Conditional Loop
– Initialize the loop control variable
– While a condition involving the loop control
variable is true
– Continue processing
– Update the loop control variable
29
Conditional Loops

Case Study:
Monitoring Oil Supply
– Problem: We want to monitor the amount of oil
remaining in a storage tank at the end of each day. The
initial supply of oil in the tank and the amount taken out
each day are data items. Our program should display
the amount left in the tank at the end of each day and it
should also display a warning when the amount left is
less than or equal to 10 percent of the tank’s capacity.
At this point, no more oil can be removed until the tank
is refilled.
30
Conditional Loops
– Analysis: Clearly, the problem inputs are the initial oil
supply and the amount taken out each day. The outputs are
the oil remaining at the end of each day and a warning
message when the oil left in the tank is less than or equal to
10 percent of its capacity. Constants to be used are the
capacity of the tank and the 10% warning threshold.
– Testing: To test the program, try running it with a few
samples of input data. One sample should bring the oil level
remaining to exactly 10 percent of the capacity. For example,
if the capacity is 10,000 gallons, enter a final daily usage
amount that brings the oil supply to 1,000 gallons and see
what happens.
31
OilSupply.cpp
// File: oilSupply.cpp
// Displays daily usage and amount left in oil tank.
#include <iostream>
using namespace std;
const float CAPACITY = 10000; // tank capacity
const float MINPCT = 10.0;
// minimum percent
float monitorOil(float, float);
32
OilSupply.cpp
int main()
{
float supply; // input - initial oil supply
float oilLeft; // output - oil left in tank
float minOil; // minimum oil supply
// Get the initial oil supply.
cout << "Enter initial oil supply: ";
cin >> supply;
33
OilSupply.cpp
// Compute the minimum oil supply.
minOil = CAPACITY * (MINPCT / 100.0);
// Compute and display the amount of oil
// left each day.
oilLeft = monitorOil(supply, minOil);
// Display a warning message if supply is less
// than minimum
cout << endl << oilLeft <<
" gallons left in tank." << endl;
34
OilSupply.cpp
if (oilLeft < minOil)
cout <<
"Warning - amount of oil left is below minimum!"
<< endl;
return 0;
}
35
OilSupply.cpp
float monitorOil(float supply, float minOil)
{
// Local data . . .
float usage;
// input from user - Each day's oil use
float oilLeft; // Amount left each day
oilLeft = supply;
while (oilLeft > minOil) {
cout << "Enter amount used today: ";
cin >> usage;
oilLeft -= usage;
36
OilSupply.cpp
cout << "After removal of " << usage << " gallons,"
<< endl;
cout << "number of gallons left is " << oilLeft
<< endl << endl;
} // end while
return oilLeft;
}

More general conditional loops
while(boolean expression)
Example: while(oilLeft > minOil && numDazys < 14)
37
5.5 Loop Design and Loop
Patterns

Sentinel-Controlled Loops
1. Read the first data item.
2. while the sentinel value has not been read
3. Process the data item.
4. Read the next data item.

Flag Controlled Loops
1. Set the flag to false.
2. while the flag is false
3. Perform some action.
4. Reset the flag to true if the anticipated event occurred.
38
Sentinel Loops
– A sentinel is a specific predetermined value
used to terminate one type of event-controlled
loop.
– It is the same type as the other extracted data
(but not possibly valid data).
– For example, the int constant -1 may act as a
sentinel when all valid int data > -1
39
SumScores.cpp
// File: SumScores.cpp
// Accumulates the sum of exam scores.
#include <iostream>
using namespace std;
void displayGrade(int);
int main()
{
40
SumScores.cpp
const int SENTINEL = -1;
int score;
int sum;
int count;
int average;
// Process all exam scores until sentinel is read
count = 0;
sum = 0;
cout << "Enter scores one at a time as
requested." << endl;
cout << "When done, enter " << SENTINEL <<
" to stop." << endl;
41
SumScores.cpp
cout << "Enter the first score: ";
cin >> score;
while (score != SENTINEL)
{
sum += score;
count++;
displayGrade(score);
cout << endl<< "Enter the next score: ";
cin >> score;
}
42
SumScores.cpp
cout << endl << endl;
cout << "Number of scores processed is " <<
count << endl;
cout << "Sum of exam scores is " << sum <<
endl;
// Compute and display average score.
if (count > 0) {
average = sum / count;
cout << "Average score is " << average;
}
return 0;
}
43
SumScores.cpp
// Insert function displayGrade here.
void displayGrade(int score)
{
// Insert function displayGrade here.
}
44
SumScores.cpp
Program Output
Enter the scores one at a time as requested.
When done, enter -1 to stop.
Enter the first score: 55
Enter the next score: 33
Enter the next score: 77
Enter the next score: -1
Sum of exam scores is 165
3 exam scores were processed.
Average of the exam scores is 55.0
45
Flag-Controlled Loops
char getNextDigit()
{
char nextChar;
bool digitRead;
digitRead = false;
while (!digitRead)
{
cin >> nextChar;
digitRead = (('0'<= nextChar) && (nextChar <= '9'));
}
return nextChar;
}
46
5.6 The do-while Statement
– The do while statement is similar to the while
loop, but the do while loop has the test at the
end. General form:
do {
iterative-part
} while ( loop-test ) ;
– Notice the iterative part executes BEFORE the
loop-test)
47
When to use the do-while
loop
– The do while loop is a good choice for
obtaining interactive input from menu
selections.
– Consider a function that won't stop executing
until the user enters an N, O, or S:
– Post test loop
48
Example do-while loop
char menuOption()
{ // POST: Return an upper case 'N', 'O' or 'S'
char option;
do {
cout << "Enter N)ew, O)pen, S)ave: ";
cin >> option;
option = toupper(option); // from ctype.h
} while (option != 'N' && option != 'O' && option != 'S');
return option;
}
49
Largest.cpp
// FILE: Largest.cpp
// FINDS THE LARGEST NUMBER IN A SEQUENCE OF
// INTEGER VALUES
#include <iostream>
#include <limits>
// needed for cin and cout
// needed for INT_MIN
using namespace std;
int main ()
{
50
Largest.cpp
// Local data ...
int itemValue;
int largestSoFar;
int minValue;
// each data value
// largest value so far
// the smallest integer
// Initialize largestSoFar to the smallest
// integer.
minValue = INT_MIN;
largestSoFar = minValue;
51
Largest.cpp
// Save the largest number encountered so far.
cout << "Finding the largest value in a
sequence: " << endl;
do
{
cout << "Enter an integer or " << minValue << " to
stop: ";
cin >> itemValue;
if (itemValue > largestSoFar)
largestSoFar = itemValue;
} while (itemValue != minValue);
52
Largest.cpp
cout << "The largest value entered was " <<
largestSoFar << endl;
return 0;
}
Program Output
Finding the largest value in a sequence:
Enter an integer or -32768 to stop: -999
Enter an integer or -32768 to stop: 500
Enter an integer or -32768 to stop: 100
Enter an integer or -32768 to stop: -32768
The largest value entered was 500
53
5.7 Review of while, for, and
do-while Loops


while - Most commonly used when repetition is
not counter controlled; condition test precedes
each loop repetition; loop body may not be
executed at all
for - Counting loop - When number of repetitions
is known ahead of time and can be controlled by a
counter; also convenient for loops involving non
counting loop control with simple initialization
and updates; condition test precedes the execution.
54
Review of while, for, and dowhile Loops

do-while - Convenient when at least one
repetition of loop body must be ensured.
Post test condition after execution of body.
55
5.8 Nested Loops

Possible to nest loops
–
–
–
–

Loops inside other loops
Start with outer jump to inner loop
Inner loop continues until complete
Back to outer loop and start again
NestLoop.cpp and Triangle.cpp examples
56
Nested Logic
outer = 1;
while(outer <= 3) {
inner = 1; // Resets for each iteration of the outer loop
while(inner <= outer) {
cout << outer << " " << inner << endl;
inner = inner + 1;
} // End of the nested loop
outer = outer + 1; // Increment the outer loop counter
} // The end of the outer loop
57
NestedLoops.cpp
// FILE: NestedLoops.cpp
// ILLUSTRATES A PAIR OF NESTED FOR LOOPS
#include <iostream>
#include <iomanip>
int main ()
{
// display heading
cout << setw(12) << "i" << setw(6) << "j" <<
endl;
58
NestedLoops.cpp
for (int i = 0; i < 4; i++)
{
cout << "Outer" << setw (7) << i << endl;
for (int j = 0; j < i; j++)
cout << " Inner" << setw (10) <<
j << endl;
}
return 0;
}
59
NestedLoops.cpp
Program Output
Outer
Outer
Inner
Outer
Inner
Inner
Outer
Inner
Inner
Inner
i
0
1
j
0
2
0
1
3
0
1
2
60
Displaying the Multiplication
Table

Example:
0 1 2 3 4
…
-------------------------------0 0 0 0
0 0
1 0 1 2
3 4
2 0 2 4 6 8
3 0 3 6
9 12

Sample code:
cout << “ 0 1 2
3
4
5
6
7
8 9”<< endl;
cout << “-------------------------------------------------------------”<<endl;
for (int row = 0; row < 10; row++)
cout << setw(3) << row << “ “;
for (int col = 0; col < 10; col++)
cout << setw(3) << row*col;
cout << endl;
61
Further General Features of
Loops





You can call functions within loops
You can use the break statement to enforce exiting any loop (even for loops)
Called functions may also contain loops (indirect nesting)
for (int i = 0; i < 10; i++) // this loop calls the function f() 10 times
f();
You can use the break statement to enforce exiting any loop (even for loops)
Counting loops (like for loops) can be in general converted into a while loop
Example above as while loop:
int i = 0;
while(i < 10) {
f();
i++;
}
62
Further General Features of
Loops

In C++ for-loops are as powerful as while-loops, any while-loop can be
converted to a for-loop (other languages like Pascal use only counting loops
for for-loops)
Example:
while(true)

for(;;)
doSomething;
doSomething;

Do-while loops can be converted to while loops and vice versa.
Example:
cout << “Enter number ”;
cin >> number;
while(number < 10) {
cout << “Enter number ”;
cin >> number;
}

do {
cout << “Enter number ”;
cin >> number;
} while(number < 10)
63
While and switch statements
counter = 10;
while(counter > 0) {
cout << “Write a letter “;
cin > ch;
switch(ch) {
case ‘A’: case ‘a’:
cout << “Input was A or a” << endl;
break;
…
case ‘Z’: case ‘z’:
cout << “Input was Z or z” << endl;;
break;
}
counter--;
}
64
Break and continue in loops

As we know:
Break means to quit the loop immediately

The continue statement has an opposite character:
Continue means go to the test condition immediately

Example:
int i = 0;
while(i < 5) {
if(I == 3)
continue;
cout << “Now the number is “ << I << endl;
}
Output:
Now the number is 0
Now the number is 1
Now the number is 2
Now the number is 4
65
Break and continue in loops




Break and continue statements are often used in conjunction with if
statements in order to exit the loop (break) or just abort one iteration of the
loop (continue).
Frequent use of break/continue makes programs less readable and therefore
less maintainable.
A good programming style is not to use them at all, or to use them only in
special situations
Example: What does this short program segment????????????
for(int i = 0; i < 4; i++) {
for(int j = 0; j <4; j++) {
if(i==j) break;
cout << “Inner: “<< i << “ “<<j<<endl;
}
if(i == j) continue;
cout << “Outer: “<< i << “ “<<j<<endl; // this statement will never be executed
}
66
5.9 Debugging and Testing
Programs


Modern Integrated Development
Environments (IDEs) include features to
help you debug a program while it is
executing.
If you cannot use a debugger, insert extra
diagnostic output statements to display
intermediate results at critical points in your
program.
67
5.10 Common Programming
Errors
–
–
–
–
–
–
Coding style and use of braces.
Infinite loops will “hang you up !!”
Use lots of comments before and after a loop.
Test various conditions of loops.
Add white space between code segments using loops.
Initialize looping variables or use internal loop control
variables (lcv) in the for loop.
– Off-by-1-Errors
• for(int i = 0; i < N; i++)
• for(int i = 0; i <= N; i++)


01 2
01 2
… N-1
… N-1, N
68
Download