The Java programming language provides a do-while statement, which can be expressed as follows:
do{
statement(s)
} while (expression);Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false. Fig. 1 shows the main idea of this loop.
Fig. 1
Example,
public static void main(String[] args) {
int x = 1;
do{
System.out.print("value of x: " + x);
x++;
System.out.print("\n");
} while(x < 11);
}Result:
value of x : 1
value of x : 2
value of x : 3
value of x : 4
value of x : 5
value of x : 6
value of x : 7
value of x : 8
value of x : 9
value of x : 10