Game Development Reference
In-Depth Information
infinite loop because of a programming error. Consider the following example:
int x=1;
int n=0;
while (n < 10)
x=x
2;
n = n+1;
The intention of this code is that the value of x is doubled ten times. However,
unfortunately the programmer forgot to put the two instructions in the body between
braces. This intention is suggested by the layout of the program, but the compiler
does not care about that. Only the x=x 2; instruction is repeated, so the value of n
will then never be greater or equal to ten. After the while -instruction, the instruction
n=n+1 will be executed, but the program never gets there. What the programmer
actually meant was:
int x=1;
int n=0;
while (n < 10)
{
2;
n = n+1;
x=x
}
It would be a pity if you had to throw away your computer or console after it was
put into a coma because you forgot to write braces around your while -instruction.
Fortunately, the operating system can stop the execution of a program by force, even
if it has not finished. The program is stopped immediately in that case, and you can
start to look for the cause of the program hanging. Consoles generally do not have
this kind of process management, so you will have to restart the console the old-
fashioned way. Although such hang-ups occur occasionally, it is your job as a game
programmer to make sure that once the game is sold to the customers, these kinds
of programming errors have been removed from the game code. This is why proper
testing is so important.
In general, if the program you wrote does not seem to do anything on startup, or
if it hangs indefinitely, check out what is happening in the while -instructions. A very
common mistake is to forget incrementing the counter variable, so the condition
of the while -instruction never becomes false , and the while -loop continues indefi-
nitely.
9.5.3 Nested Repeats
The body of a while -instruction or a for -instruction is also an instruction itself. This
instruction can be an assignment, a method call, or a block of instructions delimited
Search WWH ::




Custom Search