Game Development Reference
In-Depth Information
The first
if
statement that contains a mod operator and a relational equals operator tells the loop
to skip all of the even numbers when executing. Our output shows this by printing the current
iteration count.
The second
if
statement tells the loop to
break
once the count reaches 10. The last flow control
statement that we will look at in this chapter is the
goto
.
The goto Statement
The
goto
statement is frowned on by modern C++ developers, so I include it in this chapter merely
for the sake of completeness, so that you are aware of its existence and can work with it in legacy
code.
Note
goto
really is a rare sight in code today. In eight years I have only ever seen it used in a single
game! This Wikipedia entry explains some of the concerns that exist with the
goto
statement:
http://
Listing 6-11 modifies Listing 6-10. This time we use a
goto
to end the loop rather than a
break
.
Listing 6-11. The
goto
Statement
#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)
{
goto finished;
}
} while (true);