Do while loop is like while loop. It makes the statement is executed repeatedly. But a little bit different.
With do while loop, the condition is evaluated after the statement is executed.
The format of do while loop is:
do statement while (condition);
Sometime we want the program execute the statement repeatedly, so the program will not stop after execute only one statement, as the programs we have made in the previous post.
We can use do while loop for this case.
Look at the example complete syntax below:
#include “iostream.h”
#include “conio.h”
int main ()
{
int x;
do {
cout << "Enter number (0 for stopping): ";
cin >> x;
cout << "You entered: " << x << "\n";
} while (x != 0);
Getch();
}
See the picture below as the result:
The program above ask the user to enter a number, and show it again, then the program repeat that step until we enter number 0.
0 comments:
Post a Comment