---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 9 Edition"
java_version: 9
chapter: "Chapter 14. Blocks and Statements"
section: "14.13"
heading: "14.13. The do Statement"
parent: "Chapter 14. Blocks and Statements"
source: https://docs.oracle.com/javase/specs/jls/se9/html/jls-14.html#jls-14.13
id: "14.13"
url: https://jvm.obfuscated.software/jls/se9/14.13.html
raw: https://jvm.obfuscated.software/jls/se9/14.13.md
---

# 14.13. The `do` Statement

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 `Boolean`, or a compile-time error occurs.

A `do` statement is executed by first executing the *Statement*. Then there is a choice:

- If execution of the *Statement* completes normally, then the *Expression* is evaluated. If the result is of type `Boolean`, it is subject to unboxing conversion ([§5.1.8](5.1.8.md)).

  If evaluation of the *Expression* or the subsequent unboxing conversion (if any) completes abruptly for some reason, the `do` statement completes abruptly for the same reason.

  Otherwise, there is a choice based on the resulting value:

  - If the value is `true`, then the entire `do` statement is executed again.
  - If the value is `false`, no further action is taken and the `do` statement completes normally.

- If execution of the *Statement* completes abruptly, see [§14.13.1](14.13.1.md).

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

**Example 14.13-1. The `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.

## 14.13.1. Abrupt Completion of `do` Statement

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

- If execution of the *Statement* completes abruptly because of a `break` with no label, then no further action is taken and the `do` statement completes normally.

- If execution of the *Statement* completes abruptly because of a `continue` with no label, then the *Expression* is evaluated. Then there is a choice based on the resulting value:

  - If the value is `true`, then the entire `do` statement is executed again.
  - If the value is `false`, no further action is taken and the `do` statement completes normally.

- If execution of the *Statement* completes abruptly because of a `continue` with label `L`, then there is a choice:

  - If the `do` statement has label `L`, then the *Expression* is evaluated. Then there is a choice:

    - If the value of the *Expression* is `true`, then the entire `do` statement is executed again.
    - If the value of the *Expression* is `false`, no further action is taken and the `do` statement completes normally.

  - If the `do` statement does not have label `L`, the `do` statement completes abruptly because of a `continue` with label `L`.

- If execution of the *Statement* completes abruptly for any other reason, the `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 for labeled statements ([§14.7](14.7.md)).
