continue
The
continue statement allows skipping
whatever code of a loop body is left to
execute at the present iteration and to move to the next iteration. In case of
a
for the middle section (comparison) and the
third section (increment) of the
loop are executed. In the following loop, only numbers between 10 and 20 that
are not multiples of 3 are printed out by skipping to the next iteration if a
number is a multiple of 3:
for(int i = 10; i <= 20; i++)
{
if(!(i%3))
continue; // skip the following code
cout << i << endl;
}
Example
#include <iostream>
using namespace std;
int main()
{
cout << "Numbers between 10 and 20 that are not multiples of 3: \n\n";
for(int i = 10; i <= 20; i++)
{
if(!(i%3))
continue; // skip the following code
cout << i << endl;
}
}
Output
Numbers between 10 and 20 that are not multiples of 3:
10
11
13
14
16
17
19
20