980 likes | 1.09k Views
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.
E N D
for Syntax for (expression1; expression2; expression3) statement
for Syntax for (expression1; expression2; expression3) statement
for Syntax for (expression1; expression2; expression3) statement
for Syntax for (expression1; expression2; expression3) statement
for Syntax for (expression1; expression2; expression3) statement
for Syntax for (expression1; expression2; expression3) statement
Bad Form cout << “What is 2+2? ”;cin >> sum;for (; sum != 4;){ cout << “Incorrect answer. What is 2+2? ” << endl; cin >> sum; }
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;
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;
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;
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;
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;
Goal Output * * * * * * * * ** * ** * *
Outputting 5 Lines for (inti = 1; i <= 5; i++) {cout<< endl;}
Outputting 5 Stars for (inti = 1; i <= 5; i++) {cout<< “* ”;}
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values i=1 j=?
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values i=1 j=1
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * i=1 j=1
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * i=1 j=2
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * i=1 j=2
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * i=1 j=3
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * i=1 j=3
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * i=1 j=4
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * i=1 j=4
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * i=1 j=5
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * i=1 j=5
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * i=1 j=6
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * i=1 j=6
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * i=2 j=?
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * i=2 j=1
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * i=2 j=1
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * i=2 j=2
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * * i=2 j=2
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * * i=2 j=3
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * * * i=2 j=3
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * * * i=2 j=4
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * * * * i=2 j=4
Combining for (inti = 1; i <= 5; i++){ for (int j = 1; j <= 5; j++) cout << "* "; cout << endl;} output variable values * * * * * * * * * i=2 j=5
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!!!
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
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=?
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
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
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
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
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
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
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
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