@@ -1124,36 +1124,33 @@ $(GNAME StatementNoCaseNoDefault):
1124
1124
a match, the corresponding case statement is transferred to.
1125
1125
)
1126
1126
1127
- $(P The case expressions, $(GLINK2 expression, ArgumentList),
1127
+ $(P The case expressions in $(I ArgumentList)
1128
1128
are a comma separated list of expressions.
1129
1129
)
1130
1130
1131
1131
$(P A $(I CaseRangeStatement) is a shorthand for listing a series
1132
1132
of case statements from $(I FirstExp) to $(I LastExp).
1133
1133
)
1134
1134
1135
-
1136
- $(P If none of the case expressions match, and there is a default
1137
- statement, the default statement is transferred to.
1138
- )
1139
-
1140
-
1141
- $(P A switch statement must have a default statement.)
1142
-
1143
-
1144
1135
$(P The case expressions must all evaluate to a constant value or array,
1145
1136
or a runtime initialized const or immutable variable of integral type.
1146
1137
They must be implicitly convertible to the type of the switch
1147
1138
*Expression*. )
1148
1139
1149
1140
$(P Case expressions must all evaluate to distinct values. Const or
1150
1141
immutable variables must all have different names. If they share a
1151
- value, the first case statement with that value gets control. There must
1152
- be exactly one default statement.)
1142
+ value, the first case statement with that value gets control.)
1153
1143
1154
1144
$(P The $(GLINK ScopeStatementList) introduces a new scope.
1155
1145
)
1156
1146
1147
+ $(P A `break` statement will exit the switch $(I BlockStatement).)
1148
+
1149
+ $(P A switch statement must have exactly one *DefaultStatement*.
1150
+ If none of the case expressions match, control is transferred
1151
+ to the default statement.
1152
+ )
1153
+
1157
1154
$(P Case statements and default statements associated with the switch
1158
1155
can be nested within block statements; they do not have to be in
1159
1156
the outermost block. For example, this is allowed:
@@ -1166,53 +1163,70 @@ switch (i)
1166
1163
{
1167
1164
case 2:
1168
1165
}
1166
+ i++;
1169
1167
break;
1168
+ default:
1170
1169
}
1171
1170
--------------
1172
1171
1172
+ $(H3 $(LNAME2 no-implicit-fallthrough, No Implicit Fall-Through))
1173
1173
1174
1174
1175
1175
$(P A $(GLINK ScopeStatementList) must either be empty, or be ended with
1176
1176
a $(GLINK ContinueStatement), $(GLINK BreakStatement),
1177
1177
$(GLINK ReturnStatement), $(GLINK GotoStatement), $(GLINK ThrowStatement)
1178
- or assert(0) expression unless this is the last case. This is to
1179
- set apart with C's error-prone implicit fall-through behavior.
1180
- $(D goto case;) could be used for explicit fall-through:
1181
- )
1178
+ or `assert(0)` expression unless this is the last case. This is to
1179
+ set apart with C's error-prone implicit fall-through behavior.)
1182
1180
1183
1181
--------------
1184
- int number;
1185
- string message;
1186
- switch (number)
1182
+ switch (i)
1187
1183
{
1188
- default: // valid: ends with 'throw'
1189
- throw new Exception("unknown number");
1184
+ case 1:
1185
+ message ~= "one";
1186
+ // ERROR: implicit fall-through
1187
+ case 2:
1188
+ // valid: the body is empty
1189
+ default:
1190
+ message ~= "unknown";
1191
+ }
1192
+ --------------
1190
1193
1191
- case 3: // valid: ends with 'break' (break out of the 'switch' only)
1192
- message ~= "three ";
1193
- break;
1194
+ $(P $(D goto case;) can be used for explicit fall-through:)
1194
1195
1195
- case 4: // valid: ends with 'continue' (continue the enclosing loop)
1196
- message ~= "four ";
1197
- continue;
1196
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
1197
+ --------------
1198
+ string message;
1199
+ foreach (i; 1..5)
1200
+ {
1201
+ switch (i)
1202
+ {
1203
+ default: // valid: ends with 'throw'
1204
+ throw new Exception("unknown number");
1198
1205
1199
- case 5: // valid: ends with 'goto' (explicit fall-through to next case.)
1200
- message ~= "five ";
1201
- goto case;
1206
+ case 3: // valid: ends with 'break' (break out of the 'switch' only)
1207
+ message ~= "three";
1208
+ break;
1209
+
1210
+ case 4: // valid: ends with 'continue' (continue the enclosing loop)
1211
+ message ~= "four";
1212
+ continue; // don't append a comma
1202
1213
1203
- case 6: // ERROR: implicit fall-through
1204
- message ~= "six ";
1214
+ case 1: // valid: ends with 'goto' (explicit fall-through to next case.)
1215
+ message ~= ">";
1216
+ goto case;
1205
1217
1206
- case 1: // valid: the body is empty
1207
- case 2: // valid: this is the last case in the switch statement.
1208
- message = "one or two";
1218
+ case 2: // valid: this is the last case in the switch statement.
1219
+ message ~= "one or two";
1220
+ }
1221
+ message ~= ", ";
1209
1222
}
1223
+ writeln(message);
1210
1224
--------------
1225
+ )
1211
1226
1212
- $(P A break statement will exit the switch $(I BlockStatement).)
1213
-
1227
+ $(H3 $(LNAME2 string-switch, String Switch))
1214
1228
1215
- $(P $(LNAME2 string-switch, Strings can be used in switch expressions) .
1229
+ $(P Strings can be used in switch expressions.
1216
1230
For example:
1217
1231
)
1218
1232
0 commit comments