⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 15.7.2.htm

📁 This ECMA Standard specifies the form and establishes the interpretation of programs written in the
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Hyperlinked ECMA C# Language Specification</title><meta name="author" content="Jon Jagger" /><link rel="stylesheet" href="ecma334.css"></link></head><body><div align="right"><em><a href="http://www.jaggersoft.com">Jon Jagger</a></em></div><div align="right"><a href="mailto:jon@jaggersoft.com">jon@jaggersoft.com</a></div><form method="get" action="http://search.atomz.com/search/"><input size="30" name="sp-q"></input><input type="submit" value="Search C# Spec"></input><input type="hidden" name="sp-a" value="sp10024177"></input><input type="hidden" name="sp-f" value="ISO-8859-1"></input></form><a href="toc.htm">Table of Contents</a> <a href="1.htm">1</a> <a href="2.htm">2</a> <a href="3.htm">3</a> <a href="4.htm">4</a> <a href="5.htm">5</a> <a href="6.htm">6</a> <a href="7.htm">7</a> <a href="8.htm">8</a> <a href="9.htm">9</a> <a href="10.htm">10</a> <a href="11.htm">11</a> <a href="12.htm">12</a> <a href="13.htm">13</a> <a href="14.htm">14</a> <a href="15.htm">15</a> <a href="16.htm">16</a> <a href="17.htm">17</a> <a href="18.htm">18</a> <a href="19.htm">19</a> <a href="20.htm">20</a> <a href="21.htm">21</a> <a href="22.htm">22</a> <a href="23.htm">23</a> <a href="24.htm">24</a> <a href="25.htm">25</a> <a href="notes.htm">Notes</a> <a href="HyperlinkedCSharpECMA.zip">Download</a><span class="ruler"></span><span class="heading">ECMA-334 C# Language Specification</span><span class="navigate"><a href="15.7.1.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="15.8.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="14.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="16.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="15.htm">15</a></span><span class="clause-title-previous"> Statements</span></span><span class="clause-depth"><a href="15.6.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="15.8.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="15.7.htm">15.7</a></span><span class="clause-title-previous"> Selection statements</span></span><span class="clause-depth"><a href="15.7.1.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="15.7.2.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">15.7.2</span><span class="clause-title"> The switch statement</span></span><span class="locator">
     Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>The switch statement selects for execution a statement list having an associated switch label that corresponds to the value of the switch expression.</span> <span class="grammar-production"><span class="name"><a name="switch-statement"></a>switch-statement</span> : <span class="rhs"><span class="keyword">switch</span> <span class="terminal">(</span> <span class="non-terminal"><a href="14.14.htm#expression">expression</a></span> <span class="terminal">)</span> <span class="non-terminal"><a href="15.7.2.htm#switch-block">switch-block</a></span> </span></span><span class="grammar-production"><span class="name"><a name="switch-block"></a>switch-block</span> : <span class="rhs"><span class="terminal">{</span> <span class="non-terminal"><a href="15.7.2.htm#switch-sections">switch-sections</a></span><sub>opt</sub> <span class="terminal">}</span> </span></span><span class="grammar-production"><span class="name"><a name="switch-sections"></a>switch-sections</span> : <span class="rhs"><span class="non-terminal"><a href="15.7.2.htm#switch-section">switch-section</a></span> </span><span class="rhs"><span class="non-terminal"><a href="15.7.2.htm#switch-sections">switch-sections</a></span> <span class="non-terminal"><a href="15.7.2.htm#switch-section">switch-section</a></span> </span></span><span class="grammar-production"><span class="name"><a name="switch-section"></a>switch-section</span> : <span class="rhs"><span class="non-terminal"><a href="15.7.2.htm#switch-labels">switch-labels</a></span> <span class="non-terminal"><a href="15.2.1.htm#statement-list">statement-list</a></span> </span></span><span class="grammar-production"><span class="name"><a name="switch-labels"></a>switch-labels</span> : <span class="rhs"><span class="non-terminal"><a href="15.7.2.htm#switch-label">switch-label</a></span> </span><span class="rhs"><span class="non-terminal"><a href="15.7.2.htm#switch-labels">switch-labels</a></span> <span class="non-terminal"><a href="15.7.2.htm#switch-label">switch-label</a></span> </span></span><span class="grammar-production"><span class="name"><a name="switch-label"></a>switch-label</span> : <span class="rhs"><span class="keyword">case</span> <span class="non-terminal"><a href="14.15.htm#constant-expression">constant-expression</a></span> <span class="terminal">:</span> </span><span class="rhs"><span class="keyword">default</span> <span class="terminal">:</span> </span></span></span><span class="locator">
     Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>A <span class="non-terminal"><a href="15.7.2.htm#switch-statement">switch-statement</a></span> consists of the keyword switch, followed by a parenthesized expression (called the switch expression), followed by a <span class="non-terminal"><a href="15.7.2.htm#switch-block">switch-block</a></span>.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>The <span class="non-terminal"><a href="15.7.2.htm#switch-block">switch-block</a></span> consists of zero or more <span class="non-terminal"><a href="15.7.2.htm#switch-section">switch-section</a></span>s, enclosed in braces.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P2S3"></a>Each <span class="non-terminal"><a href="15.7.2.htm#switch-section">switch-section</a></span> consists of one or more <span class="non-terminal"><a href="15.7.2.htm#switch-label">switch-label</a></span>s followed by a <span class="non-terminal"><a href="15.2.1.htm#statement-list">statement-list</a></span> (<a href="15.2.1.htm">&#167;15.2.1</a>).</span> </span><span class="locator">
     Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>The governing type of a switch statement is established by the switch expression.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a>If the type of the switch expression is <span class="keyword">sbyte</span>, <span class="keyword">byte</span>, <span class="keyword">short</span>, <span class="keyword">ushort</span>, <span class="keyword">int</span>, <span class="keyword">uint</span>, <span class="keyword">long</span>, <span class="keyword">ulong</span>, <span class="keyword">char</span>, string, or an <span class="non-terminal"><a href="11.1.htm#enum-type">enum-type</a></span>, then that is the governing type of the switch statement.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a>Otherwise, exactly one user-defined implicit conversion (<a href="13.4.htm">&#167;13.4</a>) must exist from the type of the switch expression to one of the following possible governing types: <span class="keyword">sbyte</span>, <span class="keyword">byte</span>, <span class="keyword">short</span>, <span class="keyword">ushort</span>, <span class="keyword">int</span>, <span class="keyword">uint</span>, <span class="keyword">long</span>, <span class="keyword">ulong</span>, <span class="keyword">char</span>, string.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a>If no such implicit conversion exists, or if more than one such implicit conversion exists, a compile-time error occurs.</span> </span><span class="locator">
     Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>The constant expression of each case label must denote a value of a type that is implicitly convertible (<a href="13.1.htm">&#167;13.1</a>) to the governing type of the switch statement.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>A compile-time error occurs if two or more case labels in the same switch statement specify the same constant value.</span> </span><span class="locator">
     Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>There can be at most one default label in a switch statement.</span> </span><span class="locator">
     Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>A switch statement is executed as follows: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P6S2"></a> The switch expression is evaluated and converted to the governing type.</span> </li><li><span class="sentence"><span class="sentence-number">3</span> <a name="P6S3"></a> If one of the constants specified in a case label in the same switch statement is equal to the value of the switch expression, control is transferred to the statement list following the matched case label.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P6S4"></a> If none of the constants specified in case labels in the same switch statement is equal to the value of the switch expression, and if a default label is present, control is transferred to the statement list following the default label.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P6S5"></a> If none of the constants specified in case labels in the same switch statement is equal to the value of the switch expression, and if no default label is present, control is transferred to the end point of the switch statement.</span> </li></ul></span><span class="locator">
     Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>If the end point of the statement list of a switch section is reachable, a compile-time error occurs.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P7S2"></a>This is known as the "no fall through" rule.</span> <span class="example">[Example: The example <pre class="code-example">
switch (i) {  
   case 0:  
   CaseZero();  
   break;  
   case 1:  
   CaseOne();  
   break;  
   default:  
   CaseOthers();  
   break;  
}  
</pre>is valid because no switch section has a reachable end point. Unlike C and C++, execution of a switch section is not permitted to "fall through" to the next switch section, and the example <pre class="code-example">
switch (i) {  
   case 0:  
   CaseZero();  
   case 1:  
   CaseZeroOrOne();  
   default:  
   CaseAny();  
}  
</pre>results in a compile-time error. When execution of a switch section is to be followed by execution of another switch section, an explicit goto case or goto default statement must be used: <pre class="code-example">
switch (i) {  
   case 0:  
   CaseZero();  
   goto case 1;  
   case 1:  
   CaseZeroOrOne();  
   goto default;  
   default:  
   CaseAny();  
   break;  
}  
</pre>end example]</span> </span><span class="locator">
     Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>Multiple labels are permitted in a <span class="non-terminal"><a href="15.7.2.htm#switch-section">switch-section</a></span>.</span> <span class="example">[Example: The example <pre class="code-example">
switch (i) {  
   case 0:  
   CaseZero();  
   break;  
   case 1:  
   CaseOne();  
   break;  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -