220 likes | 240 Views
More General Task-Controlled Loops Chapter 8 CSIS 10A. Agenda. Examples and Guidelines Trapping Input Errors Multiple Reasons for Loop Exit Nested Loops. 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
E N D
Agenda • Examples and Guidelines • Trapping Input Errors • Multiple Reasons for Loop Exit • Nested Loops
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)
Finding the first square to put sum over 1000 int n=0, sum=0; 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"; Similar to Problem 1
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
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!)
Agenda • Examples and Guidelines • Trapping Input Errors • Multiple Reasons for Loop Exit • Nested Loops
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; }
Many times we have to protectour code from bad data cout<<“Enter value between 0 and 100”<<endl; cin>>num; if (num<0 || num > 100) cout<<“Bad data”; else { //process data } It would be nice to not go on until Good data is entered!
Soln1: Use a trap using while cout<<“Enter value between 0 and 100”<<endl; cin>>num; while (num<0 || num > 100) { cout<<“Bad data, try again”; cin>>num; } //process data Use For Problem 3 We can’t go on until Good data is entered!
Soln2: Use a trap using do-while do { cout<<“Enter value between 0 and 100”<<endl; cin>>num; } while (num<0 || num > 100); //process data Or Use This For Problem 3
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";
Agenda • Examples and Guidelines • Trapping Input Errors • Multiple Reasons for Loop Exit • Nested Loops
Can use complex booleans in loops See rolls.cpp int dots, // on top face of die rolls; // rolls so far 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;
Agenda • Examples and Guidelines • Trapping Input Errors • Multiple Reasons for Loop Exit • Nested Loops
Nesting loops • A loop inside another becomes a nested loop void main() { for(int x = 1; x <= 10; x=x+1) { cout <<“Now x is ” << x << endl; for(int y = 1; y <= 5; y=y+1) cout << y << endl; //end of for }//end of for } Trace the output of this program
Other loops can be nested (points.cpp) male_sum = 0; fem_sum = 0; do { cout << "Enter number of points: "; cin >> points; do { cout << "Enter sex (m or f): "; cin >> sex; sex = toupper(sex); } while (sex!='M' && sex!='F'); 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; What is output when the following is input: 5 f y 10 m y 6 f n
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; } For loop that repeats k times
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
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);
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; }