150 likes | 290 Views
ITERATION. ITERATION. FIXED ITERATION KNOWN FIXED QUANTITY OF TIMES FOR TOP TESTED ENTER LOOP ONLY IF CONDITION IS TRUE WHILE LOOP MAY NOT BE EXECUTED BOTTOM TESTED CONTINUE LOOP UNTIL CONDITION IS TRUE DO- WHILE LOOP IS ALWAYS EXECUTED ONCE. TOP TESTED LOOP (WHILE). SIMPLE
E N D
ITERATION • FIXED ITERATION • KNOWN FIXED QUANTITY OF TIMES • FOR • TOP TESTED • ENTER LOOP ONLY IF CONDITION IS TRUE • WHILE • LOOP MAY NOT BE EXECUTED • BOTTOM TESTED • CONTINUE LOOP UNTIL CONDITION IS TRUE • DO- WHILE • LOOP IS ALWAYS EXECUTED ONCE
TOP TESTED LOOP (WHILE) • SIMPLE • while (RELATIONAL EXPRESSION(s)) statement; • COMPOUND • while (RELATIONAL EXPRESSION(s)) { statement; “ “ statement(s); }
TOP TESTED LOOP (WHILE) • RULES 1. STATEMENT(s) IS/ARE EXECUTED IF AND ONLY IF THE RELATIONAL EXPRESSION(s) EVALUATE TRUE. 2. STATEMENT(s) IS/ARE EXECUTED FOLLOWING EACH TRUE EVALUATION OF THE RELATIONAL EXPRESSION(s). 3. ONE OF THE LOOP’S STATEMENTS MUST SET UP A CONDITION TO EXIT THE LOOP OR IT WILL BE AN INFINITE LOOP.
TOP TESTED LOOP (WHILE) • TYPICAL VARIATIONS • COUNTER CONTROLLED • DESIGNED TO RUN A FIXED NUMBER OF TIMES • SENTINEL CONTROLLED • SENTINEL VALUE CONTROLLED • READ FIRST INPUT & TEST • VALID EXECUTE LOOP • INVALID (MATCHES SENTINEL) DO NOT EXECUTE LOOP • CONTINUE UNTIL INPUT MATCHES SENTINEL • FLAG CONTROLLED • BOOLEAN FLAG CONTROLLED • FLAG IS INITIALIZED TO FALSE • LOOP EXECUTES UNTIL FLAG IS TRUE • FLAG IS SET TO TRUE BY STATEMENTS IN BODY OF WHILE • EOF CONTROLLED • CONTROLLED BY RESULT OF EOF FUNCTION • MORE TO COME IN CS362
TOP TESTED LOOP (WHILE) • EXAMPLES int main( ) { char answer; float score, total, avg; int count; count = 0; total = 0; answer = ‘Y’; while (answer == ‘Y’ || answer == ‘y’) { cout << “Enter the students’s score.” << endl; cin >> score; total = total + score; count ++; cout << “Is there another score to enter? Y/N” << endl; cin >> answer; } avg = total / count; cout << fixed << setprecision(2); cout << “The class average is ‘ << avg << endl; cout << endl; system(“Pause”); }
TOP TESTED LOOP (WHILE) • EXAMPLES int main ( ) { char answer; float score, total, avg; int quantity; cout << “Enter the number of students in the class.” << endl; cin >> quantity; count = 1; total = 0; while (count <= quantity) { cout << “Enter the students score.” << endl; cin >> score; total = total + score; count ++; } avg = total / quantity; cout << fixed << setprecision(2); cout << “The class average is “ << avg << endl; cout << endl; system(“Pause”); }
FIXED ITERATION(FOR) • SIMPLE • for (initialization; condition; update) statement; • COMPOUND • for (initialization; condition; update) { statement; statement: statement(s); }
FIXED ITERATION (FOR) • RULES 1. INITIALIZATION STATEMENT EXECUTES 2. LOOP CONDITION IS EVALUATED a) IF TRUE STATEMENT(S) IS/ARE EXECUTED b) IF FALSE LOOP IS EXITED 3. UPDATE STATEMENT IS EXECUTED
FIXED ITERATION (FOR) • EXAMPLES const int upper = 25; const int percent = 100; int index, quantity, lower; float score, total, avg; int main( ) { total = 0; score = 0; for (lower = 1; lower <= upper; lower ++) { cout << “ENTER THE STUDENTS SCORE” << endl; cin >> score; total = total + score; } avg = (total / upper) * percent; cout << fixed << setprecision(2); cout << “THE CLASS AVERAGE IS “ << avg << endl; cout << system(“Pause”); }
FIXED ITERATION (FOR) • EXAMPLES const int percent = 100; int index, quantity, lower, upper; float score, total, avg; int main( ) { total = 0; score = 0; upper = 0; cout << “HOW MANY STUDENTS ARE IN THE CLASS?” << endl; cin >> upper; for (lower = 1; lower <= upper; lower ++) { cout << “ENTER THE STUDENTS SCORE” << endl; cin >> score; total = total + score; } avg = (total / upper) * percent; cout << fixed << setprecision(2); cout << “THE CLASS AVERAGE IS “ << avg << endl; cout << system(“Pause”); }
BOTTOM TESTED LOOP (DO WHILE) • SIMPLE do statement; while (RELATIONAL EXPRESSION(s)); • COMPOUND do { statement; “ “ statement(s); } while (RELATIONAL EXPRESSION(s));
BOTTOM TESTED LOOP (DO WHILE) • RULES 1. STATEMENT(s) IS/ARE ALWAYS EXECUTED AT LEAST ONCE. 2. STATEMENT(s) CONTINUE TO BE EXECUTED UNTIL THE RELATIONAL EXPRESSION(s) EVALUATES TO FALSE.
BOTTOM TESTED LOOP (DO WHILE) • EXAMPLES float score, total, avg; int quantity, count; char answer; Int main( ) { total = 0; count = 0; do { cout << “ENTER THE STUDENTS SCORE” << endl; cin >> score; total = total + SCORE; count = count + 1; cout << “IS THERE ANOTHER SCORE TO ENTER? Y/N” << endl; cin >> answer; } while (answer != ‘N’ && answer != ‘n’); avg = total / count; cout << fixed << setprecision(2); cout << “THE CLASS AVERAGE IS “ << avg << endl; cout << system(“Pause”); }
BOTTOM TESTED LOOP (REPEAT) • EXAMPLES float score, total, avg; int quantity, cnt; char answer; Int main( ) { quantity = 0; total = 0; cnt = 1; cout << “ENTER THE NUMBER OF STUDENTS: “ << endl; cin >> quantity; do { cout << “ENTER THE STUDENTS SCORE” << endl; cin >> score; total = total + score; cnt = cnt + 1; } while (cnt <= quantity); avg = total / quantity; cout << fixed << setprecision(2); cout << “THE CLASS AVERAGE IS “ << avg << endl; cout << system(“Pause”); }