9/17/2008
9/17/2008
9/17/2008
9/17/2008
The do-while Statement
int sum = 0, number = 1;
do {
sum += number;
number++;
} while ( sum <= 1000000 );
These statements are executed as long as sum is less than or equal to 1,000,000.
Here's an example of the second type of repetition statement called do-while. This sample code computes the sum of integers starting from 1 until the sum becomes greater than 1,000,000. The main difference between the while and do-while is the relative placement of the test. The test occurs before the loop body for the while statement, and the text occurs after the loop body for the do-while statement. Because of this characteristic, the loop body of a while statement is executed 0 or more times, while the loop body of the do-while statement is executed 1 or more times.

In general, the while statement is more frequently used than the do–while statement.