Sams Teach Yourself C++ in 21 Days

(singke) #1

202 Day 7


Some programmers like to write:
#define EVER ;;
for (EVER)
{
// statements...
}

NOTE

A forever loop is a loop that does not have an exit condition. To exit the loop, a break
statement must be used. Forever loops are also known as eternal or infinite loops.

LISTING7.17 Demonstrating a Forever Loop
1: //Listing 7.17
2: //Using a forever loop to manage user interaction
3: #include <iostream>
4:
5: // prototypes
6: int menu();
7: void DoTaskOne();
8: void DoTaskMany(int);
9:
10: using namespace std;
11:
12: int main()
13: {
14: bool exit = false;
15: for (;;)
16: {
17: int choice = menu();
18: switch(choice)
19: {
20: case (1):
21: DoTaskOne();
22: break;
23: case (2):
24: DoTaskMany(2);
25: break;
26: case (3):
27: DoTaskMany(3);
28: break;
29: case (4):
30: continue; // redundant!
31: break;
32: case (5):
33: exit=true;
34: break;
Free download pdf