The do statement

A do statement repeatedly runs a statement until the test expression evaluates to false (or 0 in C). Because of the order of processing, the statement is run at least once.

Read syntax diagramSkip visual syntax diagramdo statement syntax
 
>>-do--statement--while--(--expression--)--;-------------------><
 

C The expression must be of arithmetic or pointer type. C++The controlling expression must be convertible to type bool.

The body of the loop is run before the controlling while clause is evaluated. Further processing of the do statement depends on the value of the while clause. If the while clause does not evaluate to false, the statement runs again. When the while clause evaluates to false, the statement ends.

A break, return, or goto statement can cause the processing of a do statement to end, even when the while clause does not evaluate to false.

C++A throw expression also can cause a while statement to end prior to the condition being evaluated.

The following example keeps incrementing i while i is less than 5:

#include <stdio.h>

int main(void) {
  int i = 0;
  do {
    i++;
    printf("Value of i: %d\n", i);
  }
  while (i < 5);
  return 0;
}

The following is the output of the above example:

Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5