Operators are used to cause an operation (or mathematical action) to be performed on one (such as !) or two operands. The easy and common example is 1 + 2 where 1 and 2 are operands, and the + is the operator. This concept can be extended much further with LSL since operands can be variables with the special case of the assignment operators requiring that the left hand side be a variable.

The following table lists the operators in descending order of evaluation, i.e. higher in the table means higher evaluation precedence. Multiple operators on the same line share evaluation precedence. Parenthesize an expression if you need to force an evaluation order.

OperatorDescriptionUsage Example
()parentheses: grouping and evaluation precedenceinteger val = a * (b + c);
[]brackets: list constructorlist lst = [a, 2, "this", 0.01];
(type)typecastingstring message = "The result is:" + (string)result;
! ~ ++ --logical NOT, bitwise NOT, increment, decrementcounter++;
* / %multiply/dot product, divide, modulus/cross productinteger rollover = (count + 1) % 5;
-subtraction, negationinteger one = 3 - 2;

integer neg_one = -1;

+addition, string concatenationinteger two = 1 + 1;

string text = "Hello" + " world";

+list concatenationlist myList = [1, 2, 3] + [4, 5];

list newList = oldList + addList;

<< >>arithmetic left shift, arithmetic right shiftinteger eight = 4 << 1;

integer neg_one = -2 >> 1;

< <= > >=less than, less than or equal to, greater than, greater than or equal tointeger isFalse = (6 <= 4);
== !=comparison: equal, not equalinteger isFalse = ("this" == "that");
&bitwise ANDinteger zero = 4 & 2;

integer four = 4 & 4;

^bitwise XORinteger zero = 4 ^ 4;

integer six = 4 ^ 2;

|bitwise ORinteger four = 4 | 4;

integer six = 4 | 2;

&& ||logical AND, logical ORinteger isFalse = (FALSE && TRUE);

integer isTrue = (FALSE || TRUE);

= += -= *= /= %=assignmentinteger four = 4;

integer eight = four; eight *= 2;

Note: Unlike most other languages that use the C-style && and || operators, both operands are always evaluated. For example,

if (TRUE || 1/0) llSay(PUBLIC_CHANNEL, "Aha!");
will cause a Math Error rather than say "Aha!"

Note: The ++ (increment) and -- (decrement) operators have two versions, pre- and post-. The pre-increment (or pre-decrement) operator increments (or decrements) its operand by 1; the value of the expression is the incremented (or decremented) value. The post-increment (or post-decrement) operator increases (or decreases) the value of its operand by 1, but the value of the expression is the operand's original value prior to the operation.

integer count = 0;
if( ++count == 1 ) // 'count' is incremented then evaluated.
    llSay(PUBLIC_CHANNEL, "Aha"); // message will be said.
integer count = 0;
if( count++ == 1 ) // 'count' is evaluated then incremented.
    llSay(PUBLIC_CHANNEL, "Aha"); // message will not be said.

Note: In most programming languages, and in the LSL bitwise operators, the AND operator has greater precedence than the OR operator. However, the LSL logical operators || and && have the same precedence. For example:

1 || 1 && 0  // result: 0 because it's the same as (1 || 1) && 0
1 | 1 & 0    // result: 1 because it's the same as 1 | (1 & 0)

Note: The order of evaluation is from right to left. If the value of x starts as 1 then the first two conditions below evaluate false and the second two evaluate true:

(x && (x = 0) == 0 && x)
(x && (x = 0) == 0 && x == 0)
(x == 0 && (x = 0) == 0)
(x == 0 && (x = 0) == 0 && x)

Both sides are evaluated regardless of the the truth of either side.

% Modulus

Note: Modulus (%), like division, cause a Script run-time error. Math Error when its second operand equals zero.

Note: The % operator only accepts integer (% as modulus) and vector (% as cross product) operands.

The modulus, also known as Modulo, produces the remainder after the first operand is divided by the second. Mathematically a % b is equivalent to a - (a/b) * b, since integer division a/b is truncated. This is also why the second operand cannot be zero.

The operator can be confusing, even to veteran scripters. The following example(s) may prove useful in understanding how modulus can be used:

if (input % 2 == 0) // input is even
{
    // do even things
}
else // it's odd
{
    // do odd things
}

+ Operator

Note: Equality test on lists does not compare contents, only the length.

result = left + right

Left TypeRight TypeResult TypeDescription
integerintegerintegerAdds left and right
integerfloatfloatAdds left and right
floatintegerfloatAdds left and right
stringstringstringConcatenates right onto the end of left.
list*listConcatenates right onto the end of left.
*listlistAffixes left onto the start of right.
vectorvectorvectorAdds left and right
rotationrotationrotationAdds left and right
Not useful for combining rotations, use * or / instead.

Shorthand Operators

Alternatives to the simple '=' operator...

Simple assignment operatorShorthand operator
a = a + 1a += 1
a = a – 1a -= 1
a = a * (n+1)a *= (n+1)
a = a / (n+1)a /= (n+1)
a = a % ba %= b

De Morgan's laws

Bitwise Equivalencies

ANDOR
~(a & b)~a | ~b
~a & ~b~(a | b)
a & ~b~(~a | b)
~(a & ~b)~a | b

Boolean Equivalencies

ANDOR
!(a && b)!a || !b
!a && !b!(a || b)
a && !b!(!a || b)
!(a && !b)!a || b

Due to De Morgan's laws, by row, code in the AND column is logically equivalent to code in the OR. a and b need not be variables, they can be expressions. In certain circumstances these equivalencies can be used to simplify complex code. It is important not to confuse the two sets when using them. The first two rows depict De Morgan's laws as it is formulated, the second two build upon it.

Useful snippets

Typecasting can also be used if you have to concatenate many parts into a string:

// the following twp statements are equivalent:
string message1 = (string)["I have ", 5, " children at the average age of ", 8.2, " years"];
string message2 = "I have 5 children at the average age of 8.200000 years";