XNA Game-Themed CS1 Examples ( XGC1 )

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

Topic: Topic.5.RepetitionStructures
Example: Ex_9.SimpleFor

Repetition: "For" 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:


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: 

  1. The initialization step

  2. The condition-testing step

  3. Executing the body of the loop step

  4. 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


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. 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)? 
  3. 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.
  4. 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?

  5. 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?

  6. 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.
  7. 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.

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