Using Loops
Loops allow a script to execute a block of code a certain number of times. Like many programming languages, the Linden Scripting Language supports three different types of loop. These loop types are summarized here.
- While Loops
- Do/While Loops
- For Loops
The next three sections will introduce these loop types.
While Loops
The most common loop type in the Linden Scripting Language is the while loop. A while statement looks very similar to an if statement. However, a while statement will execute the block of code as long as the specified expression is true. If the specified expression is not true, the while statement will not execute. It is important to note that a while statement can execute zero times. If the expression specified in the while is not true from the beginning, the while statement will not execute even once. The script shown in Listing 3.2 demonstrates a while loop.
Listing 3.2: A While Loop
default
{
touch_start(integer total_number)
{
integer a = 1;
while( a<=10 )
{
llSay(0, "Counting " + (string)a );
a++;
}
}
}
The variable a is defined to hold the number one. The while loop begins by ensuring that a is less than or equal to ten. If a is less than or equal to ten, the loop will continue. The value of a is displayed and a is incremented. The loop continues.
It is also easy to count backwards in a loop. Simply reverse a few things and the above loop will count backwards. This can be seen in Listing 3.3.
Listing 3.3: A While Loop Counts Backwards
default
{
touch_start(integer total_number)
{
integer a = 10;
while( a>=1 )
{
llSay(0, "Counting " + (string)a );
a--;
}
}
}
Notice how the above loop now initializes the variable to 10. The while loop now checks to be sure that the variable is still greater than one. Finally, each time through the loop, the variable is decremented, as opposed to incremented.
Do/While Loops
If the expression is false from the beginning, the while loop cannot be guaranteed to execute. The do/while loop differs from the while loop in this very important way. The do/while loop is guaranteed to execute at least once. This is because a while loop makes its decision at the beginning of the loop. The do/while loop makes this decision at the end of the loop. This can be seen in Listing 3.4.
Listing 3.4: A Do/While Loop
default
{
touch_start(integer total_number)
{
integer a = 1;
do
{
llSay(0, "Counting " + (string)a );
a++;
} while( a<=10 );
}
}
In the above script, the variable is initialized to one. The do/while loop begins and displays the value of the variable. The variable is incremented. Next the while, at the end of the loop, checks to see whether the variable is still less than or equal to ten. If the variable is less than or equal to ten, the loop continues.
This loop performs the same task as the while loop in the previous section. You may be wondering why it matters to make the decision at the end. Consider the following script that illustrates this. Listing 3.5 illustrates this.
Listing 3.5: A Do/While Loop That Executes Once
default
{
touch_start(integer total_number)
{
integer a = 20;
do
{
llSay(0, "Counting " + (string)a );
a++;
} while( a<=10 );
}
}
The above script sets the variable a to 20. This number is too high for the loop to use. The while loop from the previous section would simply fail to execute and nothing would be displayed. However, the above script is guaranteed to execute at least once. This would cause the above loop to simply display one line, the value 20.
For Loops
The third loop type, the for loop, can be very useful when you now the exact range that is to be looped over. For a simple loop to count between one and ten, the for loop is the loop of choice. Consider the following loop which counts between one and ten. Listing 3.6 illustrates this.
Listing 3.6: A For Loop
default
{
touch_start(integer total_number)
{
integer a;
for(a = 1; a<=10; a++ )
{
llSay(0, "Counting " + (string)a );
}
}
}
As can be seen from the above code, the for loop has three parts. The first part initializes the variable to one. The second, or middle part, of the for loop works like a while loop. As long as this expression is true, the loop will continue. The third, and final part of the for loop, is the action that should take place each iteration through the loop. For this simple loop, the variable is simply incremented.




