do while loop in c++
- The last of C++’s loops is the do-while. Unlike the for and the while loops, in which the condition is tested at the top of the loop, the do-while loop checks its condition at the bottom of the loop.
- This means that a do-while loop will always execute at least once.
- Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.
Syntax
do
{
statement(s);
}
while(condition);
Flow Diagram
Example
// divdo.cpp
// demonstrates DO loop
#include using namespace std;
int main()
{
long dividend, divisor;
char ch;
do //start of do loop
{ //do some processing
cout > dividend;
cout > divisor;
cout > ch;
}
while( ch != ‘n’ ); //loop condition
return 0;
}
Output
Enter dividend: 11
Enter divisor: 3
Quotient is 3,
remainder is 2
Do another? (y/n): n