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 move something on the screen in a sinusoidal, "wavy"
motion, using a do-while loop. A do-while loop is very similar
to the while loops that you've already seen, but different enough that you
should examine it separately from normal while loops
1. Obtain the example code
Here is the zip file to the source
files and compiled executable of this example.
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:
If you press the 'A' button, then the game draws an
additional soccer ball on the screen. If you press the 'B' button, then
the game draws one fewer soccer balls on the screen.
The program uses a do-while loop to draw the soccer balls.
A do-while loop is almost exactly like a while loop, except that the do-while
loop always executes the loop at least once. It may execute the
loop only once, but it will never execute zero times. This is done by moving the condition
from the top of the loop, to the bottom, like so:
You'll notice that the do-while loop in the blue box has
all the same parts that the normal while loop does: the body of the loop in the
green box, the condition for the loop in the yellow box, and the counting
expression in the purple box. While not
technically part of the loop, the initialization code in the pink box may be
necessary for the correct operation of the do-while loop, as it is in this
particular example. The execution of
a do-while loop is very similar to the execution of a normal while loop, except
that because the condition is located at the bottom of the loop,
The above picture should help make it clear how the loop
is executed. C# will execute the initialization code (if any - this is the
top, blue arrow), and then will ALWAYS EXECUTE THE BODY OF THE LOOP THE FIRST TIME
(the middle blue arrow). After that first execution of the body of the
loop, C# will then check to see if the condition is true, and if it is,
C# will repeat the loop (the pink arrow). If the condition is not true, then the
program will execute the next line after the loop (the green arrow).
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
InitializeWorld():
Since we only draw soccer balls on the screen, there is no m_BasketBall,
therefore there is no need to initialize it.
Instead, we just copy the named constant that defines how many soccer
balls to initially draw on the screen into the instance variable that tracks
how many soccer balls we should currently be drawing on the screen:
m_NumSoccerBalls =
NUM_STARTING_SOCCER_BALLS;
UpdateWorld():
if (GamePad.ButtonAClicked())
m_NumSoccerBalls++;
if (GamePad.ButtonBClicked())
m_NumSoccerBalls--;
#region
Here is a simple do-while loop
RemoveAllFromDrawSet();
// remove all previously showing balls ...
int i = 0;
The new code in the UpdateWorld function can be grouped up into three
categories, based on what the code accomplishes:
Incrementing or decrementing the number of soccer balls based on user
input
if
(GamePad.ButtonAClicked())
m_NumSoccerBalls++;
if
(GamePad.ButtonBClicked())
m_NumSoccerBalls--;
Printing the two messages to the top/bottom of the status bar:
String
msg = "m_NumSoccerBalls: "
+ m_NumSoccerBalls;
EchoToTopStatus(msg);
EchoToBottomStatus(
"Do
while is very similar to While!");
Removing all the prior soccer balls & creating new ones
RemoveAllFromDrawSet();
// remove all previously
showing balls ...
int i = 0;
float
xOffset = 0.0f;
// here is
the do-while loop
do
{
CreateABallAt(xOffset + BALL_INIT_X,
"SoccerBall");
i = i + 1;
xOffset = xOffset + SPACE_BETWEEN_BALLS;
}
while
(i < m_NumSoccerBalls);
Since you've previously seen code that's either identical to, or else
very similar to, the first two categories, we won't discuss them further,
but will instead examine the third category by tracing through a couple
iterations of the loop.
Let's examine the details of how the loop operates by tracing through
several iterations of the loop, in detail.
While it's not technically part of the loop itself, the initialization
code creates two variables, and initializes them:
int i = 0;
float
xOffset = 0.0f;
Variable Name
Value
i
0
xOffset
0.0f
Next, the program executes the loop for the first time. Notice
that at this point, we haven't yet checked the condition - we're going to
execute the loop this first time no matter whether the condition is true,
or false.
CreateABallAt(xOffset + BALL_INIT_X,
"SoccerBall");
i = i + 1;
xOffset = xOffset + SPACE_BETWEEN_BALLS;
Once the body of the loop has finished, a ball has been created at
(10.0f, 30.0f) and the local variables have the
following values:
Variable Name
Value
i
1
xOffset
15.0f
Next, the loop's condition is tested:
while
(i < m_NumSoccerBalls);
Because i has the value
1,and m_NumSoccerBalls was initialized to 5, and because 1 is strictly
less than 5, this evaluates to true. Thus,
C# follows the pink arrow back to the top of
the body, and executes the body again.
Next, the program executes the loop for the second time.
Once the body of the loop has finished, another ball has been created
at (25.0f, 30.0f) and the variables have the
following values:
Variable Name
Value
i
2
xOffset
30.0f
Next, the loop's condition is tested:
while
(i < m_NumSoccerBalls);
Because i has the value
2,and m_NumSoccerBalls was initialized to 5, and because 2 is strictly
less than 5, this evaluates to true. Thus,
C# follows the pink arrow back to the top of
the body, and executes the body again.
The program continues to execute the loop, for a total of five
iterations.
The primary advantage of using a do-while loop is that the do-while loop
MUST execute the first time, meaning that even if i has a value like
zero (or -1, or -20), the program will still display (at least) that first
soccer ball. Contrast this with the normal while loop, wherein the
loop may execute zero times, one time, or many times.
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.
Examining the Do-While loop: Explaining The Program's Behavior
For this exercise, you should use the same project that was explained in
the above tutorial.
Start the game up, and use the B button to set m_NumSoccerBalls to be 0, or
a negative number. How many soccer balls are displayed on the screen?
Why?
Examining the Do-While loop: Contrasting Do-While and While loops
For this exercise, you should use the same project that was explained in
the above tutorial.
Change the loop that creates the soccer balls to use a normal while loop,
but don't change anything else (i.e., don't change the condition, etc).
Next, run the game, and use the B button to set m_NumSoccerBalls to 0, or a
negative number. How many soccer balls are displayed on the screen?
Why is this different from the do-while loop?