// ------------------------------------------------------------ // for.cpp for while do loops and break // ------------------------------------------------------------ #include // Stream library void nl(int k = 1) { for (int j = 0; j < k; j++) cout << "\n"; } // ------------------------------------------------- void main () { int i,k; for (i = 0; i < 10; i++) k = i; cout << "i = " << i << " k = " << k; nl(2); i = 0; while (i < 5) {cout << i << " "; i++; } nl(); i = 0; while (i < 5) {cout << i << " "; i++; if (i == 3) break; } cout << "\n"; i = 0; do { cout << i << " "; if (i == 3) { cout << "wow"; break; } i++; } while (i < 5); nl(); for (int j = 1; j < 10; j *= 2) cout << j << " "; // j global nl(); cout << " j = " << j; // int j = 3; // WRONG j already declared in loop } // --------------------------------------------------------- /* output i = 10 k = 9 0 1 2 3 4 0 1 2 0 1 2 3 wow 1 2 4 8 j = 16 */