The continue statement forces immediate transfer of control to the loop-continuation statement of the smallest enclosing loop. (The “loop-continuation” is the statement that contains the controlling expression for the loop.) Therefore, the continue statement can appear only in the dependent statement of an iteration statement (although it may be the sole statement in that statement). In a for loop, execution of a continue statement causes expression2 to be evaluated, then expression1.
The following example shows how the continue statement can be used to bypass sections of code and skip to the next iteration of a loop:
// Get a character that is a member of the zero-terminated
// string, szLegalString. Return the index of the character
// entered.
int GetLegalChar( char *szLegalString )
{
char *pch;
do
{
char ch = getch();
// Use strchr library function to determine if the
// character read is in the string. If not, use the
// continue statement to bypass the rest of the
// statements in the loop.
if( (pch = strchr( szLegalString, ch )) == NULL )
continue;
// A character that was in the string szLegalString
// was entered. Return its index.
return (pch - szLegalString);
// The continue statement transfers control to here.
} while( 1 );
return 0;
}