Difference between revisions of "Boolean constants"
BryanDerksen (Talk | contribs) (condensing AxeMurderer's contributions to hit the key points more strongly, and distributing to appropriate sections.) |
Axe Murderer (Talk | contribs) m (added another example) |
||
Line 43: | Line 43: | ||
If you follow this pattern the ambiguity described in "Remarks" above will never impact your code's behaviour. | 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. | + | 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, returning a boolean result from a function, or setting the value of a boolean type variable. There is no other place where you need it. |
<pre> | <pre> | ||
void Dance( int bChaCha = TRUE)... <-- default value in function def | void Dance( int bChaCha = TRUE)... <-- default value in function def | ||
SetItemDroppable( oItem, TRUE); <-- boolean parameter in function call | SetItemDroppable( oItem, TRUE); <-- boolean parameter in function call | ||
+ | return TRUE; <-- return value from a function | ||
int bStillLooking = TRUE; <-- set a variable value | int bStillLooking = TRUE; <-- set a variable value | ||
</pre> | </pre> |
Revision as of 11:16, 28 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, returning a boolean result from a function, 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 return TRUE; <-- return value from a function int bStillLooking = TRUE; <-- set a variable value
See also
- See bool (2da type) for booleans used in 2DAs.