forked from DlangRen/Programming-in-D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_case.d
375 lines (278 loc) · 9.7 KB
/
switch_case.d
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
Ddoc
$(DERS_BOLUMU $(IX switch) $(IX case) $(CH4 switch) and $(CH4 case))
$(P
$(C switch) is a statement that allows comparing the value of an expression against multiple values. It is similar to but not the same as an "if, else if, else" chain. $(C case) is used for specifying the values that are to be compared with $(C switch)'s expression. $(C case) is a part of the $(C switch) statement, not a statement itself.
)
$(P
$(C switch) takes an expression within parentheses, compares the value of that expression to the $(C case) values, and executes the operations of the $(C case) that is equal to the value of the expression. Its syntax consists of a $(C switch) block that contains one or more $(C case) sections and a $(C default) section:
)
---
switch ($(I expression)) {
case $(I value_1):
// operations to execute if the expression is equal to value_1
// ...
break;
case $(I value_2):
// operations to execute if the expression is equal to value_2
// ...
break;
// ... other cases ...
default:
// operations to execute if the expression is not equal to any case
// ...
break;
}
---
$(P
The expression that $(C switch) takes is not used directly as a logical expression. It is not evaluated as "if this condition is true", as it would be in an $(C if) statement. The $(I value) of the $(C switch) expression is used in equality comparisons with the $(C case) values. It is similar to an "if, else if, else" chain that has only equality comparisons:
)
---
auto value = $(I expression);
if (value == $(I value_1)) {
// operations for value_1
// ...
} else if (value == $(I value_2)) {
// operations for value_2
// ...
}
// ... other 'else if's ...
} else {
// operations for other values
// ...
}
---
$(P
However, the "if, else if, else" above is not an exact equivalent of the $(C switch) statement. The reasons why will be explained in the following sections.
)
$(P
If a $(C case) value matches the value of the $(C switch) expression, then the operations that are under the $(C case) are executed. If no value matches, then the operations that are under the $(C default) are executed.
)
$(H5 $(IX goto case) $(IX goto default) The $(C goto) statement)
$(P
The use of $(C goto) is generally advised against in most programming languages. However, $(C goto) is useful in $(C switch) statements in some situations, without being as problematic as in other uses. The $(C goto) statement will be covered in more detail in $(LINK2 /ders/d.en/goto.html, a later chapter).
)
$(P
$(C case) does not introduce a $(I scope) as the $(C if) statement does. Once the operations within an $(C if) or $(C else) scope are finished the evaluation of the entire $(C if) statement is also finished. That does not happen with the $(C case) sections; once a matching $(C case) is found, the execution of the program jumps to that $(C case) and executes the operations under it. When needed in rare situations, $(C goto case) makes the program execution jump to the next $(C case):
)
---
switch (value) {
case 5:
writeln("five");
$(HILITE goto case); // continues to the next case
case 4:
writeln("four");
break;
default:
writeln("unknown");
break;
}
---
$(P
If $(C value) is 5, the execution continues under the $(C case 5) line and the program prints "five". Then the $(C goto case) statement causes the execution to continue to the next $(C case), and as a result "four" is also printed:
)
$(SHELL
five
four
)
$(P
$(IX fallthrough, case) This use of $(C goto case) is optional. Otherwise, if there is no $(C break) statement, the execution $(I falls through) to the next $(C case) or $(C default) section:
)
---
case 5:
writeln("five");
// No 'break' statement; continues to the next case
case 4:
writeln("four");
break;
---
$(P
$(C goto) can appear in three ways under $(C case) sections:
)
$(UL
$(LI $(C goto case) causes the execution to continue to the next $(C case).)
$(LI $(C goto default) causes the execution to continue to the $(C default) section.)
$(LI $(C goto case $(I expression)) causes the execution to continue to the $(C case) that matches that expression.)
)
$(P
The following program demonstrates these three uses by taking advantage of a $(C foreach) loop:
)
---
import std.stdio;
void main() {
foreach (value; [ 1, 2, 3, 10, 20 ]) {
writefln("--- value: %s ---", value);
switch (value) {
case 1:
writeln("case 1");
$(HILITE goto case);
case 2:
writeln("case 2");
$(HILITE goto case 10);
case 3:
writeln("case 3");
$(HILITE goto default);
case 10:
writeln("case 10");
break;
default:
writeln("default");
break;
}
}
}
---
$(P
The output:
)
$(SHELL
--- value: 1 ---
case 1
case 2
case 10
--- value: 2 ---
case 2
case 10
--- value: 3 ---
case 3
default
--- value: 10 ---
case 10
--- value: 20 ---
default
)
$(H5 The expression must be an integer, string, or $(C bool) type)
$(P
Any type can be used in equality comparisons in $(C if) statements. On the other hand, the type of the $(C switch) expression is limited to all integer types, all string types, and $(C bool).
)
---
string op = /* ... */;
// ...
switch (op) {
case "add":
result = first + second;
break;
case "subtract":
result = first - second;
break;
case "multiply":
result = first * second;
break;
case "divide":
result = first / second;
break;
default:
throw new Exception(format("Unknown operation: %s", op));
}
---
$(P
$(I $(B Note:) The code above throws an exception when the operation is not recognized by the program. We will see exceptions in $(LINK2 /ders/d.en/exceptions.html, a later chapter).)
)
$(P
Although it is possible to use $(C bool) expressions as well, because $(C bool) has only two values it may be more suitable to use an $(C if) statement or the ternary operator ($(C ?:)) with that type.
)
$(H5 $(IX .., case value range) $(IX range, case) Value ranges)
$(P
Ranges of values can be specified by $(C ..) between $(C case)s:
)
---
switch (dieValue) {
case 1:
writeln("You won");
break;
case 2: $(HILITE ..) case 5:
writeln("It's a draw");
break;
case 6:
writeln("I won");
break;
default:
/* The program should never get here because the cases
* above cover the entire range of valid die values.
* (See 'final switch' below.) */
break;
}
---
$(P
The code above determines that the game ends in a draw when the die value is 2, 3, 4, or 5.
)
$(H5 Distinct values)
$(P
$(IX , (comma), case value list) Let's assume that it is a draw for the values 2 and 4, rather than for the values that are in the range [2, 5]. Distinct values of a $(C case) are separated by commas:
)
---
case 2$(HILITE ,) 4:
writeln("It's a draw");
break;
---
$(H5 $(IX final switch) The $(C final switch) statement)
$(P
The $(C final switch) statement works similarly to the regular $(C switch) statement, with the following differences:
)
$(UL
$(LI It cannot have a $(C default) section. Note that this section is meaningless when the $(C case) sections cover the entire range of values anyway, as has been with the six values of the die above.
)
$(LI Value ranges cannot be used with $(C case)s (distinct values can be).
)
$(LI If the expression is of an $(C enum) type, all of the values of the type must be covered by the $(C case)s (we will see $(C enum) types in the next chapter).
)
)
---
int dieValue = 1;
final switch (dieValue) {
case 1:
writeln("You won");
break;
case 2, 3, 4, 5:
writeln("It's a draw");
break;
case 6:
writeln("I won");
break;
}
---
$(H5 When to use)
$(P
$(C switch) is suitable for comparing the value of an expression against a set of values that are known at compile time.
)
$(P
When there are only two values to compare, an $(C if) statement may make more sense. For example, to check whether it is heads or tails:
)
---
if (headsTailsResult == heads) {
// ...
} else {
// ...
}
---
$(P
As a general rule, $(C switch) is more suitable when there are three or more values to compare.
)
$(P
When all of the values need to be handled, then prefer $(C final switch). This is especially the case for $(C enum) types.
)
$(PROBLEM_COK
$(PROBLEM
Write a calculator program that supports arithmetic operations. Have the program first read the operation as a $(C string), then two values of type $(C double) from the input. The calculator should print the result of the operation. For example, when the operation and values are "add" and "5 7", respectively, the program should print 12.
$(P
The input can be read as in the following code:
)
---
string op;
double first;
double second;
// ...
op = strip(readln());
readf(" %s %s", &first, &second);
---
)
$(PROBLEM
Improve the calculator to support operators like "+" in addition to words like "add".
)
$(PROBLEM
Have the program throw an exception for unknown operators. We will cover exceptions in $(LINK2 /ders/d.en/exceptions.html, a later chapter). For now, adapt the $(C throw) statement used above to your program.
)
)
Macros:
SUBTITLE=switch and case
DESCRIPTION=The switch-case statement that is used for comparing the value of an expression against multiple values.
KEYWORDS=d programming language tutorial book switch case