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 preceding 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 or pointer type.