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

LSL Wiki : Boolean

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are


Boolean operators behave like other operators such as +, -, *, / etc except the inputs and output can only take on two values. These values can be represented as TRUE and FALSE or 1 and 0 although any non zero value will generally be treated as TRUE.

Unlike most modern programming languages with optimizing compilers, the boolean operators in LSL do not short-circuit. In C, for example, when using ||, if the condition on the left turns out to be true, the computer does not even evaluate the condition on the right; there is no need because we can already tell that the || will return TRUE. Similar logic works for &&: if the first condition is false, there is no need to evaluate the other condition because the && must return FALSE. This leads to speedups in complicated conditionals.

LSL does not do this, so if you're expecting it, it may make your code less efficient, or worse yet, incorrect if you're expecting a function in the second half of an || or && not to be called.

Also, unlike the most programming languages, LSL evaluates conditions right to left, so in (getVar1() && getVar2()) function getVar2() is run before getVar1().

Boolean Operators

Note: Operators that expect boolean values from integers (TRUE or FALSE) will treat all non-zero values as TRUE.
Operator Name Syntax Description
&& AND var1 && var2
Returns TRUE if var1 and var2 are both TRUE, FALSE otherwise.
var1 and var2 must both be integers.
|| OR var1 || var2
Returns TRUE if either var1 or var2 are TRUE, FALSE otherwise.
var1 and var2 must both be integers.
! NOT !var1 Returns TRUE if var1 is FALSE, or FALSE if var1 is TRUE. var1 must be an integer.

These operators are often used in conditional statements.

Q: How do you have a boolean XOR?
A: Use an if() statement:
if((1 || 0) && !(1 && 0)) //if((they are either T or F) AND NOT(T and F))

Operators | Unary | Binary | Bitwise | Boolean | Equality | Assignment
There are 3 comments on this page. [Display comments/form]