Logical expressions
Gosu logical expressions use standard logical
operators to evaluate the expression in terms of the boolean values of true and false.
Gosu supports the following logical expressions:
- Logical
AND - Logical
OR - Logical
NOT
Gosu evaluates logical expressions from left to right and uses the following rules to test the expressions for possible short-circuit evaluation:
trueORany-expression always evaluates totrue– Gosu only runs and evaluates any-expression if the expression before theORis false. If Gosu determines that the expression before theORevaluates totrue, the second expression is not evaluated.falseANDany-expression always evaluates tofalse– Gosu only runs and evaluates any-expression if the expression before theANDis true. If Gosu determines that the expression before theANDevaluates tofalse, the second expression is not evaluated.
Logical AND
Gosu uses either and or && to indicate a logical
AND expression. The operands must be of the
Boolean data type or any type convertible to
Boolean. The result is always Boolean.
Syntax
a and b
a && bExamples
Expression |
Result |
|---|---|
|
(true/true) =
|
|
(true/false) = |
|
(false/true) = |
|
(false/false) = |
Logical OR
Gosu uses either or or || to indicate a logical
OR expression. The operands must be of the
Boolean data type or any type convertible to
Boolean. The result is always Boolean.
Syntax
a or b
a || bExamples
Expression |
Result |
|---|---|
|
(true/true) =
|
|
(true/false) = |
|
(false/true) = |
|
(false/false) = |
Logical NOT
To indicate a logical negation (a logical
NOT expression), use either
the keyword not or the
exclamation point character (!).
The operand must be of the Boolean
data type or any type convertible to Boolean.
The result is always Boolean.
Syntax
not a
!aExamples
Expression |
Result |
|---|---|
|
|
|
|
|
|
|
|
The following examples illustrate how
to use the NOT operator.
- Bad example
- The following example demonstrates how not to use the logical
NOToperator.if (not liabilityCov.Limit == line.BOPLiabilityCov.Limit) { return true }This example causes an error at run time because Gosu associates the
NOToperator with the variable to its right before evaluating the expression. This association causes the expression to become:(false == line.BOPLiabilityCov.Limit)This comparison causes a class-cast exception, as follows:
'boolean (false)' is not compatible with Limit - Better example
- The following example demonstrates how to use the
NOToperator correctly.if (not (liabilityCov.Limit == line.BOPLiabilityCov.Limit)) { return true }In this example, the extra parentheses force the desired comparison, and associate the
NOToperator with the correct expression. - Preferred example
- Use the
!=operator rather thanNOTfor writing code of this type.if (liabilityCov.Limit != line.BOPLiabilityCov.Limit) { return true }The expression had no need to use the
NOToperator. The final code expression is somewhat simpler and does exactly what is required.
typeis expressions
Gosu uses the operator typeis to test the type of an
object against a named type. The result is true if the object has that type
or a subtype.
See also
