XNA Game-Themed CS1 Examples (XGC1) Release 2.0 (XNA V3.1) 2/8/2010
Topic: Topic.5.RepetitionStructures
Example: Ex_14.CountBackwards

# Repetition: Counting Down Using A For Loop

References:

• Pre-requisite: it is assumed that you have read through the prior tutorials, and are familiar with the concepts covered in those tutorials.

Goals:

• In this tutorial, we will:
• Examine a simple for loop, which is used to repeat some action a large number of times

1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

When the game starts, you'll see a screen that looks similar to this:

The program for this tutorial is almost exactly identical to the program used in the the previous tutorial.  The difference is that for this tutorial, we're using a for loop to count down rather than up (to count backwards rather than forwards)

2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen

• UpdateWorld(): // here is the for loop     for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)     {         float radian = ComputeRadianFromXPos(xPos);         yPos = ComputeSineYPos(radian) ;         CreateABallAt(xPos, yPos, "SoccerBall");     }
• Let's examine the details of how the for loop operates by tracing through the execution of the first couple of iterations in detail
1. Just like we did for the normal while loop, the initialization part of the for loop is executed exactly once.  It is always executed, and it it is executed before the first check of the condition:
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)
 Variable Name Value xPos 100.0f
2. Next, the first iteration of the loop begins:
1. At the start of each iteration of the loop, the condition is checked:
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)

This asks "Is the current value of xPos (which is 100.0f) greater than (or equal to) 0.0f?"  This is true, so the program will execute the body of the loop.

2. The body of the loop consists of the following lines of code:

CreateABallAt(xPos, yPos, "SoccerBall");

The above code will place a soccer ball at the appropriate place along the sine wave.

3. After the body of the loop has finished executing, then the counting expression is executed
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)

which decrementsthe value stored in counter by one:

 Variable Name Value xPos 99.0f
• Next, the second iteration of the loop begins:
1. At the start of each iteration of the loop, the condition is checked:
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)

This asks "Is the current value of xPos (which is 99.0f) greater than (or equal to) 0.0f?"  This is true, so the program will execute the body of the loop.

2. The body of the loop consists of the following lines of code:

CreateABallAt(xPos, yPos, "SoccerBall");

The above code will place a soccer ball at the appropriate place along the sine wave.

3. After the body of the loop has finished executing, then the counting expression is executed
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)

which decrementsthe value stored in counter by one:

 Variable Name Value xPos 98.0f
• The loop continues to iterate through many, many more iterations.  We will not be tracing through all the iterations here.

• The loop continues to iterate.  The final iteration looks like this:

1. At the start of each iteration of the loop, the condition is checked:
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)

This asks "Is the current value of xPos (which is now 0.0f, in the final iteration of the loop) greater than (or equal to) 0.0f?"  This is true (because they're equal), so the program will execute the body of the loop.

2. The body of the loop consists of the following lines of code:

CreateABallAt(xPos, yPos, "SoccerBall");

The above code will place a soccer ball at the appropriate place along the sine wave.

3. After the body of the loop has finished executing, then the counting expression is executed
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)

which decrementsthe value stored in counter by one:

 Variable Name Value xPos -1.0f
• The program goes back to check the condition
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)

and asks "Is the current value of xPos (which is now -1.0f) greater than (or equal to) 0.0f?"  This is NOT true, so the program will NOT execute the body of the loop.

FURTHER EXERCISES::

1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.
• Repeat this exercise daily for several days, so that you really get the hang of this.  As you go on, periodically review this by re-doing this exercise.
2. Go back through the previous tutorials, and re-implement the code, using loops that count down, rather than up.  Your instructor may have a specific list of tutorials (and exercises from those tutorials) that you will implement.