Boolean Test Clauses
The Boolean AND operator makes it possible to express the conjunction of two conditions (see the Boolean Constants and OperatorsBoolean_Constants_and_Operators). When used in the test clause of IF ExpressionsIF_Expressions, this makes it possible to define functions that depend on both of two conditions being true.
For example, a job may pay a flat $10 per hour for any number of hours per week. Obviously, at least 0 hours will be worked and at most 168 hours can be worked (i.e. the number of hours in a week). Any number of hours outside that range must be a mistake. If h hours are worked during a week, the following IF expression returns the correct pay when h is a valid number of hours, and it returns ? when h is not valid:
IF(0 <= h AND h <= 168, 10h, ?)
When an IF expression is simplified and AND is the top-level operator of the test clause, the truth value of the test clause is determined as follows: The left operand of the AND is evaluated and
• If its truth value is false, the value of the test clause is also false. Note that in this case the right operand of the AND is not evaluated.
• If its truth value is true, the right operand of the AND is evaluated and returned as the value of the test clause.
• If its truth value cannot be determined, the right operand of the AND is evaluated and if it is false, the value of the test clause is also false. Otherwise, the truth value of the test clause cannot be determined.
The Boolean OR operator makes it possible to express the disjunction of two conditions (see the Boolean Constants and OperatorsBoolean_Constants_and_Operators). When used in the test clause of IF ExpressionsIF_Expressions, this makes it possible to define functions that depend on either of two conditions being true.
For example, a triangle is isosceles if any two of its angles are equal. CATEGORY is a function that returns "Isosceles" or "Scalene" depending on the three angles a, b and c:
CATEGORY(a,b,c) :=
IF(a=b OR a=c OR b=c, "Isosceles", "Scalene")
Note that the OR operator corresponds to the phrase "and/or" in English. Thus, CATEGORY returns "Isosceles" even if all three angles are equal.
When an IF expression is simplified and OR is the top-level operator of the test clause, the truth value of the test clause is determined as follows: The left operand of the OR is evaluated and
• If its truth value is true, the value of the test clause is also true. Note that in this case the right operand of the OR is not evaluated.
• If its truth value is false, the right operand of the OR is evaluated and returned as the value of the test clause.
• If its truth value cannot be determined, the right operand of the OR is evaluated and if it is true, the value of the test clause is also true. Otherwise, the truth value of the test clause cannot be determined.
Each operand of AND or OR can itself be an expression involving AND or OR. For example, three values a, b, and c are a monotonic sequence if a<=b<=c or a>=b>=c. The following IF expression tests for monotonicity:
IF(a<=b AND b<=c OR a>=b AND b>=c,
"Monotonic", "Nonmonotonic")
AND has a higher precedence than OR (see the Boolean Constants and OperatorsBoolean_Constants_and_Operators), so the above test clause is interpreted as if it were parenthesized
(a <= b AND b <= c) OR (a >= b AND b >= c)
rather than
a <= b AND (b <= c OR a >= b) AND b >= c
Each relational operator (see the Relational OperatorsRelational_Operators) has a complementary operator equivalent to its negation. Thus, the negation of a test clause can be transformed into a more readable and efficient one using the complementary operator. For example, NOT(x>5) is equivalent to x<=5, and NOT(x=5) is equivalent to x/=5.
To avoid errors and save time and space, it is worth using Derive to find the simplest possible Boolean expression for use as the test clause of IF expressions (see the Boolean Constants and OperatorsBoolean_Constants_and_Operators).
Other Programming in DERIVEProgramming
Created with the Personal Edition of HelpNDoc: Free CHM Help documentation generator