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

📄 14.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
	PreDecrementExpression<br>
	PostIncrementExpression<br>
	PostDecrementExpression<br>
	MethodInvocation<br>
</i>	<i>ClassInstanceCreationExpression
</i></pre></ul><a name="5988"></a>
An <i>expression statement</i> is executed by evaluating the expression; if the expression has a value, the value is discarded. Execution of the expression statement completes normally if and only if evaluation of the expression completes normally.<p>
<a name="5989"></a>
Unlike C and C++, the Java language allows only certain forms of expressions to be used as expression statements. Note that Java does not allow a "cast to <code>void</code>"-<code>void</code> is not a type in Java-so the traditional C trick of writing an expression statement such as:<p>
<pre><a name="35455"></a>(void) ... ;								// This idiom belongs to C, not to Java!
</pre><a name="35457"></a>
does not work in Java. On the other hand, Java allows all the most useful kinds of 
expressions in expressions statements, and Java does not require a method invocation
used as an expression statement to invoke a <code>void</code> method, so such a trick is 
almost never needed. If a trick is needed, either an assignment statement <a href="15.doc.html#5281">(&#167;15.25)</a> 
or a local variable declaration statement <a href="14.doc.html#5920">(&#167;14.3)</a> can be used instead.
<p><a name="5991"></a>
<h2>14.8    The <code>if</code> Statement</h2>
<a name="35469"></a>
The <code>if</code> statement allows conditional execution of a statement or a conditional 
choice of two statements, executing one or the other but not both.
<p><ul><pre>
<i>IfThenStatement:<br>
</i>	<code>if ( </code><i>Expression</i><code> ) </code><i>Statement
</i>
<i>IfThenElseStatement:<br>
</i>	<code>if ( </code><i>Expression</i><code> ) </code><i>StatementNoShortIf</i><code> else </code><i>Statement
</i>
<i>IfThenElseStatementNoShortIf:<br>
</i>	<code>if ( </code><i>Expression</i><code> ) </code><i>StatementNoShortIf</i><code> else </code><i>StatementNoShortIf
</i></pre></ul><a name="24313"></a>
The <i>Expression</i> must have type <code>boolean</code>, or a compile-time error occurs.
<p><a name="237433"></a>
<h3>14.8.1    The <code>if-then</code> Statement</h3>
<a name="35473"></a>
An <code>if</code>-<code>then</code> statement is executed by first evaluating the <i>Expression</i>. If evaluation 
of the <i>Expression</i> completes abruptly for some reason, the <code>if</code>-<code>then</code> statement 
completes abruptly for the same reason. Otherwise, execution continues by making
a choice based on the resulting value:
<p><ul><a name="5997"></a>
<li>If the value is <code>true</code>, then the contained <i>Statement</i> is executed; the <code>if</code>-<code>then</code> statement completes normally only if execution of the <i>Statement</i> completes normally.
<a name="5998"></a>
<li>If the value is <code>false</code>, no further action is taken and the <code>if</code>-<code>then</code> statement completes normally.
</ul><a name="237435"></a>
<h3>14.8.2    The <code>if-then-else</code> Statement</h3>
<a name="236506"></a>
An <code>if</code>-<code>then</code>-<code>else</code> statement is executed by first evaluating the <i>Expression</i>. If 
evaluation of the <i>Expression</i> completes abruptly for some reason, then the <code>if</code>-
<code>then</code>-<code>else</code> &#32;statement completes abruptly for the same reason. Otherwise, execution
continues by making a choice based on the resulting value:
<p><ul><a name="6000"></a>
<li>If the value is <code>true</code>, then the first contained <i>Statement</i> (the one before the <code>else</code> keyword) is executed; the <code>if</code>-<code>then</code>-<code>else</code> statement completes normally only if execution of that statement completes normally.
<a name="42708"></a>
<li>If the value is <code>false</code>, then the second contained <i>Statement</i> (the one after the <code>else</code> keyword) is executed; the <code>if</code>-<code>then</code>-<code>else</code> statement completes normally only if execution of that statement completes normally. 
</ul><a name="35518"></a>
<h2>14.9    The <code>switch</code> Statement</h2>
<a name="35522"></a>
The <code>switch</code> statement transfers control to one of several statements depending on 
the value of an expression.
<p><ul><pre>
<i>SwitchStatement:<br>
</i><code>	switch ( </code><i>Expression</i><code> ) </code><i>SwitchBlock
</i>
<i>SwitchBlock:<br>
</i>	<code>{ </code>S<i>witchBlockStatementGroups</i><sub><i>opt</i></sub><code> </code><i>SwitchLabels</i><sub><i>opt</i></sub><code> }
</code>
<i>SwitchBlockStatementGroups:<br>
</i><code>	</code><i>SwitchBlockStatementGroup<br>
</i><code>	</code><i>SwitchBlockStatementGroups</i><code> </code><i>SwitchBlockStatementGroup
</i>
<i>SwitchBlockStatementGroup:<br>
</i>	<i>SwitchLabels</i><code> </code><i>BlockStatements
</i>
<i>SwitchLabels:<br>
</i>	<i>SwitchLabel<br>
</i>	<i>SwitchLabels</i><code> </code><i>SwitchLabel
</i>
<i>SwitchLabel:<br>
</i><code>	case </code><i>ConstantExpression</i><code> :<br>
	default :
</code></pre></ul><a name="237298"></a>
The type of the <i>Expression</i> must be <code>char</code>, <code>byte</code>, <code>short</code>, or <code>int</code>, or a compile-time error occurs.<p>
<a name="237299"></a>
The body of a <code>switch</code> statement must be a block. Any statement immediately contained by the block may be labeled with one or more <code>case</code> or <code>default</code> labels. These labels are said to be <i>associated</i> with the <code>switch</code> statement, as are the values of the constant expressions <a href="15.doc.html#5313">(&#167;15.27)</a> in the <code>case</code> labels.<p>
<a name="237328"></a>
All of the following must be true, or a compile-time error will result:<p>
<ul><a name="237329"></a>
<li>Every <code>case</code> constant expression associated with a <code>switch</code> statement must be assignable <a href="5.doc.html#170768">(&#167;5.2)</a> to the type of the <code>switch</code> <i>Expression</i>.
<a name="237333"></a>
<li>No two of the <code>case</code> constant expressions associated with a <code>switch</code> statement may have the same value.
<a name="237334"></a>
<li>At most one <code>default</code> label may be associated with the same <code>switch</code> statement.
</ul><a name="237303"></a>
In C and C++ the body of a <code>switch</code> statement can be a statement and statements with <code>case</code> labels do not have to be immediately contained by that statement. Consider the simple loop:<p>
<pre><a name="237281"></a>for (i = 0; i &lt; n; ++i) foo();
</pre><a name="237282"></a>
where <code>n</code> is known to be positive. A trick known as <i>Duff's device</i> can be used in C 
or C++ to unroll the loop, but this is not valid Java code:
<p><pre><a name="237283"></a>
int q = (n+7)/8;
<a name="237284"></a>switch (n%8) {
<a name="237285"></a>case 0:			do {		foo();				// Great C hack, Tom,
<a name="237286"></a>case 7:					foo();				// but it's not valid in Java.
<a name="237287"></a>case 6:					foo();
<a name="237288"></a>case 5:					foo();
<a name="237289"></a>case 4:					foo();
<a name="237290"></a>case 3:					foo();
<a name="237291"></a>case 2:					foo();
<a name="237292"></a>case 1:					foo();
<a name="237293"></a>			} while (--q &gt;= 0);
<a name="237294"></a>}
</pre><a name="237295"></a>
Fortunately, this trick does not seem to be widely known or used. Moreover, it is 
less needed nowadays; this sort of code transformation is properly in the province 
of state-of-the-art optimizing compilers.
<p><a name="217138"></a>
When the <code>switch</code> statement is executed, first the <i>Expression</i> is evaluated. If evaluation of the <i>Expression</i> completes abruptly for some reason, the <code>switch</code> statement completes abruptly for the same reason. Otherwise, execution continues by comparing the value of the <i>Expression </i>with each <code>case</code> constant. Then there is a choice:<p>
<ul><a name="6012"></a>
<li>If one of the <code>case</code> constants is equal to the value of the expression, then we say that the <code>case</code> matches, and all statements after the matching <code>case</code> label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the matching <code>case</code> label, then the entire <code>switch</code> statement completes normally.
</ul><ul><a name="6013"></a>
<li>If no <code>case</code> matches but there is a <code>default</code> label, then all statements after the matching <code>default</code> label in the switch block, if any, are executed in sequence. If all these statements complete normally, or if there are no statements after the <code>default</code> label, then the entire <code>switch</code> statement completes normally.
<a name="6014"></a>
<li>If no <code>case</code> matches and there is no <code>default</code> label, then no further action is taken and the <code>switch</code> statement completes normally.
</ul><a name="237254"></a>
If any statement immediately contained by the <i>Block</i> body of the <code>switch</code> statement completes abruptly, it is handled as follows:<p>
<ul><a name="6017"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>break</code> with no label, no further action is taken and the <code>switch</code> statement completes normally.
<a name="237272"></a>
<li>If execution of the <i>Statement</i> completes abruptly for any other reason, the <code>switch</code> statement completes abruptly for the same reason. The case of abrupt completion because of a <code>break</code> with a label is handled by the general rule for labeled statements <a href="14.doc.html#78993">(&#167;14.6)</a>.
</ul><a name="237607"></a>
As in C and C++, execution of statements in a switch block "falls through labels" in Java. For example, the program:<p>
<pre><a name="237615"></a>
class Toomany {
<a name="237616"></a>
	static void howMany(int k) {
<a name="237617"></a>		switch (k) {
<a name="237618"></a>		case 1:			System.out.print("one ");
<a name="237619"></a>		case 2:			System.out.print("too ");
<a name="237620"></a>		case 3:			System.out.println("many");
<a name="237621"></a>		}
<a name="237622"></a>	}
<br><a name="237623"></a>
	public static void main(String[] args) {
<a name="237624"></a>		howMany(3);
<a name="237625"></a>		howMany(2);
<a name="237626"></a>		howMany(1);
<a name="237627"></a>	}
<br><a name="237628"></a>}
</pre><a name="237629"></a>
contains a switch block in which the code for each case falls through into the code 
for the next case. As a result, the program prints:
<p><pre><a name="237632"></a>
many
<a name="237633"></a>too many
<a name="237634"></a>one too many
</pre><a name="237630"></a>
If code is not to fall through case to case in this manner, then <code>break</code> statements 
should be used, as in this example:
<p><pre><a name="237644"></a>
class Twomany {
<a name="237645"></a>
	static void howMany(int k) {
<a name="237646"></a>		switch (k) {
<a name="237647"></a>		case 1:			System.out.println("one");
<a name="237648"></a>					break;					// exit the switch
<a name="237649"></a>		case 2:			System.out.println("two");
<a name="237650"></a>					break;					// exit the switch
<a name="237651"></a>		case 3:			System.out.println("many");
<a name="237652"></a>					break;					// not needed, but good style
<a name="237653"></a>		}
<a name="237654"></a>	}
<br><a name="237655"></a>
	public static void main(String[] args) {
<a name="237656"></a>		howMany(1);
<a name="237657"></a>		howMany(2);
<a name="237658"></a>		howMany(3);
<a name="237659"></a>	}
<br><a name="237660"></a>}
</pre><a name="237642"></a>
This program prints:
<p><pre><a name="237670"></a>
one
<a name="237671"></a>two
<a name="237672"></a>many

⌨️ 快捷键说明

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