1 / 17

While, Do-While, and For loops

While, Do-While, and For loops. Sentinel-controlled while (a pretest loop). Sentinel-controlled (y/n) do - while (a posttest loop). gradecount = 0; do { cout << "Enter a grade:"; cin >> grade; // do stuff with grades gradecount ++; cout << "More grades?"; cin >> ansrch ;

Download Presentation

While, Do-While, and For loops

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. While, Do-While, and For loops

  2. Sentinel-controlled while (a pretest loop) Sentinel-controlled (y/n) do - while (a posttest loop) gradecount = 0; do { cout << "Enter a grade:"; cin >> grade; // do stuff with grades gradecount++; cout << "More grades?"; cin >> ansrch; } while (ansrch == 'y' || ansrch == 'Y') gradecount = 0; cout << "Enter first grade:"; cin >> grade; while (grade != -1) { // do stuff with grades gradecount++; cout << "Enter next grade:"; cin >> grade; }

  3. Sentinel-controlled while Count-controlled while gradecount = 0; cout >> "Enter number of grades:" cin >> numgrades; while (gradecount < numgrades) { cout << "Enter a grade:"; cin >> grade; // do stuff with grades gradecount++; } gradecount = 0; cout << "Enter first grade:"; cin >> grade; while (grade != -1) { // do stuff with grades gradecount++; cout << "Enter next grade:"; cin >> grade; }

  4. Count-controlled while Equivalent for loop gradecount = 0; cout >> "Enter number of grades:" cin >> numgrades; while (gradecount < numgrades) { cout << "Enter a grade:"; cin >> grade; // do stuff with grades gradecount++; } cout >> "Enter number of grades:" cin >> numgrades; for (gradecount = 0; gradecount < numgrades; gradecount++) { cout << "Enter a grade:"; cin >> grade; // do stuff with grades }

  5. Are these for loops equivalent?(i.e. do they produce the same results?) for (i = 1; i <= 5; i++) cout << "\nhello"; for (i = 0; i < 5; i++) cout << "\nhello";

  6. Are these for loops equivalent?(i.e. do they produce the same results?) for (i = 1; i <= 5; i++) cout << endl << i; for (i = 0; i < 5; i++) cout << endl << i;

  7. What do these for loops do? for (i = 5; i > 0; i--) cout << endl << i; for (i = 0; i > 5; i++) cout << endl << i; for (i = 5; i < 50; i+=5) cout << endl << i; for (i = 5; i > 0; i++) cout << endl << i; for (i = 5; i < 50; i+=10) cout << endl << i;

  8. What do these code segments do? x = 0; for (i = 1; i <= 5; i++) x += i; cout << endl << x; x = 0; for (i = 1; i <= 5; i++) x *= i; cout << endl << x; x = 1; for (i = 1; i <= 5; i++) x *= i; cout << endl << x;

  9. And this? ticketcost = 8.50; maxsold = 20; cout << " Price Chart"; cout << "\n Num Tickets Total Cost"; for (i = 1; i <= maxsold ; i++) { totalprice = i * ticketcost; cout << endl << setw(5) << i << setw(12) << totalprice; }

  10. Nested Loops! Result? for (i = 1; i <= 5; i++) for (j = 1; j <= 3; j++) cout << endl << i << setw(5) << j;

  11. Nested Loops! Result? for (i = 1; i <= 5; i++) for (j = 1; j <= 3; j++) cout << "*";

  12. Nested Loops! Result? for (i = 1; i <= 5; i++) for (j = 1; j <= 3; j++) cout << endl << "*";

  13. Nested Loops! Result? for (i = 1; i <= 5; i++) { cout << endl; for (j = 1; j <= 3; j++) cout << "*"; }

  14. Nested Loops! Result? for (i = 1; i <= 5; i++) { cout << "\nrow " << i << " : "; for (j = 1; j <= 3; j++) cout << "*"; }

  15. Nested Loops! Result? for (i = 1; i <= 5; i++) { cout << "\nrow " << i << " : "; for (j = 1; j <= i; j++) cout << "*"; }

  16. Programming Assignment 4Due: Wednesday, October 2950 points Write a program to produce an N x N multiplication table as illustrated on the handout. The user should be prompted to enter the size of the table, N, which can be anywhere from 2 to 20, inclusive. For 5 bonus points, use a loop to validate the user input and force the user to keep entering N until the number is valid. For 5 more bonus points, make sure the title is centered no matter what the size of the table is. Turn in a hard copy of your code and a hard copy of several sample runs, as shown on the handout.

  17. Sections covered from Chapter 5Relevant Checkpoints / Review Questions • Sections • 5.1 (but not prefix vs postfix stuff), 5.2 – 5.8, 5.10, 5.11 • CheckPoints • 5.2, 5.3, 5.6 – 5.15 • Review Questions • 1 – 11, 14 – 27, 30 – 38, 42 – 45, 50 – 52, 56 – 61

More Related