Game Development Reference
In-Depth Information
Figure 10-2.
The Painter game showing the player the remaining number of lives
A Shorter Notation for Incrementing Counters
Many
while
instructions, especially those that use a counter, have a body that contains an
instruction for incrementing a variable. This can be done with the following instruction:
i = i + 1;
As a side note, especially because of these kinds of instructions, it's unwise to express the
assignment as “is”. The value of
i
can of course never be the same as
i + 1
, but the value of
i
becomes
the old value of
i
, plus 1. These kinds of instructions are very common in programs, so a
special, shorter notation exists that does exactly the same thing:
i++;
The
++
can be expressed as “is incremented.” Because this operator is placed after the variable it
operates on, the
++
operator is called a
postfix operator
. To increment a variable by more than 1,
there is another notation
i += 2;
which means the same as
i = i + 2;
Search WWH ::
Custom Search