You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.
This shows you the differences between two versions of the page.
types:boolean [2015-01-08 14:59 SLT] sei created |
types:boolean [2015-02-04 18:54 SLT] (current) sei style, remove Boolean operator from See Also as we don't know what to do about that yet |
||
---|---|---|---|
Line 2: | Line 2: | ||
===== Boolean ===== | ===== Boolean ===== | ||
- | LSL does not support a boolean type per se; instead, its function is performed by an [[types/integer]]. When a variable or parameter is a boolean, it means that it's an integer which represents a truth value of [[constants/FALSE]] when it is zero, or [[constants/TRUE]] when it is not zero. | + | LSL does not support a boolean type per se; instead, its function is performed by an $lty[integer]. When a variable or parameter is a boolean, it means that it's an integer which represents a truth value of //false// when it is zero, or //true// when it is not zero. |
- | Note that the constant [[constants/TRUE]] has the value 1, but //any// value different to 0 will serve as a truth value of //true// for variables that represent a boolean value. | + | Note that the constant $lct[TRUE] has the value 1, but //any// value different to 0 (including negatives) will serve as a truth value of //true// for variables that represent a boolean value. |
- | It is not recommended to test boolean values against [[constants/TRUE]] or [[constants/FALSE]]. In most cases, that's not necessary. When the variables are properly named, they usually can be better read by omitting such comparisons. Consider the following example: | + | It is not recommended to test boolean values against $lct[TRUE] or $lct[FALSE]. In most cases, that's not necessary. When the variables are properly named, they usually can be better read by omitting such comparisons. Consider the following example: |
- | <code lsl2> | + | <file lsl2 boolexample1.lsl> |
integer flag = FALSE; | integer flag = FALSE; | ||
Line 26: | Line 26: | ||
} | } | ||
} | } | ||
- | </code> | + | </file> |
- | And now compare it with this more readable version: | + | And now compare it with this version: |
- | <code lsl2> | + | <file lsl2 boolexample2.lsl> |
integer rotating; | integer rotating; | ||
Line 48: | Line 48: | ||
} | } | ||
} | } | ||
- | </code> | + | </file> |
- | The latter version is more compact, more readable, faster, and uses less memory. In short, it's better in all respects. The trick for readability is to decide the name of the variable depending on what it represents when the truth value is TRUE; in the latter example, when the variable is true it will represent that the object is rotating, thus the choice ''rotating''. | + | The latter version is more compact, more readable, faster, and uses less memory. In short, it's better in all respects. It works, because when the argument to an $lkw[/if] statement is an integer, any value distinct to 0 will be taken as true. The trick for readability is to decide the name of the variable depending on what it represents when the truth value is //true//; in the latter example, when the variable is true it will represent that the object is rotating, thus the choice ''rotating'' for the variable name. |
==== See also ==== | ==== See also ==== | ||
- | * [[constants/integer]] type | + | * $lty[integer] type |
- | * [[operators/Boolean]] operators | + | * $lkw[if] statement |
- | * [[language/if]] statement | + |