CHAPTER 14: Blocks and Statements Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

14.11 The do Statement

14.11.1 Abrupt Completion , 14.11.2 Example

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


DoStatement:

	do Statement while ( 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:

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

The following code is one possible implementation of the toHexString method (S20.7.14) 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.

Top© 1996 Sun Microsystems, Inc. All rights reserved.