do
StatementThe do
statement executes a Statement and an Expression repeatedly until the value of the Expression is false
.
DoStatement:
do
Statementwhile (
Expression) ;
The Expression must have type boolean
, or a compile-time error occurs.
A do
statement is executed by first executing the Statement. Then there is a choice:
do
statement completes abruptly for the same reason. Otherwise, there is a choice based on the resulting value:
Executing a do
statement always executes the contained Statement at least once.
Abrupt completion of the contained Statement is handled in the following manner:
break
with no label, then no further action is taken and the do
statement completes normally.continue
with no label, then the Expression is evaluated. Then there is a choice based on the resulting value:
true
, then the entire do
statement is executed again.false
, no further action is taken and the do
statement completes normally.continue
with label L, then there is a choice:
do
statement has label L, then the Expression is evaluated. Then there is a choice:
true
, then the entire do
statement is executed again.false
, no further action is taken and the do
statement completes normally.do
statement does not have label L, the do
statement completes abruptly because of a continue
with label L.do
statement completes abruptly for the same reason. The case of abrupt completion because of a break
with a label is handled by the general rule.do
statementThe following code is one possible implementation of the toHexString
method of class Integer
:
public static String toHexString(int i) { StringBuffer buf = new StringBuffer(8); do { buf.append(Character.forDigit(i & 0xF, 16)); i >>>= 4; } while (i != 0); return buf.reverse().toString(); }
Because at least one digit must be generated, the do
statement is an appropriate control structure.