100 likes | 191 Views
Loops/Iteration. Used to repeat an action Must have a STOP condition Three flavors - for, while , do/while. 01/05/100. 1. stmt before loop. test ?. loop body. stmt following loop. Anatomy of a while loop. 1 check the test 2 if the test is true exec the body
E N D
Loops/Iteration • Used to repeat an action • Must have a STOP condition • Three flavors - for, while, do/while 01/05/100 1
stmt before loop test ? loop body stmt following loop Anatomy of a while loop • 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; 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) 01/05/100 2
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; } 01/05/100 3
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; 01/05/100 4
PracticeWhat’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; 01/05/100 5
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; 01/05/100 6
stmt before loop test ? loop body stmt following loop Error checking with a while loopif the number is out of range, enter the while loopotherwise, skip the loopreenter 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 < cMin || 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; 01/05/100 7
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
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;} }
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 }