You are here

Working with equations : Boolean expressions

Boolean expressions

Boolean expressions return only "true" or "false". Boolean expressions are commonly used in conditional expressions to decide between two or more alternative subexpressions, but a Simile variable can have a boolean value if its whole equation is a boolean expression; indeed, condition and alarm components have to have boolean values.

Constants

The expressions "true" and "false" may be used as Boolean constants in expressions. Note that quotation marks must be used around the words. This is because these constants are implemented as built-in enumerated types.

Mathematical comparisons

A simple condition consists of a direct comparison between two values, using the relational operators

>

greater than

X > Y

X is greater than Y

<

less than

X < Y

X is less than Y

>=

greater than or equal to

X >= Y

X is greater than or equal to Y

<=

less than or equal to

X <= Y

X is less than or equal to Y

==

equal to

X == Y

X is equal to Y

!=

not equal to

X != Y

X is not equal to Y

Thus, the following are legal examples:

  • 5 > 3 though it's hard to see the point of this, since this is always true
  • height <= 30 where the variable "height" is an influencing variable

Each of the two terms, on the left and right of the comparison, can themselves be arbitrary mathematical expressions. Thus, the following are also legal:

  • 5 > 3*2 (result is false, since 5 < 6)
  • 0.1*height <= 30
  • k*z+2 < 17*h

Logical comparisons

Four Boolean operators can be used in expressions. There are "and", "or", "xor" and "not". The following table shows the two alternative forms of these operators, which can also written in symbolic form. Note also that a comma "," is accepted as an alternative symbol for "and".

     

A and B

A && B

true if and only if both A and B are true

A or B

A ; B

true if either A is true or B is true

A xor B

A != B

true if either A is true or B is true, but not both

not A

! A

true if and only if A is not true

The truth tables for the four operators are given here. Note that "and", "or" and "xor" require two operands, whilst "not" requires one operand. The equation parser will signal an error if an incorrect number of operands is used.

Functions

Some functions return boolean values, i.e., any(), all(), channel_is(), dies_of(), and first().

In: Contents >> Working with equations >> Components of an equation