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

# Repetition: A Non-Counting 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 how to use a for loop that isn't used just for counting

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 demonstrates how to use the for loop.  This is nearly identical to the previous tutorial, except that instead of using one variable (counter) to keep track of how many soccer balls to draw, and a second variable (xOffset) to keep track of where to put the next soccer ball, we will instead just use a single variable.  By keeping track of xOffset, and defining a new named constant (FINAL_X_POSITION) that defines where to stop putting soccer balls, this program can use the pattern of a for loop, yet not use a traditional 'counting loop' pattern.

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 xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)     {         CreateABallAt(xOffset + BALL_INIT_X, "SoccerBall");     }
• Let's examine the details of how the for loop operates by tracing through it's execution 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 xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
 Variable Name Value xOffset 0.0f
2. The first iteration of the loop begins:
1. At the start of each iteration of the loop, the condition is checked:
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)

This asks "Is the current value of xOffset (which is zero) strictly less than 75?"  This is true, so the program will execute the body of the loop.

• You'll notice that FINAL_X_POSITION is defined to be 5 * SPACE_BETWEEN_BALLS, and that SPACE_BETWEEN_BALLS is defined to be 15.0f.  Therefore, the numeric valuke of FINAL_X_POSITION = 75.0f ( which is 5 * 15.0f)
2. The body of the loop consists of the following lines of code:
CreateABallAt(xOffset + BALL_INIT_X, "SoccerBall");
Once the body of the loop has executed, there will be a new ball located at (10.0f, 30.0f).
3. After the body of the loop has finished executing, then the counting expression is executed
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)

which increases xOffset by the value of SPACE_BETWEEN_BALLS:

 Variable Name Value xOffset 15.0f
3. The second iteration of the loop begins by checking the condition
1. At the start of each iteration of the loop, the condition is checked:
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
This asks "Is the current value of xOffset (which is 15.0f) strictly less than 75?"  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(xOffset + BALL_INIT_X, "SoccerBall");
Once the body of the loop has executed, there will be a new ball located at (25.0f, 30.0f).
3. After the body of the loop has finished executing, then the counting expression is executed
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
which increases xOffset by the value of SPACE_BETWEEN_BALLS:
 Variable Name Value xOffset 30.0f
4. The third  iteration of the loop begins by checking the condition
1. At the start of each iteration of the loop, the condition is checked:
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
This asks "Is the current value of xOffset (which is 30.0f) strictly less than 75?"  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(xOffset + BALL_INIT_X, "SoccerBall");
Once the body of the loop has executed, there will be a new ball located at (40.0f, 30.0f).
3. After the body of the loop has finished executing, then the counting expression is executed
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
which increases xOffset by the value of SPACE_BETWEEN_BALLS:
 Variable Name Value xOffset 45.0f
5. The fourth  iteration of the loop begins by checking the condition.
1. At the start of each iteration of the loop, the condition is checked:
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
This asks "Is the current value of xOffset (which is 45.0f) strictly less than 75?"  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(xOffset + BALL_INIT_X, "SoccerBall");
Once the body of the loop has executed, there will be a new ball located at (55.0f, 30.0f).
3. After the body of the loop has finished executing, then the counting expression is executed
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
which increases xOffset by the value of SPACE_BETWEEN_BALLS:
 Variable Name Value xOffset 60.0f
6. The fifth and final iteration of the loop begins by checking the condition
1. At the start of each iteration of the loop, the condition is checked:
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
This asks "Is the current value of xOffset (which is 60.0f) strictly less than 75?"  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(xOffset + BALL_INIT_X, "SoccerBall");
Once the body of the loop has executed, there will be a new ball located at (70.0f, 30.0f).
3. After the body of the loop has finished executing, then the counting expression is executed
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
which increases xOffset by the value of SPACE_BETWEEN_BALLS:
 Variable Name Value xOffset 75.0f
7. After the fifth iteration through the loop, the program again checks the condition, to see if it should repeat the loop again:
• At the start of each iteration of the loop, the condition is checked:
for (float xOffset = 0.0f; xOffset < FINAL_X_POSITION; xOffset += SPACE_BETWEEN_BALLS)
This asks "Is the current value of xOffset (which is 75.0f) strictly less than 75?"  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. Moving The Soccer Balls
For this exercise, you should use the same project that was explained in the above tutorial.
The goal for this exercise is to 'animate' the soccer balls, so that all the soccer balls slowly move rightwards across the screen.
Modify the provided solution so that your program contains another instance variable, initialized with a value like 0.0f.  Each time UpdateWorld is called, make sure that you first add, say, 5.0f to the instance variable, and then when you create the soccer balls, you add this number to the 'X' value of the new soccer balls' location.
Note that you need to keep all five soccer balls on the screen - you're not allowed to have them disappear as you move them rightwards.
3. Moving The Soccer Balls
For this exercise, you should use the same project that was explained in the above tutorial.
The goal for this exercise is to 'animate' the soccer balls, so that all the soccer balls slowly move leftwards across the screen.
Note that you need to keep only five soccer balls on the screen - you're not allowed to have more appear as you move them leftwards.