# User:Toady Nakamura/Sandbox

Jump to: navigation, search

# Operators

## Arithmatic Operators

LSL has arithmetic operators which can be used with integers, floats, vectors and rotations.

The order of operation is

``` result = left + right
```

The symbols, definition and examples of the operators:

```=  ... denotes equality ... 1 = 1
+  ... addition         ... 1 + 2 = 3
-  ... subtraction      ... 3 - 2 = 1
*  ... multiplication   ... 2 * 3 = 6
/  ... division         ... 6 / 3 = 2
%  ... remainder operator ... (see explanation below)
```

Since integers are whole numbers, if you divide 15/2 the answer is not a whole number and if you did that in a script the extra *remainder* is discarded as the answer is rounded toward zero. If you need the remainder, use the *remainder operator*, written as % to get what was otherwise lost.

This script snippet shows how the remainder operator works <lsl>

```integer i;
i = 15 % 2;     // 15/2 = 7.5; 0.5 + 0.5 (for the two halves which were discarded) =1;
// the remainder i is assigned the value 1
i = 15 % 4;     // 15/4 = 12; 15-12=3;
//  the remainder i is assigned the value 3
i = -15 % 4;    // i is assigned the value -3
```

// remember to round toward zero not just "round down"! </lsl>

Vector data contains a set of three float values. In LSL adding vectors to vectors is done <x,y,z> + <x1,y1,z1> = the combined vector. The constant ZERO_VECTOR is equal to <0,0,0>. You can get to individual parts of a vector as shown in this snippet:

<lsl> vector vec; float x = vec.x; float y = vec.y; float z = vec.z; </lsl>

To combine rotations, you use the multiply and divide operators. Multiply * applies the rotation in the positive direction, the divide / does a negative rotation. The constant ZERO_ROTATION equals <0.0, 0.0, 0.0, 1.0>. Read the official wiki page for more on Rotations

### Assignment using Arithmatic operators

There is a handy shorthand for adding repetitive values as seen in this snippet: <lsl> integer x; integer y;

```x  +=  y;    //  means    x = x + y;  ... the new value of x is equal to x + y
x  -=  y;    //  means    x = x - y;  ... the new value of x is equal to x - y
x  *=  y;    //  means    x = x * y;  ... the new value of x is equal to x * y
x  /=  y;    //  means    x = x / y;  ... the new value of x is equal to x / y
```

</lsl>

### Signed integers

Example: The plus integer (ex.: +1) is generally assumed. "It was -3 degrees today!" uses a negatively signed integer.

### Comparison Operators

The comparison operators seek to determine equality (TRUE, value 1) or inequality (FALSE, value 0).

``` !=  ...  not equal               ... 3 != 2 + 2
<   ...  less than               ... 3 < 4
>   ...  greater than            ... 4 > 3
<=  ...  less than or equal      ... 2 <= 3
>=  ...  greater than or equal   ... 3 >= 2
```

The result of a comparison operator is either 0 (FALSE) or 1 (TRUE).

### Unary Operator

Because counting up or down by 1 is so common, a special *unary operator* has been created.

``` ++ ... count up by one
-- ... count down by one
```

There are two ways to use the unary operators, but be careful, they are not identical. <lsl>

```integer i = 0;
i++;     // i now has the value of 1 - because i++ means " evaluate then increment "
// contributes its value before incrementation
++i;     // i now has the value of 2 -- because ++i means " increment then evaluate "
```

</lsl>

When used as part of a larger expression, i++ evaluates (values itself) and then increments but ++i counts up first, then adds its own value.

<lsl> *** NEEDS BETTER EXAMPLE >>>>> this has if/else and we havent' explained it yet

```integer i = 0;
if (i++ == 0)  // Is evaluated then incremented.
llSay( 0, "The value of i was 0 when the comparison was done" )
// Will be said even though i ends up with the value of 1.
else
llSay( 0, "The value of i was 1 when the comparison was done" )
```

</lsl> On the other hand, if we switch i++ to ++i, i.e. <lsl>

```integer i = 0;
if (++i == 0) // Is incremented then evaluated.
llSay( 0, "The value of i was 0 when the comparison was done" ) // Will not be said.
else
llSay( 0, "The value of i was 1 when the comparison was done" )
```

</lsl> the chat output would be "The value of i was 1 when the comparison was done".

The operator minus-minus (written --) works similarly.

>>>> NEED BETTER EXAMPLE HERE

See for a list of all LSL operators and for a generalized discussion about Operators.

### Cautions

• If you try to divide (/) or remainder (%) anything by zero, you will get a run-time error and script fail.
• If any arithmatic returns a value outside the range −2147483648 to 2147483647, different rules apply.

A good explanation can be foundhere.

• Hexadecimal is rarely used in LSL, if you desire to learn more about it, click here .

## Truth Values (Booleans)

Truth values (Booleans) are either 0 (FALSE) or 1 (TRUE).

### Truth Value Operators

``` !  ... means "not"
&& ... means "and"
|| ... means "or"
```

These snippets show some of the ways statements evaluate to TRUE or FALSE <lsl> //The ! operator takes a single operand and evaluates to TRUE if the operand is false, and FALSE otherwise.

```integer i;
i = !(3 == 7);     // 3 == 7 is FALSE, so i gets the value TRUE
```

//The && operator takes two operands and evaluates to TRUE if both its operands are true, and FALSE otherwise.

```i = (3 < 7) && (3 > 7);     // 3 < 7 is TRUE, but (3 > 7) is FALSE, so i gets the value FALSE
```

//The || operator takes two operands and evaluates to TRUE if either one of its operands are true, and FALSE otherwise.

```i = (3 < 7) || (3 > 7);     // one of the operands (3 < 7) is TRUE, so i gets the value TRUE
```

</lsl>

Like the binary arithmetic operators, && and || have a corresponding assignment type operator.

``` x &&= y;    // Synonymous with x = x && y;
x ||= y;    // Synonymous with x = x || y;
```

### Statements Requiring Truth Values

There are four statement types that require a truth value condition:

• if
• for
• while
• do-while

There are three operators which look similar but have different meanings.

``` =  ... assignment          ... "equal to"
== ... test for equality   ... "exactly equal to"
!= ... test for inequality ... "not equal to"
```

#### if

An "if-then-else" command does a test (written inside parentheses), and if that test is TRUE, then the command will do something {written in brackets}, else the command will do something else - or nothing - when the test is FALSE. The command is formatted like these snippets:

<lsl> if (test)

```    {do this}
```

else

```    {do that}
```

</lsl> You can leave off the "else" part if there's nothing for it to do, instead write <lsl>

```if (test)
{do this}
```

</lsl>

Here's an example of how to set an integer as a switch, you can think of it as an iSwitch, and this script shows you how to use it.

<lsl> integer i = FALSE; // defined as off globally // and the value of off/on to FALSE (aka i=0;)

```             // notice one * = * equals sign above, because the value is being set
```

default {

```    state_entry()
{
llParticleSystem([ ]); // starts with particles off
}
touch_start(integer total_number)
{
if (i==FALSE)  // notice two *==* because this is testing for
// exact equality
{
llParticleSystem([
PSYS_PART_FLAGS,
PSYS_PART_WIND_MASK | PSYS_PART_EMISSIVE_MASK,
PSYS_SRC_PATTERN, PSYS_SRC_PATTERN_EXPLODE,
PSYS_PART_START_COLOR, <1,0,0>
]);
i=TRUE; // flip the iSwitch to the other setting & wait for next touch
// notice one * = * because the value is being set
}
else   // if the touch happened when particles were on
{
llParticleSystem([ ]); // turn particles off
i=FALSE; // flip the iSwitch to the off position & wait for next touch
// notice one * = * because the value is being set
}
}
```

} </lsl>

<lsl> integer Counter; // define an integer to hold a value later default {

```    state_entry()
{
llSay(0, "..I can count to myself, up to 10!..");
}
```
```    touch_start(integer total_number)
{
for (Counter=1; Counter<=10; ++Counter) //**
{
llOwnerSay((string)Counter);
}
}
llOwnerSay("That wasn't so hard!");
```

} // ** This line contains the instructions for how many times to do this loop // First ... set initial value ... Counter = 1; // Second ... do the loop if the counter is less than or equal to 10 ... Counter <=10 // Third ... after each loop, add 1 to Counter ... ++Counter. </lsl>

These examples were originally on (http://wintermute.linguistics.ucla.edu/lsl/loop.html Ed Boost) which is unfortunately a dead link.

#### for

In programming, a "test" is something that comes out True or False (Yes or No and/or 1 & 0 ).