### The do-statement

• The last loop statement: the do-statement

• The do-statement is different from the while-statement and the for-statement in that:

 The do-statement makes the continuation test after executing the statements in the body

• Due to the different ordering in the test/execute steps, the do-statement is rarely used in computer algorithms

Because in most computer algorithms that have been developed, the algorithm

• first test the condition and
• execute the statements in the body only when the condition is satisfied

• Syntax and meaning of the do-statement

• The general syntax of the do-statement is as follows:

Explanation:

• The keyword do announces (to the Java compiler) that we started an do-statement

• Following the keyword do, you can write (only) one statement

 This is the body of the do-statement The body will be execute once before the LOOP-CONTINUATION-CONDITION is tested. Furthermore, the body will be executed as long as the loop-continuation-condition is true

• The keyword while and a conditional clause ( LOOP-CONTINUATION-CONDITION ) follow the body

 The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition clause of an if-statement) This is the condition of the do-statement

Note:

 The execution of the do-statement can be explained more clearly with a flow chart (discussed in the next topic).

• As usual, the body of a do-statement will usually be a statement block.

A typical do-statement will look like this:

• Flow chart of a do-statement

• Do-statement:

• Flow chart representing a while-statement:

• Execution of the do-statement:

 The computer executes the statements in the body of the do-statement once (without testing the loop-continuation-condition) Then it tests the loop-continuation-condition If loop-continuation-condition is true, the do-statement is repeated (from the beginning) Otherwise, the do-statement is terminated and the execution continues with the statement that follows the do-statement.

• Example:

 ``` a = 1; do { System.out.println(a); a++; } while ( a <= 4 ) System.out.println("Done"); ``` ``` Output: 1 2 3 4 Done ```

Flow chart of this program:

• Structure diagram of a do-statement

• Do-statement:

• Structure diagram representing a do-statement:

• The outer rectangle containing the LOOP-CONTINUATION-CONDITION represents the do-statement:

The do-statement is one (single) statement)

The do-statement will only complete execution when the LOOP-CONTINUATION-CONDITION becomes false

• The inner rectangle (shaded with a green color) is the do-loop body

• Example:

 ``` a = 1; do { System.out.println(a); a++; } while ( a <= 4 ) System.out.println("Done"); ```

Structure diagram of this program:

• Difference in behavior between do-statement and while-statement

• The difference between a do-statement and a while-statement is the first iteration:

 The do-statement will always execute the body once, even if the loop-continuation-condition is false in the first iteration In contrast, if the loop-continuation-condition is false in the first iteration, the while-statement will terminate without executing the body.

• Example:

While-statement Do-statement
```   a = 10;

while ( a <= 4 )
{
System.out.println(a);
a++;
}

System.out.println(a); // prints 10
```
```   a = 10;

do
{
System.out.println(a);
a++;
}
while ( a <= 4 ) ;

System.out.println(a);  // Prints 11
```
• The while-statement exits immediately
• The variable a contains the value 10 (i.e., unchanged) after the while-statement
• The body of the do-statement is executed once
• The variable a contains the value 11 (changed by a++;) after the do-statement

• Re-writing a do-statement using a while-statement

• The do-statement is not necessary in a programming language

• It is possible (but clumpsy) to re-write a do-statement using a while-statement

• How to re-write a do-statement using a while-statement:

Explanation:

 Because the body of the do-statement is executed before testing the loop-continuation-condition, we place a copy of the body before the while-loop This will cause the body to be executed once before testing the loop-continuation-condition The while-statement will executed the body as long as the loop-continuation-condition is true Therefore, we can replaced the do-statement using a while-statement However, we have duplicated the statements in body (which is very clumpsy)

• Break and continue statement used in a do-statement

• The effect of the break and the continue statement used inside a do-statement is the same as inside a while-statement:

• The break statement will cause the do-statement to terminate immediately:

The execution will continue with the statement following the do-statement.

• The continue statement will cause the do-statement to jump to the end of the body

The execution will continue with testing the loop-continuation-condition.

If the condition is true, the do-statement is repeated, and otherwise (if the condition is false), the do-statement is terminated.