Loops


Although there are variations, a loop is basically a block of code that is run continuously while a condition is true. In this text, I'll explain 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.

Remember the order in which the loop's statements are processed. First the variable assignment is performed. This is only done once. Next, the condition is tested. If the condition is true, then the code block (or a single statement) is run. After the code block is run, the third statement of the loop is executed. Remember that the third statement runs after the condition is tested.

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

for(i=0; i < 5; i=i+1)
{
      var[i]=-1;
}
Or:
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 internally as in the following example:

for(i=0; player[i] != GetSenderRef() && i < 10; i=i+1);
That loop was not used to run a statement or block of code. We needed the loop to return the number of the player in our array. When the loop finished, i should have been a number like 5. That would mean that player[5] in our array of players was the senderref.

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 senderref was not in our array. This is an advanced technique, and it doesn't matter if it doesn't make that much sense now.

The above description barely covers the for loop which is by far the most versatile. For more information, look up the for keyword in the Keywords Section.

The While Loop

The while loop runs a statement or block of code as long as a condition is true. For example:
i=0;
while(i != 21)
{
     var[i]=-1;
     i=i+1;
}
As you can see, 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 as in the following example:
findThing=FirstThingInView(player, 180, 5, 0xfff);
while(findThing != -1)
{
     SetThingUserData(findThing, 1);
     findThing=NextThingInView();
}
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. This 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.
do
{
    // run code
}
while(condition);
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);
Notice that because the code to run is given before the condition, the while() statement ends with a semicolon.

Continue