Repeating Code: For and While loops

Okay, we know how to do different things in different circumstances with 'if', now we're going on to doing the same thing over and over again. Sounds a bit strange to want to do the same thing many times, and if we couldn't change what was stored in variables it would be. However we can, and the most common use is to run the same piece of code over several different values by changing what is stored in a variable.

For

The first way to do this is a 'for' loop. You can use for to run over each element in a list, or over every value in a range of numbers or object numbers.

It looks like:
for variable in (list)
  ...code...
endfor

For example:
for element in ({#2, #98, #100, #605, #1356})
  player:tell(element, " ", element:title());
endfor

The example above would first assign the value #2 to the variable element, then tell the player the value stored in element, followed by a space, and then its title. It then hits the endfor line, so goes back to the top to look for a new value for element. When it gets there, it finds another value after #2 - #98. It assigns this to element and does the same thing. After it has worked its way past #1356 it will continue on with any code that follows the endfor line.

The output of such a verb might be something like:
#2 Mort
#98 Crystal
#100 Generic Character Class
#605 Kass
#1356 Loki Entertainment

Another For

It's slightly different to run over values in a range:
for variable in [start .. end]
   ... code ...
endfor

Start and End are the beginning and end of the range of values that you want to loop over. Obviously they have to be the same type of value; for value in [1..#5] will generate an error (E_TYPE). Trying to have the beginning higher than the end is permissable, but the code inside the for loop will never get used.

So for a similar example:
for element in [#0..#1000]
  player:tell(element, " ", element:title());
endfor

The above would do the same as the first snippet, but instead of the 5 selected objects, it would try to do it on every object between #0 and #1000. Very spammy!

While

A less often used way to repeat code, is a 'while' loop. Instead of having a certain set of values which will change with every iteration (ie every time the code loops back to the top) and stop when it runs out of values, while will keep looping back until it generates a false value inside its brackets.

It looks like:
while (true-or-false-test)
  ... code ...
endwhile

Until the test is false (like an if test is true or false), the code up until the endwhile line will be run over and over again. If the test is false to begin with, the code inside the loop isn't called at all. An example will hopefully make this a little clearer - like with if the true/false test, it's most useful to be testing a variable.

count = 1;
while ( count <= 5 )
   player:tell(count, "...");
  count = count +1;
endwhile

The first line creates a variable named count. Our while loop starts in the second line, it will keep running while count is less than or equal to 5. The next line tells the player the number stored in count followed by 3 dots. Line 4 adds one to the value in count (ie it sets the value of count to the current value of count + 1). The last line says that this is the end of the while loop, go back to the top and start again. When count gets incremented up to 6, the test count <= 5 is false, so the loop stops.

This would look like:
1...
2...
3...
4...
5...

Each of the three are good in different situations, but don't worry about having to chose which one to use; if it's not obvious which one would be the best, then it probably doesn't matter. :)


Last modified: Wed Sep 15 23:41:01 GMT 1999