More General TaskControlled Loops Chapter 8 CSIS 10A 1 Agenda Examples and Guidelines Trapping Input Errors Multiple Reasons for Loop Exit Nested Loops 2 Task Controlled Loop When a loop exits on a condition determined by a variable used in the loop You don’t know when it will stop (not step controlled) The user input does not necessarily determine when it will stop either (not user-controlled) 3 Finding the first square to put sum over 1000 int n=0, sum=0; Similar to Problem 1 while (sum <= 1000) { n++; sum = sum + n*n; } cout << "Sum first goes over 1000 when " << n << " squared is added.\n"; cout << "Sum is " << sum << ".\n"; 4 Trace the last program here (instead of 1000, use 10) n sum (sum <10) Output 5 Guidelines in developing solutions Understand the problem Develop the loop algorithm What variables? What exit test? What needs to be done Before/After loop? How to generate next value Pseudocode Code and Test 6 Some Problem Statements If $500 is invested at 4.5% compounded annually, after how many years will the balance first exceed $1000 ? (Problem 2) Find what value of n makes the series sum = 1 + 1/2 + 1/3 + … 1/n exceed the value 2…and what is the sum? (let’s develop some solutions!) 7 Agenda Examples and Guidelines Trapping Input Errors Multiple Reasons for Loop Exit Nested Loops 8 How can this program go wrong? // letter1.cpp char ch; cout << "Enter capital letter: "; cin >> ch; if (ch >= 'A' && ch <= 'M') cout << ch << " in first half of alphabet.\n"; else cout << ch << " in second half of alphabet.\n"; return 0; } 9 Many times we have to protect our code from bad data cout<<“Enter value between 0 and 100”<<endl; cin>>num; if (num<0 || num > 100) cout<<“Bad data”; else It would be nice to not go on until { Good data is entered! //process data } 10 Soln1: Use a trap using while cout<<“Enter value between 0 and 100”<<endl; cin>>num; Use For Problem 3 while (num<0 || num > 100) { cout<<“Bad data, try again”; cin>>num; We can’t go on until } Good data is entered! //process data 11 Soln2: Use a trap using do-while do { cout<<“Enter value between 0 and 100”<<endl; cin>>num; Or Use This For } Problem 3 while (num<0 || num > 100); //process data 12 Letter1.cpp Fixed in letter2.cpp char ch; do { cout << "Enter capital letter: "; cin >> ch; } while (ch<'A' || ch>'Z'); if (ch >= 'A' && ch <= 'M') cout << ch << " in first half of alphabet.\n"; else cout << ch << " in second half of alphabet.\n"; 13 Agenda Examples and Guidelines Trapping Input Errors Multiple Reasons for Loop Exit Nested Loops 14 Can use complex booleans in loops int dots, // on top face of die rolls; // rolls so far See rolls.cpp srand(time(0)); // init r.n.g. rolls = 0; do { dots = rand()%6 + 1; // random # between 1 and 6 rolls++; cout << dots << ' '; } while (dots!=1 && dots != 6); if (dots == 1) cout << "You lose $"; else // dots == 6 cout << "You win $"; cout << rolls << endl; 15 Agenda Examples and Guidelines Trapping Input Errors Multiple Reasons for Loop Exit Nested Loops 16 Nesting loops A loop inside another becomes a nested loop void main() { for(int x = 1; { cout <<“Now for(int y = cout }//end of for } x <= 10; x=x+1) x is ” << x << endl; 1; y <= 5; y=y+1) << y << endl; //end of for Trace the output of this program 17 male_sum = 0; fem_sum = 0; do { cout << "Enter number of points: "; cin >> points; do { cout << "Enter sex (m or f): "; cin >> sex; What is output when sex = toupper(sex); the following is input: } while (sex!='M' && sex!='F'); 5 f y 10 m y 6 f n if (sex=='F') fem_sum += points; else male_sum += points; cout << "Continue? (y or n): "; cin >> ans; } while (toupper(ans) != 'N'); cout << "Female sum = " << fem_sum << endl; cout << "Male sum = " << male_sum << endl; Other loops can be nested (points.cpp) 18 You should be able to trace a nested loop (see problem 4) for (int k=1; k<=4; k++) { for (m=k; m>0; m--) cout<<“*”; cout<<endl; } k m (k<=4) (m>0) Output For loop that repeats k times 19 Converting an inner loop to a function call (Problem 8) for (int k=1; k<=4; k++) { // draw a line of k stars cout<<endl; } This will be a function call To a new void function you will create called line 20 Using line( ) line(‘x’, 4); // displays xxxx line(‘y’, 2); // displays yy What does this display? line(‘z’, 8); What does this display? int k=3; line(‘a’, k); 21 Defining line( ) Void function because it doesn’t return a value 1st parameter: char theSymbol // what to display 2nd parameter: int numTimes // how many copies So we have void line(char theSymbol, int numTimes) { // for loop that repeats numTimes cout<<theSymbol; } 22