do

14.11 The do Statement

The do statement executes a Statement and an Expression repeatedly until the value of the Expression is false.

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:

Executing a do statement always executes the contained Statement at least once.

14.11.1 Abrupt Completion

Abrupt completion of the contained Statement is handled in the following manner:

14.11.2 Example of do statement

The 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.