📄 15.7.2.htm
字号:
case 2:
default:
CaseTwo();
break;
}
</pre>is valid. The example does not violate the "no fall through" rule because the labels case 2: and default: are part of the same <span class="non-terminal"><a href="15.7.2.htm#switch-section">switch-section</a></span>. end example]</span> </span><span class="paragraph"><span class="note">[Note: The "no fall through" rule prevents a common class of bugs that occur in C and C++ when break statements are accidentally omitted. In addition, because of this rule, the switch sections of a switch statement can be arbitrarily rearranged without affecting the behavior of the statement. For example, the sections of the switch statement above can be reversed without affecting the behavior of the statement: <pre class="code-example">
switch (i) {
default:
CaseAny();
break;
case 1:
CaseZeroOrOne();
goto default;
case 0:
CaseZero();
goto case 1;
}
</pre>end note]</span> </span><span class="paragraph"><span class="note">[Note: The statement list of a switch section typically ends in a break, goto case, or goto default statement, but any construct that renders the end point of the statement list unreachable is permitted. For example, a while statement controlled by the boolean expression true is known to never reach its end point. Likewise, a throw or return statement always transfers control elsewhere and never reaches its end point. Thus, the following example is valid: <pre class="code-example">
switch (i) {
case 0:
while (true) F();
case 1:
throw new ArgumentException();
case 2:
return;
}
</pre>end note]</span> </span><span class="paragraph"><span class="example">[Example: The governing type of a switch statement may be the type string. For example: <pre class="code-example">
void DoCommand(string command) {
switch (command.ToLower()) {
case "run":
DoRun();
break;
case "save":
DoSave();
break;
case "quit":
DoQuit();
break;
default:
InvalidCommand(command);
break;
}
}
</pre>end example]</span> </span><span class="paragraph"><span class="note">[Note: Like the string equality operators (<a href="14.9.7.htm">§14.9.7</a>), the switch statement is case sensitive and will execute a given switch section only if the switch expression string exactly matches a case label constant. end note]</span> </span><span class="locator">
Paragraph 9</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P9S1"></a>When the governing type of a switch statement is string, the value null is permitted as a case label constant.</span> </span><span class="locator">
Paragraph 10</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P10S1"></a>The <span class="non-terminal"><a href="15.2.1.htm#statement-list">statement-list</a></span>s of a <span class="non-terminal"><a href="15.7.2.htm#switch-block">switch-block</a></span> may contain declaration statements (<a href="15.5.htm">§15.5</a>).</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P10S2"></a>The scope of a local variable or constant declared in a switch block is the switch block.</span> </span><span class="locator">
Paragraph 11</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P11S1"></a>Within a switch block, the meaning of a name used in an expression context must always be the same (<a href="14.5.2.1.htm">§14.5.2.1</a>).</span> </span><span class="locator">
Paragraph 12</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P12S1"></a>The statement list of a given switch section is reachable if the switch statement is reachable and at least one of the following is true: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P12S2"></a> The switch expression is a non-constant value.</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P12S3"></a> The switch expression is a constant value that matches a case label in the switch section.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P12S4"></a> The switch expression is a constant value that doesn't match any case label, and the switch section contains the default label.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P12S5"></a> A switch label of the switch section is referenced by a reachable goto case or goto default statement.</span> </li></ul></span><span class="locator">
Paragraph 13</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P13S1"></a>The end point of a switch statement is reachable if at least one of the following is true: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P13S2"></a> The switch statement contains a reachable break statement that exits the switch statement.</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P13S3"></a> The switch statement is reachable, the switch expression is a non-constant value, and no default label is present.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P13S4"></a> The switch statement is reachable, the switch expression is a constant value that doesn't match any case label, and no default label is present.</span> </li></ul></span><span class="ruler"></span><table><tr><td><table align="left" bgcolor="navy"><tr bgcolor="navy"><td><font face="Arial,sans-serif" size="6" color="yellow"><strong>{ JSL }</strong></font></td></tr></table></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Jagger Software Ltd</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Company # 4070126</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>VAT # 762 5213 42</strong></font></td></tr></table><img src="valid-html401.png" align="left" height="31" width="88" alt="Valid HTML 4.01" /><img src="vcss.gif" align="left" height="31" width="88" alt="Valid CSS" /></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -