1 / 97

Counting Loops

Counting Loops. for Syntax. for (expression1; expression2; expression3)     statement . for Syntax. for (expression1; expression2; expression3)     statement . for Syntax. for (expression1; expression2; expression3)     statement . for Syntax.

kedem
Download Presentation

Counting 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. Counting Loops

  2. for Syntax for (expression1; expression2; expression3)    statement

  3. for Syntax for (expression1; expression2; expression3)    statement

  4. for Syntax for (expression1; expression2; expression3)    statement

  5. for Syntax for (expression1; expression2; expression3)    statement

  6. for Syntax for (expression1; expression2; expression3)    statement

  7. for Syntax for (expression1; expression2; expression3)    statement

  8. Bad Form cout << “What is 2+2? ”;cin >> sum;for (; sum != 4;){ cout << “Incorrect answer. What is 2+2? ” << endl; cin >> sum; }

  9. Example #1 float average;long sum = 0;short i = 0;cout << “enter positive integer: ”;cin >> max;for(i=0; i <= max; i++)sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

  10. Example #1 float average;long sum = 0;short i = 0;cout << “enter positive integer: ”;cin >> max;for(i=0; i <= max; i++)  sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

  11. Example #1 float average;long sum = 0;short i = 0;cout << “enter positive integer: ”;cin >> max;for(i=0; i <= max; i++)  sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

  12. Example #1 float average;long sum = 0;short i = 0;cout << “enter positive integer: ”;cin >> max;for(i=0; i <= max; i++)  sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

  13. Example #2 float average;long sum = 0;short i = 0;cout << “enter positive integer: ”;cin >> max;for (i=0; i <= max; i+=2)sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

  14. Goal Output * * * * * * * * ** * ** * *

  15. Outputting 5 Lines for (inti = 1; i <= 5; i++) {cout<< endl;}

  16. Outputting 5 Stars for (inti = 1; i <= 5; i++) {cout<< “* ”;}

  17. Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values i=1 j=?

  18. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values i=1 j=1

  19. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * i=1 j=1

  20. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * i=1 j=2

  21. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * i=1 j=2

  22. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * i=1 j=3

  23. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * i=1 j=3

  24. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * i=1 j=4

  25. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * i=1 j=4

  26. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * i=1 j=5

  27. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * i=1 j=5

  28. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * i=1 j=6

  29. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * i=1 j=6

  30. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * i=2 j=?

  31. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * i=2 j=1

  32. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * i=2 j=1

  33. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * i=2 j=2

  34. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * * i=2 j=2

  35. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * * i=2 j=3

  36. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * * * i=2 j=3

  37. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * * * i=2 j=4

  38. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * * * * i=2 j=4

  39. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * * * * i=2 j=5

  40. Combining for (inti = 1; i <= 5; i++){    for (int j = 1; j <= 5; j++)  cout << "* ";                      cout << endl;} output variable values * * * * * * * * * * i=2 j=5 oops!!!

  41. Goal vs Actual Goal * * * * * * * * ** * ** * * Actual * * * * * * * * * ** * * * ** * * * ** * * * * i=1 j=5 i=2 j=4 i=3 j=3 i=4 i=5 j=2 j=1

  42. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values i=1 j=?

  43. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values i=1 j=1

  44. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values * i=1 j=1

  45. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values * i=1 j=2

  46. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values * * i=1 j=2

  47. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values * * i=1 j=3

  48. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values * * * i=1 j=3

  49. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values * * * i=1 j=4

  50. Fixed for (inti=1; i <= 5; i++){    for (int j=1; j <= 5 – i + 1; j++)  cout << "* ";                      cout << endl;} output variable values * * * * i=1 j=4

More Related