Don't click here unless you want to be banned.

LSL Wiki : for

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ec2-204-236-235-245.compute-1.amazonaws.com

for Loop


The for loop is used to execute statements while a condition is true, and executes a line of code after each iteration/cycle/step. More commonly, it executes code a certain number of times, and operates a counter to facilitate it.

Format:
for (initialization; test; update)
{
    statements
}

initialization and update are any single LSL statements. test is any LSL statement that returns an integer.

Example:
integer x;
list things = ["apple", "venus", "Volume 1"];
integer length = llGetListLength(things);

for (x = 0; x < length; x++)
{
    llSay(0, llList2String(things, x));
}
This code says every element of the list things in order.

To execute statements max number of times, with x going from 0 to max - 1, use:
for (x = 0; x < max; x++)

To execute statements max number of times, with x going from 1to max, use:
for (x = 1; x <= max; x++)

To execute statements max number of times, with x going from max to 1, use:
for (x = max; x >= 1; x--)

Note: the test statement is executed every iteration of the loop, so if there is some value that does not change while the loop is running, don't do this:
for (x = 0; x < unchangingCalc(); x++)

Instead, do this:
integer calc = unchangingCalc();
for (x = 0; x < calc; x++)

Any for loop can be re-written as a while loop like this:
initialization
while (test)
{
	statements
	update
}

So, the initialization is always executed, then the test is evaluated, and if it is FALSE, the statements and the update are never executed.

For loops and while loops are syntatically equivalent (one can be converted to the other and the result will be exactly the same) but it is traditional to use for loops to run a block of statements a set number of times (for example, 10 times, once for every item in a list, or once for every result from a sensor) and to use while loops to execute statements until some condition is no longer true (for example, waiting for something).

Since the statements that make up a for loop can be arbitrary LSL, there is a huge temptation to try and be clever; for example, by rewriting the above example as:

Bad Example:
integer x;
list things = ["apple", "venus", "Volume 1"];

for (x = 0; x < llGetListLength(things); llSay(0, llList2String(things, x++)))
{
    ;
}

Do not give in to this temptation!!! It makes code harder to read, gains nothing and makes experienced scripters cry. Additionally, the function llGetListLength is needlessly called once for every element in the list.

Better Example:
integer x;
list things = ["apple", "venus", "Volume 1"];
integer length = llGetListLength(things);

for (x = 0; x < length; x++)
{
    llSay(0, llList2String(things, x));
}

Very Bad Example:
list things = ["apple", "venus", "Volume 1"];
integer x;
for (x=-llGetListLength(things); x; llSay(0, llList2String(things, x++)));


Flow Control
There are 3 comments on this page. [Display comments/form]