Game Development Reference
In-Depth Information
This is a very compact instruction that increments the counter and draws the sprite at different
positions. This instruction is equivalent to the following
while
instruction:
var i = 0;
while (i < this.lives) {
Canvas2D.drawImage(sprites.lives,
new Vector2(i * sprites.lives.width + 15, 60));
i = i + 1;
}
And here's another example:
for (var i = this.lives - 1; i >=0; i--)
Canvas2D.drawImage(sprites.lives,
new Vector2(i * sprites.lives.width + 15, 60));
To which
while
instruction is this
for
instruction equivalent? Does it make a difference in practice
whether you increase or decrease the counter in this case? Figure
10-3
contains the syntax diagram
of the
for
instruction.
for instruction
for
(
expression
;
,
declaration
expression
;
expression
)
instruction
,
Figure 10-3.
Syntax diagram of the
for
instruction
A Few Special Cases
There are a few special cases that you need to know about when dealing with
while
and
for
loops.
The following subsections discuss these cases.
No Repeat at All
Sometimes the condition in the header of a
while
instruction is already
false
at the beginning.
Look at the following code fragment:
var x = 1;
var y = 0;
while (x < y)
x++;
Search WWH ::
Custom Search