Example of break (used to break out of the immediate loop). It will execute 100 times then break out of the while loop.
Code:
int i =1;
while (i > 0)
{
cout << "Loop=" << i << endl;
++i;
if (i > 100)
break;
}
cout << "Done" << endl;
Output:
Loop=1
Loop=2
...
...
Loop=100
Done
Now example of continue, which says go back to start of loop and go on with execution:
Code:
int i =1;
while (i > 0)
{
if (i % 2)
{
// Odd number, increment and resume the while
++i;
continue;
};
cout << "Loop=" << i << endl;
++i;
if (i > 100)
break;
}
cout << "Done" << endl;
Output:
Loop=2
Loop=4
Loop=6
...
...
Loop=100
Done
DISCLAMER: The code is not written in the most efficient way, but done so to make it easier to read.