Iteration Structures

The eternal question: "Where is my next instruction?" Think of code as a stream. The sequential structure flows smoothly down. The decision structure might branch the stream, or skip further downstream. The iteration structure, uniquely, has the potential to flow upstream.

An iteration structure, or loop, is used when you want a block of code to run more than once. For instance, let's say we want the ball from the previous examples to bounce up and down exactly ten times.

The basic looping structure is the while statement.

While Statements

The syntax for the while statement is this:

while (condition)

Before executing the while statement, the condition will be tested. If the condition is false, the while statement will never execute, and the program will skip to the line after the while statement -- just like a one-branch if statement.

If the condition is true, the statements inside are executed. After the last statement, the condition is checked again. If it is false, the next statement is after the while statement. If it is still true, the statements inside all execute again.

Important: The statements inside the while loop must, somewhere, do something that will potentially change the condition. If your while statement does nothing that will ever change the condition, it will execute forever -- an infinite loop.

Note: Many times, statements must be placed above the while loop that initialize or "prime" the loop, especially counter loops. Our example below is a counter loop.

With all of the above information in mind, we can write our while loop. By now, you should know how to define a "bounce this none none" verb and program it.

n = 0;
while (n < 10)
  player.location:announce_all("The ball bounces up and down.");
  n = n + 1;

Note that we must prime the loop by creating a new integer variable called "n" and setting it to zero. Thus when the loop begins, we know that "n < 10" is true and that the loop will execute at least once. Our loop simply sends a message to everyone at the player's location, then increments n. We then return to the condition and check again -- it will still be true until n equals 10. At that time we will exit the loop and execute the statement following it -- or exit the verb, in this case, since there are no statements following it.

Note: It is common practice to put loop maintenance statements, like incrementing counter variables, at the end of the loop.

The while statement is the only loop structure we really need. However, there is another looping structure specifically designed for counting loops and stepping through MOOlists. We'll look at the latter function later; for now, let's redo our counting while loop using a for statement.

For Statements -- Counting

The syntax for a counting for loop is:

for variable in [start..end]

All variables above must be integers. If end > start, then the loop is guaranteed to execute (end - start) times. If start == end, then the loop will execute exactly once. Otherwise, if end < start, the loop will never execute.

Here's our example redone:

for n in [1..10]
  player.location:announce_all("The ball bounces up and down.");

Note that the for loop does not have to be primed, nor do we need a loop maintenance statement -- all of this is contained in the structure of the for loop. This eliminates a lot of potential for error.

Also, though it wasn't used in our example, n is available inside the for loop and is incremented at each iteration, and will be available after the for loop as a variable set to end. If you want to see this in action, in a more advanced verb, use the program below:

for n in [1..10]
  player.location:announce_all("The ball bounces up and down for the ", $string_utils:english_ordinal(n), " time.");