How To Cog - Chapter 2.6 - Loops

Although there are variations, a loop is basically a block of code that is run continuously while a condition is true. This part of the chapter will show you how to use loops in Cog. Using loops can be dangerous without safeguards. If the loop's condition is always true and nothing happens to make it false, the loop will continue indefinitely. So it's a good practice to include safeguards in your loops.

The For Loop

The for loop looks like this:

for(variable assignment; condition; variable incrementation)
    // run code

The for loop consists of three statements which are seperated by two semicolons. The first part is where the counting variable is assigned a value. In the second part, the true/false condition is defined. In the third statement, another variable assignment is done. This assignment usually increments the counting variable by one.

When a loop is started, the variable assignment is performed first. This is only done once. Next, the condition is tested. If the condition is true, then the code block (or single statement) is run. After the code block is run, the third statement of the loop is executed.

In example, a normal loop sets an array (covered in the next section) of five variables to -1.

for(i=0; i < 5; i=i+1)


  for(i=0; i < 5; i=i+1) var[i]=-1;

First, i is set to 0. Next the condition is tested. If i is less than 5, the code will run and var[i] will be set to -1. After var[i] has been assigned, i is incremented by one. Then it "loops" and tests the condition again. The loop continues until i is equal to five. Then the loop terminates. Our example loop ran five times. If we had used i <= 5, then the loop would have run one more time.

For loops can also run without a following statement as in this example:

  for(i=0; player[i] != GetSenderRef() && i < 10; i=i+1);

This loop was not used to run a statement or block of code. The loop returns the number of a player in an array. When the loop finished, i should have been a number like 5. That would mean player[5] in our array of players was the sender of a message.

If i reached 10 and the the loop had not yet found a match for the senderref, the loop would terminate because i has to be less than 10 for the condition to be true. So if the loop finished leaving i at 10, that would mean the sender was not in our array.

The above description barely covers the for loop which is by far the most versatile.

The While Loop

The while loop runs a statement or block of code as long as a condition is true. For example:

while(i != 21)

The while loop can be made to do the same tasks as the for loop, but the while loop does not have the built-in counting variable. The while loop is best used in situations like this:

findThing=FirstThingInView(player, 180, 5, 0xfff);
while(findThing != -1)
     SetThingUserData(findThing, 1);

First, findThing is set to a value other than negative one. This value should be a thing somewhere near the player. Because findThing is not equal to -1, the loop begins. The loop sets the userdata of findThing to one. Then findThing is assigned to the next thing in view of the player. If there was another thing in view, findThing will not be equal to -1 and the while loop will continue. The while loop will set the userdata of all things in view of the player to 1.

When there are no more things in view, NextThingInView() will return -1. findThing will then be equal to -1 and the condition that the while loop tests will be false. When the condition is false, the loop ends.

The Do..While Loop

The do..while loop is designed to run a block of code at least once regardless of the loop's condition.

    // run code

The block of code is run before JK checks the condition. If the condition is false, the loop ends. But if the condition is true, the loop will continue until the condition is false.

Like other conditional statements, do..while can run a single statement or a code block. For example:

  do var1=var1+1;
  while(var1 < var2);

Because the code to run is given before the condition, the while() statement ends with a semicolon.

Previous: Conditions Up: How To Cog Next: Arrays
  • Create:
This page was last modified 15:48, 14 April 2006.   This page has been accessed 1,560 times.