C++ Do-While Loop

The do-while is another type of loop supported by C++ language. In the case of while loop and for loop – three expressions are compulsory. This is also true for do-while, but in a different way.

do-while Loop Structure

The do part allow the code block to execute first and then the while part perform a test for terminating the loop. The loop terminates when the test condition is false.

The general structure for do-while is given below.




         do something;



Graphical Representation of do-while

The graphical representation of do-while is useful in making flowcharts for C++ programs.

do-while Loop Diagram
do-while Loop Diagram

Example Program: do-while

//do-while loop

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
    //Variable decalaration
    int sum;
    int value;
    sum = 0;
    value = 10;
    //Do-while to compute the results
               sum = sum + value;
    }while(value >= 0);
    //printing outputs
    cout << "Sum = " << " " << sum << endl;
    return EXIT_SUCCESS;


Sum = 55

Please support us by disabling your adblocker or whitelist this site from your adblocker. Thanks!

turn of adblocker imag