Game Development Reference
In-Depth Information
The break statement can be used to exit any of the loops we have covered in this chapter. If we had
a for loop and had a condition where we would like to stop execution, then we would be able to
use break , and the same goes for both versions of the while loop. C++ also provides the continue
keyword to control loops.
The break and continue Keywords
Listing 6-10 shows an example loop that contains both the continue and break statements.
Listing 6-10. Using continue and break
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
unsigned int array[10];
unsigned int count = 0;
do
{
if ((count % 2) == 0)
{
++count;
continue;
}
array[count] = count;
cout << "Loop Iteration: " << array[count++] << endl;
if (count == 10)
{
break;
}
} while (true);
return 0;
}
The output from our program is the following:
Loop Iteration: 1
Loop Iteration: 3
Loop Iteration: 5
Loop Iteration: 7
Loop Iteration: 9
 
Search WWH ::




Custom Search