Top-down design. Stepwise Refinement
Download
Report
Transcript Top-down design. Stepwise Refinement
Overview
• Go over parts of quiz?
• Another iteration structure
for loop
for loop
for (expression1; expression2; expression3)
statement;
Step 1: Evaluate expression1
Step 2: Evaluate expression2.
If true, execute statement
If false, continue execution at next statement
following the for loop.
Step3: Evaluate expression3
Step 4: Repeat Step 2 and 3
Example: Display the numbers from 1 to 10
for (int i = 1; i <= 10; i++)
cout << i << endl;
for loop with compound statement
for (expression1; expression2; expression3) {
statement1;
statement2;
}
Example:
Input 10 numbers and calculate the total
int i, number, total = 0;
for (i = 1; i <= 10; i++) {
cout << “\nEnter next number: “;
cin >> number;
total = total + number;
}
cout << “The total of the numbers entered is “ << total
<< endl;
Exercises
Use a for loop structure to solve the following
problems:
1) Display the numbers from 7 to 77 in steps of 7
2) Display the numbers from 20 to 2 in steps of –2
3) Display the following numbers 2,5,8,11,14,17,20
4) Display the following numbers 99,88,77,66,55,
44,33,22,11,0
control variable for the for loop
i is the control variable for the for loop in this example
Initialize control variable
Final value of control variable
Update control variable
for (i = 1; i <= 10; i++)
cout << i << endl;
Declaration of loop control
variable inside for structure
for (int i = 1; i <= 10; i++)
cout << i << endl;
instead of
int i;
for (i = 1; i <= 10; i++)
cout << i << endl;
for loops
for (expression1; expression2; expression3)
statement;
is equivalent to:
expression1;
while (expression2) {
statement;
expression3;
}
//Initialize loop control variable
//Test loop control variable
// Update loop control variable
Example: for loop vs while loop
Display the numbers from 1 to 10
for (int i = 1; i <= 10; i++)
cout << i << endl;
OR
int i = 1;
while (i <= 10) {
cout << i << endl;
i++;
}
Example: change while loop to a
for loop
int sum = 0;
int count = -5;
while (count <= 15) {
sum = sum + count;
count ++;
}
int sum = 0;
for (int count = -5; count <= 15; count++)
sum = sum + count;
When to use for loop
Use the for loop for a counter controlled repetition
Use the while loop for repetition when there is no counter.
(i.e reading in input).
for (int i = 1; i <= 10; i++)
cout << i << endl;
bool notDone = true;
while (notDone) {
cin << input;
if (-1 == input)
notDone = false;
}
Omitting expression1
for (; expression2; expression3)
statement;
Can omit expression 1 if loop control variable is initialized
some place else. Note ; place holder
Example: Display the numbers from 1 to 10
int i = 1;
for (; i <= 10; i++)
cout << i << endl;
Omitting expression2
for (expression1; ; expression3)
statement;
When expression2 is omitted, C++ assumes the condition
is always true, thereby creating an infinite loop
Example: Display the numbers from 1 to infinity and beyond
for (int i = 1; ; i++)
cout << i << endl;
Omitting expression3
for (expression1; expression2;)
statement;
When expression3 is omitted, this loop also becomes an
infinite loop unless the loop control variable is updated inside
the loop
Example: Display the numbers from 1 to infinity and beyond
for (int i = 1; i <= 10;)
cout << i << endl;
comma separated lists of
expressions
Expression1 and Expression3 may be a comma separated list of
expressions.
For example:
int inputData;
for (int i = 0, sum = 0; (i < 10) && (sum < 1000); i++) {
cin >> inputData;
sum = sum + inputData;
}
Note: Put only expressions involving the control variables in the
initialization and increment expressions of the for structure.
common programming error
Changing the loop control variable inside the body of the loop is
allowed but dangerous.
for (int i = 0; i < 10; i++) {
cin >> i;
}
common programming error
Problem: Vary the control variable over the following
sequence of values 2, 5, 8, 11, 14, 17, 20
Solution1: for (int i = 2; i <= 20; i +=3)
Solution2: for (int i = 2; i < 22; i +=3)
Solution2 more confusing. More likely to lead to boundary
condition problems.
Exercises
1) Write a program that inputs 10 grades,
determines the highest grade and displays
the result. Use a for loop.
2) Write the same program as in problem
number 1 using a while loop.