Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

The do-while Statement

do
   statement
   while ( expression ) ;

The do-while statement executes a statement repeatedly until the specified termination condition (the expression) evaluates to zero. The test of the termination condition is made after each execution of the loop; therefore, a do-while loop executes one or more times, depending on the value of the termination expression. The do-while statement can also terminate when a break, goto, or return statement is executed within the statement body.

The expression must have arithmetic or pointer type. Execution proceeds as follows:

  1. The statement body is executed.
  2. Next, expression is evaluated. If expression is false, the do-while statement terminates and control passes to the next statement in the program. If expression is true (nonzero), the process is repeated, beginning with step 1.

The following sample demonstrates the do-while statement:

// do_while_statement.cpp
#include <stdio.h>
int main()
{
   int i = 0;
   do
   {
      printf("\n%d",i++);
   } while (i < 3);
}

See Also

Iteration Statements | C++ Keywords | while (executes zero or more times)

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft