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.
It is important to 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 processed. Remember that the third statement runs after the condition is tested.
In this first example, we have a normal loop which sets an array 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
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 ten, that would mean the senderref was not in our array.
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.
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.
You should now have a good knowledge of loops. ;-)