XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.5.RepetitionStructures
Example: Ex_6.SimpleDoWhile

Repetition: Do-While Loops


References:

Goals:



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:


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


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.
  2. 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?
  3. 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?

Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.
2/8/2010