Presentation is loading. Please wait.

Presentation is loading. Please wait.

01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.

Similar presentations


Presentation on theme: "01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while."— Presentation transcript:

1 01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while

2 01/05/100 2 1 check the test 2 if the test is true –exec the body –when the body has finished go to step 1 if the test is false –exit the loop int n = ?; // try n as 6 while (n >= 0) { n -= 2; cout << n << endl; } cout << “final n is “ << n << endl; Anatomy of a while loop test ? loop body stmt following loop stmt before loop The test is always a “keep going” condition. To determine the termination condition, negate the test. I.e. the loop will keep going as long as n >= 0 the loop will terminate when n becomes negative (n < 0)

3 01/05/100 3 while Loops The test is checked at the very beginning and then again each time after the after the entire loop body has been executed The test is NOT checked in the middle of the loop body This is true for all the loops (for, while, and do/while), not just the while loop x = ?; // try x as 45 while (x < 50) { x++; cout << x << endl; x++; cout << x << endl; }

4 01/05/100 4 Practice what’s the output? int a = 20, b = 50; while (a < b) { if (a % 5 == 0) { cout << “s”; } else if (a % 3 == 0) { cout << “e”; } else { cout << “w”; } a += 4; } cout << “\n”; cout << “a’s final value is “ << a << endl;

5 01/05/100 5 Practice What’s the output? int d = 90; while (d < 80) { d ++; } cout << “d is “ << d << endl; int x = 90; while (x < 100) { x -= 5; } cout << “final value for x is “ << x << endl;

6 01/05/100 6 Summing (even) numbers with a while loop Example of an indeterminate loop - the user’s input will determine how many times the loop executes int sum = 0, evensum = 0, number; cout << “ First number please “; cin >> number; while (number > 0) { sum += number; if (number % 2 == 0) { evensum += number; } cout << "number please "; cin >> number; } cout << "sum is " << sum << endl; cout << “sum of even #’s is “ << evensum << endl;

7 01/05/100 7 Error checking with a while loop if the number is out of range, enter the while loop otherwise, skip the loop reenter loop when the user’s number continues to be out of range const int cMax = 10, cMin = 5; cout << "Pls enter a # between " << cMax << " and " << cMin << " please " << endl; cin >> number; while (number cMax) { cout << "error in input \n"; cout << "Pls enter a # between " << cMax << " and " << cMin << " please " << endl; cin >> number; } cout << “The user entered “ << number << endl; test ? loop body stmt following loop stmt before loop

8 Block/Scope { } define a scope void main() { int n = 9; // this n belongs to main { int n = 56; // this n belongs to this block nested in main cout << n << endl; // use the inner most n } cout << n << endl; // don’t know about inner n, use n from main } this is not a good idea, having nested blocks that reuse the same variable name

9 Scope/Blocks identifiers declared in a nested block cannot be referred to outside the block but identifiers in an outer block are accessible in a nested block void main() { int a = 5; { int x = 9; cout << “inside inner block\n”; cout << x << “ “ << a << endl; } // ILLEGAL, x is not visible in this block cout << x << “ “ << a << endl;} }

10 Where you really use blocks You won’t use free-standing nested or sequential blocks demo’ed in the last slides, but you do use blocks in constructs that have a pair of { } void main() { int sum = 0; while (sum < 80) { int response; cout << “Please enter a number\n”; cin >> response; sum += response; } cout << response << endl; // ILLEGAL, response is declared inside of the while loop block and cannot be used outside of the while loop cout << sum << endl; // sum can be used both in and out of the while loop }


Download ppt "01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while."

Similar presentations


Ads by Google