11 out of 30 rated this helpful - Rate this topic

The do-while Statement (C++)

Executes a statement repeatedly until the specified termination condition (the expression) evaluates to zero.


do
      statement
      while ( expression ) ;

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_s("\n%d",i++);
    } while (i < 3);
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
while loop
Please help answer this. Customer have two account, the variable name are bankAcc1 and bankAcc2, and the variable for the balance are balance1, and balance2, now I need to write a program that use the while loop in which a customer can not have negative balance for each of the account, and the bankAcc1 and bankAcc2 can not hold the same bank account number.
I have to write a corret condition for the while loop... please help
Explanation
The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time.
Explanation
The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time
Explanation
The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time.
Explanation
The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time.