Difference between revisions of "Boolean constants"
m (moved infromation not pertinent to constants to Discussion tab) |
BryanDerksen (Talk | contribs) (condensing AxeMurderer's contributions to hit the key points more strongly, and distributing to appropriate sections.) |
||
Line 1: | Line 1: | ||
− | |||
{{Constant table start | {{Constant table start | ||
|sourcefile=script.ldf | |sourcefile=script.ldf | ||
Line 22: | Line 21: | ||
{{Constant table end}} | {{Constant table end}} | ||
− | + | == Remarks == | |
<!-- This section contains additional comments, observations and known issues. --> | <!-- This section contains additional comments, observations and known issues. --> | ||
+ | Note that the DA scripting language does not have an explicit boolean type, and using [[integer]]s to simulate it in this manner can lead to potentially subtle ambiguity. | ||
− | + | If you test for a variable's truth value by testing if it is equal to TRUE you may not get the correct result since TRUE == x is a true expression only when x is 1, yet all non-zero values are also not FALSE; FALSE != x is true when x is not zero. There is an ambiguity whenever x is not 1 even though all non-zero values are treated logically as true. When x is 2, for instance, both TRUE == x and FALSE == x are false statements, leading one to conclude that x is neither TRUE nor FALSE when its value is 2 (or any other value besides 0 and 1) | |
+ | |||
+ | == Examples == | ||
<!-- This section contains examples transcluded from the snippet library. --> | <!-- This section contains examples transcluded from the snippet library. --> | ||
+ | Note also that it is never necessary, and furthermore wasteful, to explicitly "test for TRUE" or "test for NOT FALSE" in a boolean expression anyway. One would never write, for example: | ||
+ | <pre> | ||
+ | if( (x == y) == TRUE ) ... <-- redundant comparison with TRUE | ||
+ | if( (x == y) != FALSE ) ... <-- redundant comparison with FALSE | ||
+ | </pre> | ||
+ | because the sub-expression (x == y) will evaluate to TRUE or FALSE already. One should instead write: | ||
+ | <pre> | ||
+ | if (x == y) ... | ||
+ | if ( !(x == y) ) ... | ||
+ | </pre> | ||
+ | |||
+ | If you follow this pattern the ambiguity described in "Remarks" above will never impact your code's behaviour. | ||
+ | |||
+ | The identifiers TRUE and FALSE are only really useful for defining default values of boolean type parameters in a function definition, passing a true value to a function with a boolean parameter when you call it, or setting the value of a boolean type variable. There is no other place where you need it. | ||
+ | <pre> | ||
+ | void Dance( int bChaCha = TRUE)... <-- default value in function def | ||
+ | SetItemDroppable( oItem, TRUE); <-- boolean parameter in function call | ||
+ | int bStillLooking = TRUE; <-- set a variable value | ||
+ | </pre> | ||
− | + | == See also == | |
<!-- This section contains links to articles, functions or constant groups. --> | <!-- This section contains links to articles, functions or constant groups. --> | ||
+ | *See [[bool (2da type)]] for booleans used in [[2DA]]s. | ||
[[Category:Constants]] | [[Category:Constants]] |
Revision as of 16:47, 26 October 2009
Constant name | Type | Value | Description | Source |
---|---|---|---|---|
FALSE | int | 0 | script.ldf | |
TRUE | int | 1 | script.ldf |
Remarks
Note that the DA scripting language does not have an explicit boolean type, and using integers to simulate it in this manner can lead to potentially subtle ambiguity.
If you test for a variable's truth value by testing if it is equal to TRUE you may not get the correct result since TRUE == x is a true expression only when x is 1, yet all non-zero values are also not FALSE; FALSE != x is true when x is not zero. There is an ambiguity whenever x is not 1 even though all non-zero values are treated logically as true. When x is 2, for instance, both TRUE == x and FALSE == x are false statements, leading one to conclude that x is neither TRUE nor FALSE when its value is 2 (or any other value besides 0 and 1)
Examples
Note also that it is never necessary, and furthermore wasteful, to explicitly "test for TRUE" or "test for NOT FALSE" in a boolean expression anyway. One would never write, for example:
if( (x == y) == TRUE ) ... <-- redundant comparison with TRUE if( (x == y) != FALSE ) ... <-- redundant comparison with FALSE
because the sub-expression (x == y) will evaluate to TRUE or FALSE already. One should instead write:
if (x == y) ... if ( !(x == y) ) ...
If you follow this pattern the ambiguity described in "Remarks" above will never impact your code's behaviour.
The identifiers TRUE and FALSE are only really useful for defining default values of boolean type parameters in a function definition, passing a true value to a function with a boolean parameter when you call it, or setting the value of a boolean type variable. There is no other place where you need it.
void Dance( int bChaCha = TRUE)... <-- default value in function def SetItemDroppable( oItem, TRUE); <-- boolean parameter in function call int bStillLooking = TRUE; <-- set a variable value
See also
- See bool (2da type) for booleans used in 2DAs.