1 / 22

More General Task-Controlled Loops Chapter 8 CSIS 10A

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

glenng
Download Presentation

More General Task-Controlled Loops Chapter 8 CSIS 10A

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More General Task-Controlled LoopsChapter 8 CSIS 10A

  2. Agenda • Examples and Guidelines  • Trapping Input Errors • Multiple Reasons for Loop Exit • Nested Loops

  3. 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)

  4. 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

  5. Trace the last program here (instead of 1000, use 10)

  6. 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

  7. 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!)

  8. Agenda • Examples and Guidelines • Trapping Input Errors  • Multiple Reasons for Loop Exit • Nested Loops

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

  10. 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!

  11. 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!

  12. 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

  13. 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";

  14. Agenda • Examples and Guidelines • Trapping Input Errors • Multiple Reasons for Loop Exit  • Nested Loops

  15. 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;

  16. Agenda • Examples and Guidelines • Trapping Input Errors • Multiple Reasons for Loop Exit • Nested Loops 

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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);

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

More Related