The do ... while Statement
The Next statement we are going to focus on will be the do...while statement. The do...while statement is almost the same as the while statement. Its syntax is:
do statement while (condition);
The only difference is that the do...while statement executes the statement first and then evaluates the condition. These two steps are repeated until the condition evaluates to zero (false). A do...while loop will always loop at least once, regardless of the value of the condition, because the statement executes before the condition is evaluated the first time.
I'll rewrite the above code but use the do...while instead of the while statement.
 | |  | | | #include <iostream.h>
void main()
{
int n;
do
{
cout <<"Enter a number to be cubed, terminate with 0: ";
cin >> n;
cout << n <<" cubed is: " << n*n*n << "n";
} while( n > 0 );
} |
|  |
|
This programs output is the exact same as the while statement. The only difference is that this program will always iterate at least once through the code before checking the condition.Please Rate this Code: