Boolean evaluation
.mdTraditionally, the result of a logical or Boolean expression is considered true if it evaluates to 1 and false if it evaluates to 0.
In the D3 system, expressions that depend on a result of true or false also evaluate other values as true or false. This tends to vary somewhat between implementations. In generic D3, any nonzero integer that is positive or negative, evaluates to true. For example:
x = 5 if x then stop
Because x is a nonzero integer, the program
takes the then branch and stops. In generic D3, any
zero or null value evaluates to false. For instance:
y = "" if y then stop else print "yup"
This prints yup because y is evaluated as false. Some D3 implementations additionally evaluate
any negative numbers as false, and positive numbers as true. This
also holds true for +, -, ., +., and -..
This means that you must test your system to determine how it handles true and false. This program does it:
loop print "value to test " : input value until value = "quit" do if value then print "true" else print "false" repeat
Test it with negative numbers, null (Enter), positive numbers, letters, and so on.
A relational expression evaluates to 1 if the relation is true, and evaluates to 0 if the relation is false.
Relational operators have lower precedence than all arithmetic and string operators. Therefore, relational operators are only evaluated after all arithmetic and string operations have been evaluated.
In the logical expression:
x=ythe resolution of equal and unequal character pairs are handled as follows:
-
When both
xandyare numeric, the comparison is numeric. -
When either
xis numeric andyis a string, orxis a string and y is numeric, the string is converted to an equivalent numeric, if possible. If the conversion is successful, the comparison is numeric. If the conversion is not possible, the number is converted to a string, and the comparison is lexical. -
When both
xandyare strings, both are converted to numeric, if possible, and the comparison is numeric. If the conversion is not possible, the comparison is lexical.
The case of characters does not affect a comparison if casing is off. For example:
if "a" = "A" then...
This is true with casing off. It is false with casing on.