next up previous
Next: break Statement Up: Overview of C++ Previous: Activity

continue Statement

It is possible to force an early iteration of a loop, by passing the loops normal control structure. This is accomplished by using continue. The continue statement forces the next iteration of the loop to takes place, skipping any code between itself and the conditional expression that control the loop. If you put a continue statement in the body of a for or a while loop the computer ignores any statement in the loop that follows the continue. You use the continue statement when data in the body of the loop is bad, out of bounds or unexpected. Instead of acting on the bad data you might want to go back to the top of the loop and get another data value. For example the following program prints the even numbers between 0 and 100

#include <iostream.h>
main()
{
int x;
for(x=0;x<=100;x++)
{
if(x%2!=0) continue; cout << x; }
return 0;
}

Only even numbers are printed because an odd one will cause the loop to iterate early. In while and do while loops a continue statement will cause control to go directly to the conditional expression and then continue the looping process. In the case of the for , the increment part of the loop is performed then the conditional expression is executed and then the loop continues.


next up previous
Next: break Statement Up: Overview of C++ Previous: Activity
Yousef Haik
2/23/1998