180 likes | 314 Views
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 ;
E N D
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; }
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; }
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 }
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";
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;
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;
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;
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; }
Nested Loops! Result? for (i = 1; i <= 5; i++) for (j = 1; j <= 3; j++) cout << endl << i << setw(5) << j;
Nested Loops! Result? for (i = 1; i <= 5; i++) for (j = 1; j <= 3; j++) cout << "*";
Nested Loops! Result? for (i = 1; i <= 5; i++) for (j = 1; j <= 3; j++) cout << endl << "*";
Nested Loops! Result? for (i = 1; i <= 5; i++) { cout << endl; for (j = 1; j <= 3; j++) cout << "*"; }
Nested Loops! Result? for (i = 1; i <= 5; i++) { cout << "\nrow " << i << " : "; for (j = 1; j <= 3; j++) cout << "*"; }
Nested Loops! Result? for (i = 1; i <= 5; i++) { cout << "\nrow " << i << " : "; for (j = 1; j <= i; j++) cout << "*"; }
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.
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