1 / 15

ITERATION

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

Download Presentation

ITERATION

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

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

  3. TOP TESTED LOOP (WHILE) • SIMPLE • while (RELATIONAL EXPRESSION(s)) statement; • COMPOUND • while (RELATIONAL EXPRESSION(s)) { statement; “ “ statement(s); }

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

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

  6. 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”); }

  7. 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”); }

  8. FIXED ITERATION(FOR) • SIMPLE • for (initialization; condition; update) statement; • COMPOUND • for (initialization; condition; update) { statement; statement: statement(s); }

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

  10. 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”); }

  11. 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”); }

  12. BOTTOM TESTED LOOP (DO WHILE) • SIMPLE do statement; while (RELATIONAL EXPRESSION(s)); • COMPOUND do { statement; “ “ statement(s); } while (RELATIONAL EXPRESSION(s));

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

  14. 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”); }

  15. 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”); }

More Related