// File name: whiloop.cpp #include #include using namespace std; int main() { int i, sum; // i = 0; // while (i < 10) // cout << setw(3) << i; // the above is an endless loop, i never changes, condition always true i = 0; while (i < 10) cout << setw(3) << ++i; cout << setw(3) << i << endl; // 1 2 3 4 5 6 7 8 9 10 10 output from above loop i = sum = 0; // i and sum set to zero while (i < 10) { sum = sum + ++i; cout << setw(3) << i << setw(3) << sum; } cout << setw(3) << i << setw(3) << sum << endl; // 1 1 2 3 3 6 4 10 5 15 6 21 7 28 8 36 9 45 10 55 10 55 output i = 5; while (i > 0 && i < 10) // AND loop - both conditions must be true cout << setw(3) << ++i; cout << setw(3) << i << endl; // 6 7 8 9 10 10 output of the above loop i = sum = 0; while (++i < 4 || sum < 20) // OR loop - only one condition true cout << setw(3) << i << setw(3) << (sum = sum + i); cout << setw(3) << i << setw(3) << sum << endl; // 1 1 2 3 3 6 4 10 5 15 6 21 7 21 i = 5; do { ++i; cout << setw(3) << i; } while (i < 5); cout << setw(3) << i << endl; // 6 6 output of the above post-test loop i = 0; while (++i < 10); cout << setw(3) << i; cout << setw(3) << i << endl; // 10 10 output of the above loop - note semicolon on while statement system("pause"); return 0; }