// File name: forloops.cpp #include #include using namespace std; int main() { int i, j, k, sum; i = 3; cout << i; for (i = 1; i <= 10; ++i) cout << setw(3) << i; cout << setw(3) << i << endl; // 3 1 2 3 4 5 6 7 8 9 10 11 is output of above loop for (j = 3; j <= i; j = j + 2) // note j declared in for cout << setw(3) << j; cout << setw(3) << j << endl; // 3 5 7 9 11 13 is output of above loop for (k = 1; k == 10; ++k) cout << setw(3) << k; cout << setw(3) << k << endl; // 1 is output of above loop. k == 10 is false, loop not entered. sum = 0; for (i = 10; i >= 1; --i) { sum = sum + i; cout << setw(3) << i << setw(3) << sum; } cout << setw(3) << i << setw(3) << sum << endl; // 10 10 9 19 8 27 7 34 6 40 5 45 4 49 3 52 2 54 1 55 0 55 output for (i = 1; i <= 5; ++i) { for (int j = 1; j <= 5; ++j) cout << setw(3) << i << setw(3) << j; cout << endl; } cout << "j = " << j << endl; // 1 1 1 2 1 3 1 4 1 5 output of 1st pass // 2 1 2 2 2 3 2 4 2 5 output of 2nd pass // 3 1 3 2 3 3 3 4 3 5 output of 3rd pass // 4 1 4 2 4 3 4 4 4 5 output of 4th pass // 5 1 5 2 5 3 5 4 5 5 output of 5th pass // j = 13 for (i = 1; i <= 10; ++i); cout << setw(3) << i; cout << setw(3) << i << endl; // 11 11 is the output system("pause"); return 0; }