The do Statement

The do statement executes a statement repeatedly until the specified termination condition (the expression) evaluates to zero. The test of the termination condition is made after each execution of the loop; therefore, a do loop executes one or more times, depending on the value of the termination expression. The following function uses the do statement to wait for the user to press a specific key:

void WaitKey( char ASCIICode )

{

char chTemp;

do

{

chTemp = getch();

}

while( chTemp != ASCIICode );

}

A do loop rather than a while loop is used in the above code—with the do loop, the getch function is called to get a keystroke before the termination condition is evaluated. This function can be written using a while loop, but not as concisely:

void WaitKey( char ASCIICode )

{

char chTemp;

chTemp = getch();

while( chTemp != ASCIICode )

{

chTemp = getch();

}

}

The expression must be of an integral type, a pointer type, or a class type with an unambiguous conversion to an integral type.