do while loop in C plus plus

Do while loop in C++

Do while loop in C++ is different from the for loop and the while loop, in that the test_expression is evaluated at the end of the loop rather than when the loop is entered. For practical purposes, this means that the do while loop is guaranteed to execute at least once. The do while loop is rare compared to the for and the while loop.
The syntax of the do while loop is as follows:
do
{
// block of statements
}
while (test_expression);
For e.g., the sum program can be rewritten as follows:
Int sumSoFar = 0;
Int i=0;
Do
{
sumSoFar = sumSoFar +i; // This is the body of the do while loop
}
while(I <= 100);

The while loop is called as pre-test loop and do while loop is called as post-test loop. Because, the while loop tests the condition first and then enters the loop, but do while first enters the body and then tests the condition.

Flow-chart of do- while loop in C++

flow-chart-of-do-while-loop-in-C++
flow-chart-of-do-while-loop-in-C++

Leave a Reply

Your email address will not be published.