1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| enum Colors{ COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_GREEN, COLOR_BLUE,};void PrintColor(Colors eColor){ using namespace std; if (eColor == COLOR_BLACK) cout << "Black"; else if (eColor == COLOR_WHITE) cout << "White"; else if (eColor == COLOR_RED) cout << "Red"; else if (eColor == COLOR_GREEN) cout << "Green"; else if (eColor == COLOR_BLUE) cout << "Blue"; else cout << "Unknown";} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| void PrintColor(Colors eColor){ using namespace std; switch (eColor) { case COLOR_BLACK: cout << "Black"; break; case COLOR_WHITE: cout << "White"; break; case COLOR_RED: cout << "Red"; break; case COLOR_GREEN: cout << "Green"; break; case COLOR_BLUE: cout << "Blue"; break; default: cout << "Unknown"; break; }} |
nX + 2 or nX - nY. The one restriction on this expression is that it must evaluate to an integral type (that is, char, short, int, long, or enum). Floating point variables and other non-integral types may not be used here.
1
2
3
4
5
6
| switch (nX){ case 4: case 4: // illegal -- already used value 4! case COLOR_BLUE: // illegal, COLOR_BLUE evaluates to 4!}; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| bool IsNumber(char cChar){ switch (cChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return true; default: return false; }} |
return true;, which returns the value true to the caller.1) The end of the switch block is reached
2) A return statement occurs
3) A goto statement occurs
4) A break statement occurs
1
2
3
4
5
6
7
8
9
10
11
12
13
| switch (2){ case 1: // Does not match -- skipped cout << 1 << endl; case 2: // Match! Execution begins at the next statement cout << 2 << endl; // Execution begins here case 3: cout << 3 << endl; // This is also executed case 4: cout << 4 << endl; // This is also executed default: cout << 5 << endl; // This is also executed} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| switch (2){ case 1: // Does not match -- skipped cout << 1 << endl; break; case 2: // Match! Execution begins at the next statement cout << 2 << endl; // Execution begins here break; // Break terminates the switch statement case 3: cout << 3 << endl; break; case 4: cout << 4 << endl; break; default: cout << 5 << endl; break;}// Execution resumes here |


0 comments: