Upload
chad-horn
View
215
Download
0
Embed Size (px)
Citation preview
Overview
• Go over parts of quiz?
• Another iteration structure for loop
for loopfor (expression1; expression2; expression3) statement;
Step 1: Evaluate expression1Step 2: Evaluate expression2.
If true, execute statementIf 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 10for (int i = 1; i <= 10; i++)
cout << i << endl;
for loop with compound statementfor (expression1; expression2; expression3) { statement1; statement2; }
Example: Input 10 numbers and calculate the totalint 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 followingproblems:
1) Display the numbers from 7 to 77 in steps of 72) Display the numbers from 20 to 2 in steps of –23) Display the following numbers 2,5,8,11,14,17,204) Display the following numbers 99,88,77,66,55,
44,33,22,11,0
control variable for the for loop
for (i = 1; i <= 10; i++) cout << i << endl;
i is the control variable for the for loop in this example
Initialize control variableFinal value of control variable
Update control variable
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; //Initialize loop control variable while (expression2) { //Test loop control variable statement; expression3; // 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 loopUse 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 expression1for (; 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 10int i = 1;for (; i <= 10; i++)
cout << i << endl;
Omitting expression2for (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 beyondfor (int i = 1; ; i++)
cout << i << endl;
Omitting expression3for (expression1; expression2;) statement;
When expression3 is omitted, this loop also becomes an infinite loop unless the loop control variable is updated insidethe loop
Example: Display the numbers from 1 to infinity and beyondfor (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.