Loops
In our last example, it displayed the first argument sent to the game: the name of the game. The name of our game can be
helpful to know, but of more importance are any arguments following the name. A common program convention is to enter the
name followed by a -? for instructions on how to use the program. Right now if you entered demo -? you'd get
the same results as if you just entered demo. Multiple command line arguments are separated by blank spaces.
To see if there was an additional argument entered, we could add the line echo($Game::argv[1]); to our
parseArgs function. But what if there were 2 arguments entered, or 3? And if we do add that line, if the user
doesn't enter any argument, we still get output to the console.
This is where loops come in. What loops allow us to do is to run a statement repeatedly based on a condition. The condition
is a specific comparision that identifies when to stop repetition. As you may recall, in addition to the arguments coming
to our game in the array argv[], we also receive a count of the arguments in the variable argc.
We can use this variable to run a loop that many times, echoing out only the number arguments that are entered.
For a set number of repetitions, the for loop is used. The for loop requires an initialization
variable, a comparision condition, and an update expression. When the command is started the initialization variable is set.
If the comparision is true then the body of the loop is executed. Then the update expression is executed and the comparision
condition is once again checked. This processes is repeated until the comparision condition is not true, and the loop is exited.
// main.cs 7 APR 05
function onExit() {
}
function parseArgs() {
for (%i=0; %i < $Game::argc; %i = %i + 1) {
echo($Game::argv[%i]);
}
}
enableWinConsole(true);
parseArgs();
quit();
Now in our function, observe the for loop. Within the parentheses the first item is the initialization variable.
In this case we are setting the variable i to 0. Since the variable i is only being used within this
function for this purpose as a simple counter, it is identified with local scope with the percent sign, and is named simply. It
starts with 0 because, as we mentioned before, arrays start with 0.
Next is the comparision condition. If the value stored in the placeholder %i is less than the value in the
placeholder $Game::argc, which is the number of command line arguments, then we can execute the statements
within the block following. The last item is the update expression. One is added to the value in %i and the
result is stored in the variable %i. There is a shortcut operator used to increment a value by one, and it is
simply ++, so %i = %i + 1 is the same as %i++.
Since there is always one argument, if we run our game without any extra arguments, $Game::argc equals 1. On our
first comparision %i equals 0, so it is true that it is less than $Game::argc and our for
block is executed.
Instead of a literal number such as 0 as the index for our variable $Game::argv[] we use the variable
%i, which is a placeholder for 0 in this instance. After displaying the first argument, the update expression
is evaluated, making our variable %i equal 1. Then the comparision to $Game::argc is not true, since
1 is not less than 1, so our for block is exited and our game continues.
Try out this game with multiple different arguments and see what results you get.

