Logical expressions
Linking logical expressions
Logical expressions is the term used for all expressions which return a truth value (true or false). The logical expressions therefore also include the Comparative expressions (equal to, not equal to, greater than, less than, etc.), although these are explained in their own, separate chapter.
Logic operations connect several logical expressions to form a more complex logical expression. Depending on the logical operator, the combined expression is for example precisely true if all linked expressions are true (AND operation) or if at least one of the linked expressions is true (OR operation).
Logic operations can be used within the template syntax. One option for output them is by using the instruction $CMS_VALUE(...)$.
AND operation
The AND operation can be performed using the operator '&&'.
a&&b
Whereby both 'a' and 'b' are logical expressions.
The expression:
(2<3) && (3>2)
returns the value 'true' of Boolean data type as the two linked expressions are also 'true'.
OR operation
The OR operation can be performed using the operator '||'.
a||b
Whereby both 'a' and 'b' are logical expressions.
The expression:
(3<3) || (3>2)
returns the value 'true' of Boolean data type as one of the two linked expressions is also 'true'.
Negation
An expression can be negated using the operator '!'. On executing the negation of an expression 'a' an expression with the opposite truth value is formed:
!a
means, if:
- the expression a is 'true' then the negation is 'false'
- the expression a is 'false' then the negation is 'true'
An overview of the results of logical operations
Expression 'a' | Operator | Expression 'b' | Result of the logic operation (linking) |
---|---|---|---|
true | && | true | true |
true | && | false | false |
false | && | true | false |
false | && | false | false |
true | || | true | true |
true | || | false | true |
false | || | true | true |
false | || | false | false |
! | true | false | |
! | false | true | |
Operator precedence
In FirstSpirit combinations of several operators within logical or comparative expressions are evaluated, like in other programming languages (e.g. Java), in a specific order.
In a logical AND operation (conjunction) a && b b is not evaluated if a returns already the Boolean value 'false'. In a logical OR operation (disjunction) a || b b is not evaluated if a returns already the Boolean value 'true'.
In addition there is a ranking order of operators: Comparative expressions ("==", "!=", "<", ">", "<=", ">=") have higher precedence than Logical expressions ("&&", "||"). This means that comparative expressions are evaluated before logical expressions, special use of parentheses is not necessary.
Furthermore, within comparative expressions the operators "<", ">", "<=" and ">=" have higher precedence than the operators "==" and "!=" and are evaluated first. Within the logical expressions "&&" has higher precedence than "||".
Before logical and comparative expressions are evaluated, first of all Arithmetical operations are performed. Consequently the following operator precedence results:
Operators with the same precedence are evaluated from left to right. Using parentheses can alter the order of evaluation determined by operator precedence. |
Example:
The text "The value of the variable a begins with b or B." is to be output if a is not null and the value (here Ball) begins with the character "b" (in upper or lower case).
$CMS_SET(a, "Ball")$
$CMS_IF(a != null && a.charAt(0).toString.lowerCase == "b")$
The value of the variable a begins with b or B.
$CMS_END_IF$
In this example the expression a != null as well as the expression a.charAt(0).toString.lowerCase == "b" return the Boolean value 'true'. Result: The text will be output.
In a second example the text "The value of the variable a begins with b or B." is to be output if a is not null and the value begins with the character "b" (in upper or lower case).
$CMS_SET(a, null)$
$CMS_IF(a != null && a.charAt(0).toString.lowerCase == "b")$
The value of the variable a begins with b or B.
$CMS_END_IF$
In this case the text will not be output because first the expression a != null is evaluated. It returns the value 'false' so that the operator with the lower priority (here: &&) takes no effect at all.