17
Overview • Go over parts of quiz? • Another iteration structure for loop

Overview Go over parts of quiz? Another iteration structure for loop

Embed Size (px)

Citation preview

Page 1: Overview Go over parts of quiz? Another iteration structure for loop

Overview

• Go over parts of quiz?

• Another iteration structure for loop

Page 2: 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;

Page 3: Overview Go over parts of quiz? Another iteration structure for loop

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;

Page 4: Overview Go over parts of quiz? Another iteration structure for loop

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

Page 5: Overview Go over parts of quiz? Another iteration structure for loop

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

Page 6: Overview Go over parts of quiz? Another iteration structure for loop

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;

Page 7: Overview Go over parts of quiz? Another iteration structure for loop

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 }

Page 8: Overview Go over parts of quiz? Another iteration structure for loop

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++;}

Page 9: Overview Go over parts of quiz? Another iteration structure for loop

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;

Page 10: Overview Go over parts of quiz? Another iteration structure for loop

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;}

Page 11: Overview Go over parts of quiz? Another iteration structure for loop

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;

Page 12: Overview Go over parts of quiz? Another iteration structure for loop

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;

Page 13: Overview Go over parts of quiz? Another iteration structure for loop

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;

Page 14: Overview Go over parts of quiz? Another iteration structure for loop

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.

Page 15: Overview Go over parts of quiz? Another iteration structure for loop

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; }

Page 16: Overview Go over parts of quiz? Another iteration structure for loop

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.

Page 17: Overview Go over parts of quiz? Another iteration structure for loop

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.