LSL Wiki Mirror 10-5-2006: Boolean

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings ::

Boolean

Boolean operators behave like a function call that accepts two parameters and returns either TRUE or FALSE, except in the case of !, which is a boolean operator that works with only one parameter.

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 doesn't 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.

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.
> Greater then var1 > var2
Returns TRUE if var1 is greater then var2, FALSE otherwise.
var1 and var2 must be either an integer or a float.
< Less then var1 < var2 Returns TRUE if var1 is less then var2, FALSE otherwise.
var1 and var2 must be either an integer or a float.
>= Greater then or equal to var1 >= var2
Returns TRUE if var1 is greater then or equal to var2, FALSE otherwise.
var1 and var2 must be an integer or a float.
<= Less then or equal to var1 <= var2
Returns TRUE if var1 is less then or equal to var2, FALSE otherwise.
var1 and var2 must be an integer or a float.
!= Not equal to var1 != var2
Returns TRUE if var1 is not equal to var2, FALSE otherwise.
var1 and var2 can be any type.
== Equal to var1 == var2
Returns TRUE if var1 is equal to var2, FALSE otherwise.
var1 and var2 can be any type.
Note:

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 and F or T and T) AND (are NOT T and T))


Types | Operators
There are 3 comments on this page. [Display comments/form]