August 2001
Beginner
482 pages
10h 52m
English
It is possible that the body of a while loop will never execute. The while statement checks its condition before executing any of its statements, and if the condition evaluates false, the entire body of the while loop is skipped. Listing 6.5 illustrates this.
0: // Listing 6.5
1: // Demonstrates skipping the body of
2: // the while loop when the condition is false.
3: #include <iostream>
4:
5: int main()
6: {
7: int counter;
8: std::cout << "How many hellos?: ";
9: std::cin >> counter;
10: while (counter > 0)
11: {
12: std::cout << "Hello!\n";
13: counter--;
14: }
15: std::cout << "counter is OutPut: " << counter;
16: return 0;
17: }
|
How many hellos?: 2 Hello! Hello! counter ...
Read now
Unlock full access