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 Boolean, or a compile-time error occurs.
A while statement is executed by first evaluating the Expression. If the result is of type Boolean, it is subjected to unboxing conversion (§5.1.8).
If evaluation of the Expression or the subsequent unboxing conversion (if any) 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
whilestatement is executed again, beginning by re-evaluating the Expression. - If execution of the Statement completes abruptly, see §14.12.1.
- If execution of the Statement completes normally, then the entire
-
If the (possibly unboxed) value of the Expression is
false, no further action is taken and thewhilestatement completes normally.If the (possibly unboxed) value of the Expression is
falsethe first time it is evaluated, then the Statement is not executed.
14.12.1. Abrupt Completion of while Statement
Abrupt completion of the contained Statement is handled in the following manner:
-
If execution of the Statement completes abruptly because of a
breakwith no label, no further action is taken and thewhilestatement completes normally. -
If execution of the Statement completes abruptly because of a
continuewith no label, then the entirewhilestatement is executed again. -
If execution of the Statement completes abruptly because of a
continuewith labelL, then there is a choice:- If the
whilestatement has labelL, then the entirewhilestatement is executed again. - If the
whilestatement does not have labelL, thewhilestatement completes abruptly because of acontinuewith labelL.
- If the
-
If execution of the Statement completes abruptly for any other reason, the
whilestatement completes abruptly for the same reason.The case of abrupt completion because of a
breakwith a label is handled by the general rule for labeled statements (§14.7).