|
CHAPTER 14: Blocks and Statements |
|
 Previous |
 Java Language |
 Index |
 Next |
14.10 The while Statement
14.10.1 Abrupt Completion
The while
statement executes an Expression and a Statement repeatedly until the value of the Expression is false
.
WhileStatement:
while ( Expression ) Statement
WhileStatementNoShortIf:
while ( Expression ) StatementNoShortIf
The Expression must have type boolean
, or a compile-time error occurs.
A while
statement is executed by first evaluating the Expression. If evaluation of the Expression completes abruptly for some reason, the while
statement completes abruptly for the same reason. Otherwise, execution continues by making a choice based on the resulting value:
- If the value is true
, then the contained Statement is executed. Then there is a choice:
- If execution of the Statement completes normally, then the entire while
statement is executed again, beginning by re-evaluating the Expression.
- If execution of the Statement completes abruptly, see S14.10.1 below.
- If the value of the Expression is false
, no further action is taken and the while
statement completes normally.
If the value of the Expression is false
the first time it is evaluated, then the Statement is not executed.
Abrupt completion of the contained Statement is handled in the following manner:
- If execution of the Statement completes abruptly because of a break
with no label, no further action is taken and the while
statement completes normally.
- If execution of the Statement completes abruptly because of a continue
with no label, then the entire while
statement is executed again.
- If execution of the Statement completes abruptly because of a continue
with label L, then there is a choice:
- If the while
statement has label L, then the entire while
statement is executed again.
- If the while
statement does not have label L, the while
statement completes abruptly because of a continue
with label L.
- If execution of the Statement completes abruptly for any other reason, the while
statement completes abruptly for the same reason. Note that the case of abrupt completion because of a break
with a label is handled by the general rule for labeled statements (S14.6).
 | © 1996 Sun Microsystems, Inc. All rights reserved. |