do Keyword

The do keyword is a part of do-while statement.

do-while Statement

The do-while statement lets you repeat a statement or compound statement until a specified expression becomes false.

Syntax

do statement while ( expression ) ;

The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once.

The expression must have numeric or boolean type. Execution proceeds as follows:

  1. The statement body is executed.

  2. Next, expression is evaluated. If expression is false, the do-while statement terminates and control passes to the next statement in the program. If expression is true (nonzero), the process is repeated, beginning with step 1.

This is an example of the do-while statement:

x=100;
do 
{
    y = sin( x );
    x--;
} while ( x > 0 );

In this do-while statement, the two statements y = sin( 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.