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
simple while-statement tutorial
. The difference is that for this tutorial,
we're using a
for loop
, which is a special type of loop that's commonly
used when the program needs to count something.
As you've noticed, a normal while loop has four steps:
The initialization step
The condition-testing step
Executing the body of the loop step
The counting expression step
The parts of the while loop that handle these different parts
are pictured here:
You may have found that it's easy to forget to write out
the various steps. It's easy to forget an initialization step, or to
forget to increment the counter in the counting expression step. Because
counting loops are very, very common, C# (and many other languages) have a loop
that makes it harder to forget any of the important parts of a counting loop.
The
for
loop puts the initialization, condition, and counting expression on the
same line as the start of the loop. With everything in one place, it's
much more difficult to forget anything:
You'll notice that the exact same parts are being used for
both loops (
counter++
is identical to
counter = counter + 1;
in this
situation). In fact, there's a simple, mechanical way to transform a
counting while loop into a for loop, as the following picture shows:
By following the arrows 'backwards', one can also
transform a for loop back into a counting while loop.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
UpdateWorld():
float
xOffset =
0.0f;
// here is the for loop
for
(
int
counter = 0; counter < 3; counter++)
{
CreateABallAt(xOffset +
BALL_INIT_X,
"SoccerBall"
);
xOffset = xOffset +
SPACE_BETWEEN_BALLS;
}
Let's examine the details of how the
for loop
operates by tracing through
it's execution in detail
While it's not technically part of the loop itself, the following line creates a
variable named
xOffset
, and initializes it with the value zero.
Let's summarize the variables (and their values) in the following table:
float
xOffset = 0.0f;
Variable Name
Value
xOffset
0.0f
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 (
int
counter = 0;
counter < 3;
counter++)
Variable Name
Value
xOffset
0.0f
counter
0
Next, the first iteration of the loop begins:
At the start of each iteration of the loop, the condition is checked:
for (int
counter = 0;
counter < 3
;
counter++)
This asks "Is the current value of counter (which is
currently zero) strictly less
than 3?" This is true, so the program will execute the body of the
loop.
The body of the loop consists of the following lines of code:
CreateABallAt(xOffset + BALL_INIT_X,
"SoccerBall"
);
xOffset = xOffset + SPACE_BETWEEN_BALLS;
Once the body of the loop has executed, there will be a new ball
(located at (BALL_INIT_X, BALL_Y_POS),
aka (10.0f, 30.0f), because xOffset has the value 0.0f)
Once the body of the loop has finished executing, the function's variables
will have the following values:
Variable Name
Value
counter
0
xOffset
15.0f
After the body of the loop has finished executing, then the counting
expression is executed
for (int
counter = 0; counter < 3;
counter++
)
which increments the value stored in counter by one:
Variable Name
Value
counter
1
xOffset
15.0f
The second iteration of the loop begins by checking the condition
At the start of each iteration of the loop, the condition is checked:
for (int
counter = 0;
counter < 3;
counter++)
This asks "Is the current value of counter (which is
currently one) strictly less
than 3?" This is true, so the program will execute the body of the
loop.
The body of the loop consists of the following lines of code:
CreateABallAt(xOffset + BALL_INIT_X,
"SoccerBall"
);
xOffset = xOffset + SPACE_BETWEEN_BALLS;
Once the body of the loop has executed, there will be a new
ball centered at (25.0f, 30.0f), because xOffset has the value 15.0f).
After that, the value of xOffset will be increased by SPACE_BETWEEN_BALLS
again.
Once the body of the loop has finished executing, the function's variables
will have the following values:
Variable Name
Value
counter
0
xOffset
30.0f
After the body of the loop has finished executing, then the counting
expression is executed
for (int
counter = 0; counter < 3;
counter++
)
which increments the value stored in counter by one:
Variable Name
Value
counter
2
xOffset
15.0f
The third (and final) iteration of the loop begins by again
checking the condition
At the start of each iteration of the loop, the condition is checked:
for (int
counter = 0;
counter < 3;
counter++)
This asks "Is the current value of counter (which is
currently two) strictly less
than 3?" This is true, so the program will execute the body of the
loop.
The body of the loop consists of the following lines of code:
CreateABallAt(xOffset + BALL_INIT_X,
"SoccerBall"
);
xOffset = xOffset + SPACE_BETWEEN_BALLS;
Once the body of the loop has executed, there will be a new
ball centered at (40.0f, 30.0f), because xOffset has the value 30.0f).
After that, the value of xOffset will be increased by SPACE_BETWEEN_BALLS
again.
Once the body of the loop has finished executing, the function's variables
will have the following values:
Variable Name
Value
counter
0
xOffset
45.0f
After the body of the loop has finished executing, then the counting
expression is executed
for (int
counter = 0; counter < 3;
counter++
)
which increments the value stored in counter by one:
Variable Name
Value
counter
3
xOffset
15.0f
After the third iteration, 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 (int
counter = 0;
counter < 3;
counter++)
This asks "Is the current value of counter (which is
three) strictly less
than 3?" This is NOT true, so the program will NOT execute the
body of the loop, but will instead proceed on to the first line of code
below the loop (in this case, that's "
m_BasketBall.CenterX
+= GamePad.ThumbSticks.Right.X;
")
It's extremely important to emphasize this point: the loop ends
after the program has done it's final iteration of the loop. And
since counter is incremented in the loop,
counter will have the value of 3,
NOT 2
, after the loop has
ended!!
FURTHER EXERCISES::
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.
Familiarizing Yourself With Loops: Practice
For this exercise, you should use the same project that was explained in the
above tutorial.
Try modifying the program so that the program will draw five soccer balls on
the screen, rather than three. Can you draw 2, instead of 5 (or 10)?
Familiarizing Yourself With Loops: Experimenting With The Loop
For this exercise, you should use the same project that was explained in the
above tutorial.
Try setting the condition on the loop to be "counter < 0", and observe what
happens. Explain your observations.
Familiarizing Yourself With Loops: Missing Initialization
For this
exercise, you should use the same project that was explained in the above
tutorial. Try removing the initialization step of the loop, so that your
for loop looks like:
for
(
;
counter < 3
;
counter++ )
and observe what happens. How does this help you to remember the
initialization step?
Familiarizing Yourself With Loops: Infinite Loops
For this exercise, you should use the same project that was explained in the
above tutorial. Try removing the counting expression, so that your for loop
looks like:
for
(
int
counter = 0
;
counter < 3
;
)
and observe what happens. Next, think about the code, and explain what
you're seeing.
Hint:
If UpdateWorld never ends, then the Update step in the
Great Draw->Update Cycle
never finishes - what do you think will happen
to the cycle?
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. Don't worry if the soccer balls
leave the screen.
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. Don't
worry if the soccer balls leave the screen.