-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path80_switch_case_statement.txt
More file actions
90 lines (63 loc) · 1.57 KB
/
80_switch_case_statement.txt
File metadata and controls
90 lines (63 loc) · 1.57 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Section 09: switch-case Statement
----------------------------------------------------------------------
The switch statement I
switch (integer_control_expr) {
case expression_1:
statement_1;
break;
case expression_2:
statement_2;
break;
case expression_3:
statement_3;
break;
...
case expression_n:
statement_n;
break;
default:
statement_default;
}
Example 1
switch (selection) {
case '1': cout << "1 selected";
break;
case '2': cout << "2 selected";
break;
case '3':
case '4': cout << "3 or 4 selected";
break;
default: cout << "1, 2, 3, 4 NOT selected";
}
fall-through Example
switch (selection) {
case '1': cout << "1 selected";
case '2': cout << "2 selected";
case '3': cout << "3 selected";
case '4': cout << "4 selected";
break;
default: cout << "1, 2, 3, 4 NOT selected";
}
with an enumeration
enum Color {
red, green, blue
};
Color screen_color {green};
switch (screen_color) {
case red: cout << "red"; break;
case green: cout << "green"; break;
case blue: cout << "blue"; break;
default: cout << "should never execute";
}
The switch Statement II
- The control expression must evaluate to an integer
type
- The case expressions must be constant expressions
that evaluate to integer or integers literals
- Once a match occurs, all following case sections
execute UNTIL a 'break' has reached the switch
complete
- Best practice - provide a break statement for
each case
- Best practice - 'default' is optional, but should
be handled