📄 14.doc.html
字号:
</pre><a name="237277"></a>
<h2>14.10 The <code>while</code> Statement</h2>
<a name="237278"></a>
The <code>while</code> statement executes an <i>Expression</i> and a <i>Statement</i> repeatedly until the
value of the <i>Expression</i> is <code>false</code>.
<p><ul><pre>
<i>WhileStatement:<br>
</i> <code>while ( </code><i>Expression</i><code> ) </code><i>Statement
</i>
<i>WhileStatementNoShortIf:<br>
</i> <code>while ( </code><i>Expression</i><code> ) </code><i>StatementNoShortIf
</i></pre></ul><a name="35485"></a>
The <i>Expression</i> must have type <code>boolean</code>, or a compile-time error occurs.
<p><a name="217153"></a>
A <code>while</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>while</code> statement completes abruptly for the same reason. Otherwise, execution continues by making a choice based on the resulting value:<p>
<ul><a name="6028"></a>
<li>If the value is <code>true</code>, then the contained <i>Statement</i> is executed. Then there is a choice:
<ul>
<a name="6029"></a>
<li>If execution of the <i>Statement</i> completes normally, then the entire <code>while</code> statement is executed again, beginning by re-evaluating the <i>Expression</i>.
<a name="6030"></a>
<li>If execution of the <i>Statement</i> completes abruptly, see <a href="14.doc.html#21139">§14.10.1</a> below.
</ul>
<a name="6031"></a>
<li>If the value of the <i>Expression</i> is <code>false</code>, no further action is taken and the <code>while</code> statement completes normally.
</ul><a name="6032"></a>
If the value of the <i>Expression</i> is <code>false</code> the first time it is evaluated, then the
<i>Statement</i>  is not executed.
<p><a name="21139"></a>
<h3>14.10.1 Abrupt Completion</h3>
<a name="6033"></a>
Abrupt completion of the contained <i>Statement</i> is handled in the following manner:
<p><ul><a name="6034"></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>while</code> statement completes normally.
<ul>
<a name="6035"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>continue</code> with no label, then the entire <code>while</code> statement is executed again.
<a name="79063"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>continue</code> with label <i>L</i>, then there is a choice:
<ul>
<a name="79064"></a>
<li>If the <code>while</code> statement has label <i>L</i>, then the entire <code>while</code> statement is executed again.
<a name="79065"></a>
<li>If the <code>while</code> statement does not have label <i>L</i>, the <code>while</code> statement completes abruptly because of a <code>continue</code> with label <i>L</i>.
</ul>
<a name="79070"></a>
<li>If execution of the <i>Statement</i> completes abruptly for any other reason, the <code>while</code> statement completes abruptly for the same reason. Note that 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">(§14.6)</a>.
</ul>
</ul><a name="6045"></a>
<h2>14.11 The <code>do</code> Statement</h2>
<a name="35504"></a>
The <code>do</code> statement executes a <i>Statement</i> and an <i>Expression</i> repeatedly until the
value of the <i>Expression</i> is <code>false</code>.
<p><ul><pre>
<i>DoStatement:<br>
</i><code> do </code><i>Statement</i><code> while ( </code><i>Expression</i><code> ) ;
</code></pre></ul><a name="35496"></a>
The <i>Expression</i> must have type <code>boolean</code>, or a compile-time error occurs.
<p><a name="6048"></a>
A <code>do</code> statement is executed by first executing the <i>Statement</i>. Then there is a choice:<p>
<ul><a name="217176"></a>
<li>If execution of the <i>Statement</i> completes normally, then the <i>Expression</i> is evaluated. If evaluation of the <i>Expression</i> completes abruptly for some reason, the <code>do</code> statement completes abruptly for the same reason. Otherwise, there is a choice based on the resulting value:
<ul>
<a name="217172"></a>
<li>If the value is <code>true</code>, then the entire <code>do</code> statement is executed again.
<a name="6051"></a>
<li>If the value is <code>false</code>, no further action is taken and the <code>do</code> statement completes normally.
</ul>
<a name="6052"></a>
<li>If execution of the <i>Statement</i> completes abruptly, see <a href="14.doc.html#45366">§14.11.1</a> below.
</ul><a name="6053"></a>
Executing a <code>do</code> statement always executes the contained <i>Statement</i> at least once.
<p><a name="45366"></a>
<h3>14.11.1 Abrupt Completion</h3>
<a name="43246"></a>
Abrupt completion of the contained <i>Statement</i> is handled in the following manner:
<p><ul><a name="6714"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>break</code> with no label, then no further action is taken and the <code>do</code> statement completes normally.
<a name="6056"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>continue</code> with no label, then the <i>Expression</i> is evaluated. Then there is a choice based on the resulting value:
<ul>
<a name="6057"></a>
<li>If the value is <code>true</code>, then the entire <code>do</code> statement is executed again.
<a name="6058"></a>
<li>If the value is <code>false</code>, no further action is taken and the <code>do</code> statement completes normally.
</ul>
<a name="79109"></a>
<li>If execution of the <i>Statement</i> completes abruptly because of a <code>continue</code> with label <i>L</i>, then there is a choice:
<ul>
<a name="79110"></a>
<li>If the <code>do</code> statement has label <i>L</i>, then the <i>Expression</i> is evaluated. Then there is a choice:
<ul>
<a name="79111"></a>
<li>If the value of the <i>Expression</i> is <code>true</code>, then the entire <code>do</code> statement is executed again.
<a name="79112"></a>
<li>If the value of the <i>Expression</i> is <code>false</code>, no further action is taken and the <code>do</code> statement completes normally.
</ul>
<a name="79113"></a>
<li>If the <code>do</code> statement does not have label <i>L</i>, the <code>do</code> statement completes abruptly because of a <code>continue</code> with label <i>L</i>.
</ul>
<a name="24581"></a>
<li>If execution of the <i>Statement</i> completes abruptly for any other reason, the <code>do</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 <a href="14.doc.html#78993">(§14.6)</a>.
</ul><a name="24585"></a>
<h3>14.11.2 Example of <code>do</code> statement</h3>
<a name="43313"></a>
The following code is one possible implementation of the <code>toHexString</code> method
<a href="javalang.doc6.html#7332">(§20.7.14)</a> of class <code>Integer</code>:
<p><pre><a name="43303"></a>
public static String toHexString(int i) {
<a name="43304"></a> StringBuffer buf = new StringBuffer(8);
<a name="43305"></a> do {
<a name="43306"></a> buf.append(Character.forDigit(i & 0xF, 16));
<a name="43307"></a> i >>>= 4;
<a name="43308"></a> } while (i != 0);
<a name="43309"></a> return buf.reverse().toString();
<a name="43310"></a>}
</pre><a name="24586"></a>
Because at least one digit must be generated, the <code>do</code> statement is an appropriate
control structure.
<p><a name="24588"></a>
<h2>14.12 The <code>for</code> Statement</h2>
<a name="35537"></a>
The <code>for</code> statement executes some initialization code, then executes an <i>Expression</i>,
a <i>Statement</i>, and some update code repeatedly until the value of the <i>Expression</i> is
<code>false</code>.
<p><ul><pre>
<i>ForStatement:<br>
</i> <code>for ( </code><i>ForInit</i><sub><i>opt</i></sub><code> ; </code><i>Expression</i><sub><i>opt</i></sub><code> ; </code><i>ForUpdate</i><sub><i>opt</i></sub><code> )<br>
</code> <i>Statement
</i>
<i>ForStatementNoShortIf:<br>
</i> <code>for ( </code><i>ForInit</i><sub><i>opt</i></sub><code> ; </code><i>Expression</i><sub><i>opt</i></sub><code> ; </code><i>ForUpdate</i><sub><i>opt</i></sub><code> )<br>
</code><i>StatementNoShortIf
</i>
<i>ForInit:<br>
</i> <i>StatementExpressionList<br>
LocalVariableDeclaration
</i>
<i>ForUpdate:<br>
StatementExpressionList
</i>
<i>StatementExpressionList:<br>
StatementExpression<br>
StatementExpressionList</i><code> , </code><i>StatementExpression
</i></pre></ul><a name="35529"></a>
The <i>Expression</i> must have type <code>boolean</code>, or a compile-time error occurs.
<p><a name="45367"></a>
<h3>14.12.1 Initialization of <code>for</code> statement</h3>
<a name="11751"></a>
A <code>for</code> statement is executed by first executing the <i>ForInit</i> code:
<p><ul><a name="6081"></a>
<li>If the <i>ForInit</i> code is a list of statement expressions <a href="14.doc.html#5984">(§14.7)</a>, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If evaluation of any expression completes abruptly for some reason, the <code>for</code> statement completes abruptly for the same reason; any <i>ForInit</i> statement expressions to the right of the one that completed abruptly are not evaluated.
<a name="6082"></a>
<li>If the <i>ForInit</i> code is a local variable declaration, it is executed as if it were a local variable declaration statement <a href="14.doc.html#5920">(§14.3)</a> appearing in a block. In this case, the scope of a declared local variable is its own initializer and any further declarators in the <i>ForInit</i> part, plus the <i>Expression</i>, <i>ForUpdate</i>, and contained <i>Statement</i> of the <code>for</code> statement. If execution of the local variable declaration completes abruptly for any reason, the <code>for</code> statement completes abruptly for the same reason.
<a name="6083"></a>
<li>If the <i>ForInit</i> part is not present, no action is taken.
</ul><a name="45370"></a>
<h3>14.12.2 Iteration of <code>for</code> statement</h3>
<a name="217196"></a>
Next, a <code>for</code> iteration step is performed, as follows:
<p><ul><a name="217202"></a>
<li>If the <i>Expression</i> is present, it is evaluated, and if evaluation of the <i>Expression</i> completes abruptly, the <code>for</code> statement completes abruptly for the same reason. Otherwise, there is then a choice based on the presence or absence of the <i>Expression</i> and the resulting value if the <i>Expression</i> is present:
<ul>
<a name="6086"></a>
<li>If the <i>Expression</i> is not present, or it is present and the value resulting from its evaluation is <code>true</code>, then the contained <i>Statement</i> is executed. Then there is a choice:
<ul>
<a name="6087"></a>
<li>If execution of the <i>Statement</i> completes normally, then the following two steps are performed in sequence:
<ul>
<a name="45351"></a>
<li>First, if the <i>ForUpdate</i> part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If evaluation of any expression completes abruptly for some reason, the <code>for</code> statement completes abruptly for the same reason; any <i>ForUpdate</i> statement expressions to the right of the one that completed abruptly are not evaluated. If the <i>ForUpdate</i> part is not present, no action is taken.
<a name="236152"></a>
<li>Second, another <code>for</code> iteration step is performed.
</ul>
<a name="236156"></a>
<li>If execution of the <i>Statement</i> completes abruptly, see <a href="14.doc.html#45371">§14.12.3</a> below.
</ul>
<a name="236157"></a>
<li>If the <i>Expression</i> is present and the value resulting from its evaluation is <code>false</code>, no further action is taken and the <code>for</code> statement completes normally.
</ul>
</ul><a name="6092"></a>
If the value of the <i>Expression</i> is <code>false</code> the first time it is evaluated, then the <i>Statement</i> is not executed.<p>
<a name="6093"></a>
If the <i>Expression</i> is not present, then the only way a <code>for</code> statement can complete normally is by use of a <code>break</code> statement.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -