The do-while loop in C++


C++ language contains three different ways of looping structure, do-while structure is one of them. This structure is similar to the while structure except the test condition occurs at the bottom of the loop. This statement can be used to repeat one or more statement in a program.

General Format of do-while loop

  1. do
  2. statement1;
  3. while (expression);

do-while-loop in c++

A statement could either be a single statement or compound statement. In some algorithms, the statement to be repeated must be executed at least once that is if it guarantees that the body of the loop will be executed at least once. C++ language provides this type of control structure called the do-while loop. It is also to be noted that the expression or test condition must be enclosed in parentheses and followed by a semicolon.

The do-while loop is an exit-condition loop. This means that the code must always be executed first and then the expression or test condition is evaluated. If it is true, the code executes the body of the loop again. This process is repeated as long as the expression evaluates to true. if the expression is false, the lop terminates and control transfers to the statement following the do-while loop. It is desirable only if the problem dictates that the code must be done at least once even if the condition fails for the first time itself. ingeneral, the do-while is used less often than while and for loop. However it is the natural loop to use when the condition depends on the result of some calculation performed inside the loop.

Program Example

  1. //program name: dowhile.cpp
  2. //this program adds 1, 2,,….,10 numbers
  3. #include
  4. #include
  5. const MAX  = 10;
  6. int main()
  7. {
  8. clrscr();
  9. int sum=0, num = 1;
  10. do
  11. {
  12. sum = sum + num;
  13. num++;
  14. }
  15. while(num <= MAX);
  16.    cout<<’’SUM = ‘’<
  17. return 0;
  18. }

Run output

SUM = 55

The statement in program dowhile.cpp must be executed at least once. The loop condition is tested after the body of the loop.This is also known as the post-test loop. The initialization is of sum to 0 and num to 1 is done before entering into the loop. The variable that will hold the accumulative value inside the loop must be initialized in this case. The next step is the execution of the condition. The first time  the loop is executed the variable sum declared as intger data type becomes

Sum = 0 + 1;     //sum  = 1


Num  = 1 + 1;    // num = 2


While(2 <= 10)

This is true, so the loop is then repeated the second time. This time the sum becomes 3

i.e; sum  = 1+ 2


num = 2 + 1


While (3 <= 10)

This is true. The process is then repeated until num > max. Once num takes the value of 11, the tested condition becomes false and the loop terminates.

The better example using do-while is illustrated in a program name dorespon.cpp that takes input from the user and insists that the user must input a character in response and repeatedly requesting input until it is correct.

Program Example

  1. //program name: dorespon.cpp
  2. #include
  3. #include
  4. int main()
  5. {
  6. clrscr();
  7. char ch;
  8. do
  9. {
  10.    cout<<’’to continue, enter Y. to exit, enter any Character’’<
  11.    cin>>ch;
  12. }
  13. while (ch == ‘Y’ || ch  ==  ‘y’);
  14. return 0;
  15. }

Comparison of do-while and while loops

The key difference is that since the do-while has its test condition at the bottom of the loop body whereas the while statement has its test condition at the beginning. The brackets are not  necessary in do-while because the keywords ‘do’ and ‘while’ enclose the statements and form a block of code whereas brackets are necessary in a while loop for a block of code. Unlike while loop, it uses a semicolon at the end.

The program name difference.cpp shows how the pre-test and post-test loops work. A variable flag has been used to show the output of a while loop.

Program Example

  1. //program name: difference.cpp
  2. #include
  3. #include
  4. int main()
  5. {
  6. clrscr();
  7. int flag=0, i=40, max=20;
  8. cout<<’’OUTPUT FROM DO-WHILE LOOP\n’’;
  9. do
  10. {
  11.    cout<
  12. }
  13. while(I <= max);
  14.    cout<<’’OUTPUT FROM WHILE LOOP\n’’;
  15. while (i  <=  max)
  16. {
  17.    cout<
  18.    flag  =  1;
  19. }
  20. if (flag  ==  0)
  21.    cout<
  22. return  0;
  23. }

Run output


40 is less than 20


40 is not less than 20

The do-while loop guidelines

  • Executed the body of the loop at least once
  • Evaluate the expression. If true, the program executes the body of the program again. If false, the loop terminates and the next program statement is executed

More tutorials on C++ Loops: