3. Other Loop Constructions
C# also provides two other loop constructions, called do – while and while.
These are not actually vital, in that you can always get the looping
behavior that you want by using an appropriately designed for loop, but they can be useful in situations where you don’t want to go to the trouble of creating a for loop construction. You can find out more about these kinds of loops and when they would be useful in the glossary in the do – while entry.
4. Fun with for Loops
You can test your understanding of the for loop behavior by looking at some for loops and trying to work out what they would do. For instance, look at this one:
for (layer = 0 ; layer > 4 ; layer++)
There’s a mistake in this statement, but it’s rather hard to spot. The mistake is that the test is now layer > 4. The > character means "greater than." This means that the test is now true only when the value of layer is greater than 4. Because the initialization sets the value of layer
to 0, this condition is never true. The result is that the code in the
statement controlled by the loop is never performed. Now look at this
statement:
for (layer = 0 ; layer < 4 ; layer--)
There’s another mistake here. The less-than character (<) is in the correct place, but rather than increasing the value of layer each time around, the change makes layer smaller by using the -- operator each time. This means that the value of layer never becomes greater than 4, so the loop never ends. The result is that your program appears to "get stuck" at this point.
You can write code to request this as follows if you really want a loop that goes on forever:
for (layer = 0 ; true ; layer--)
Simply putting the value true
in the position of the condition causes the loop to never stop. If
you’re wondering what would happen if you ran a loop like this, you can
try it if you like, but I can save you the trouble. If you run either of
these never-ending loops, you eventually get the message shown in Figure 5. This is the message that XNA displays when it runs out of memory.
The reason you get this message is that each time the DrawString
method is called in the body of the loop, it uses a small amount of
memory to record what is drawn. If you call the method a large number of
times, it eventually uses up all the memory available for this purpose,
and the memory allocation part of XNA throws an exception when it is
asked for memory it doesn’t have. The good news is that this doesn’t
cause any damage, but it does cause serious damage to your credibility.
One of the nice things
about loops is that you can get a lot more work done by the computer
simply by changing the values that cause them to stop. For instance,
look at this code:
for (layer = 0; layer < 40 ; layer++)
This
version of the loop draws 40 red time values before putting the yellow
one on top. It gives rise to the rather funky display shown in Figure 6.
This is nice, but you can
do even better. You can make the display even more funky by using some
other drawing tricks that XNA provides.