do Statement
do
statement
while(expression);
The do keyword executes statement repeatedly until expression becomes false (0).
Example
// Example of the do-while statement
do
{
y = f( x );
x--;
} while ( x > 0 );
In this do-while statement, the two statements y = f( x );
and x--;
are executed, regardless of the initial value of x
. Then x > 0
is evaluated. If x
is greater than 0, the statement body is executed again and x > 0
is reevaluated. The statement body is executed repeatedly as long as x
remains greater than 0. Execution of the do-while statement terminates when x
becomes 0 or negative. The body of the loop is executed at least once.