jeffheaton's picture

    If/else statements allow the script to make decisions. These decisions are usually based on variables. An if statement will execute a block of code if the statement evaluates to true. An else statement can be used in conjunction with the if statement. If the if statement evaluates to false, the if statement's else statement will be executed. Not all if statements will have an else statement. The else statement is optional.

    The next section will show how to use if statements.

Understanding If-Statements

    Consider a simple script that will say hello to any avatar that says hello to the object containing the script. Such a script is shown in Listing 3.1.

Listing 3.1: A Hello Script

default
{
    state_entry()
    {
        llListen(0, "", NULL_KEY, "");
    }
    
    listen(integer channel, string name, key id, string message) 
    {
        if( llToUpper(message) == "HELLO" )
        {
            llSay(0,"Hello " + name );
        }
    }
}

    The state_entry event is called first. This event uses the llListen function call to request that the listen event be called whenever something is said near the object that contains the script. The first parameter to llListen specifies that the script should listen on channel zero. The second parameter specifies the avatar to listen for. Because an empty string is specified, the script will listen to all avatars. The third parameter allows an avatar to be specified by key. The fourth parameter specifies the message desired. If a value is specified for the fourth parameter, only messages that exactly match that value will be received.

    Every time that something is said near the script, the listen event will be called. The listen event uses an if statement to look for the world “hello”. Notice how the if statement uses the llToUpper function. This allows the script to be able to respond for “Hello”, “hello”, “HELLO” or any other combination of capital and lower case letters. Also notice the double equal (==). This specifies that this is a comparison. A single equal (=) is only used to assign a value to a variable.

    An if statement can also be used with numbers. For example, to check to see whether the variable a is equal to 10, the following statement would be used.

if( a == 10 )
{
  llSay(0,"a is 10");
}

    It is also possible to use comparison operators other than equal. To see whether the variable a is not equal to ten, the following code is used.

if( a != 10 )
{
  llSay(0,"a is not 10");
}

    Greater than and less than can also be used. The following code checks to see whether the variable a is greater than ten.

if( a > 10 )
{
  llSay(0,"a is greater than 10");
}

    Greater than or equal can be used as well. The following code checks to see whether the variable a is greater than or equal to ten.

if( a >= 10 )
{
  llSay(0,"a is greater than or equal to 10");
}

    It is also possible to use boolean logic in an if statement. This will be explained in the next section.

Using Boolean Logic

    Boolean logic can also be used with an if statement. The and (&&) and or(||) operators allow two comparisons to be used in an if statement. For example to check whether the variable a is equal to 5 or 6, the following code could be used.

if( a==5 || a==6 )
{
  llSay(0,"a is 5 or 6");
}

    The or statement requires only one half of the expression to be true. If the variable a is equal to 5 or the variable a is equal to 6, then the if statement will execute.

    The and operator is much more selective. The following if statement checks to see whether the variable a is greater than 5, yet less than 10.

if( a>5 && a<10 )
{
  llSay(0,"a is greater than 5 and less than 10");
}

    For this if statement to execute, the variable must be greater than five, as well as less than ten. If just one of these requirements is not met, the if statement will not execute.

Understanding Else-Statements

    Else statements can also be used in conjunction with if statements. Consider the following script segment.

if( a==5 )
{
  llSay(0,"a is equal to 5");
}
else
{
  llSay(0,"a is not equal to 5");
}

    The else statement follows the if statement. If the if statement does not execute, the else statement will execute. It is illegal to have an else statement without a corresponding if statement. Unless some sort of error occurs, it is impossible to make it through an if/else statement without executing either the if statement's block of code, or the else statement's block of code.

    It is also possible to use an else if statement. This is a combination of the else and if statements, as seen below.

if( a==5 )
{
  llSay(0,"a is equal to 5");
}
else if (a==6 ) 
{
  llSay(0,"a is equal to 6");
}

    The above code contains two if statements. If a is not equal to five, then the second if statement is checked. If a is not equal to six, nothing happens. Any number of if/else statements can be strung together in this way. It is possible to create lengthy if/else ladders in this way. It is also possible to attach a final else statement to the ladder to execute if none of the above if statements executed. The following code demonstrates this.

if( a==1 )
{
  llSay(0,"a is equal to 1");
}
else if (a==2 ) 
{
  llSay(0,"a is equal to 2");
}
else if (a==3 ) 
{
  llSay(0,"a is equal to 3");
}
else  
{
  llSay(0,"a is not equal to 1, 2 or 3");
}

    The code above will execute one of the if statements if the variable is equal to one, two or three. The final else statement will be executed if none of the above three if statements match. The above technique is quite common in programming. Often the variable will be compared against a list of numbers. The Linden Scripting Language contains a special statement just for this situation. The switch and case statements can be used to better represent the above code. The switch and case statements are explained in the next section.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.