Start page / Template development / Template syntax / Expressions / Comparative expressions

Comparative expressions

Comparative expressions are a subdomain of the Logical expressions and return a truth value ('true' or 'false'). The following operators can be used for comparative expressions:

  • greater than '>'
  • less than '<'
  • greater than or equal to '>='
  • less than or equal to '<='
  • equal to '=='
  • not equal to '!=='

Comparative operations can be used within the template syntax. One option for output them is by using the instruction $CMS_VALUE(...)$.

'==' operator

The check for equality can be carried out using the operator '=='.

a==b

The evaluation of the expression checks whether a equals b.

Example:

$CMS_SET(a, 2)$
$CMS_SET(b, a)$
$CMS_VALUE(a==b)$

returns the value 'true'.

'!=' operator

The check for inequality can be carried out using the operator '!=='.

a!=b

The evaluation of the expression checks whether a is not equal to b.

Example:

$CMS_SET(a, 2)$
$CMS_SET(b, a)$
$CMS_VALUE(a!=b)$

returns the value 'false'.

'<' operator

The check for "less than" can be carried out using the operator '<'.

a<b

The evaluation of the expression checks whether a is less than b.

Example:

$CMS_SET(a,2)$
$CMS_SET(b,a)$
$CMS_VALUE(a<b)$

returns the value 'false'.

'<=' operator

The check for "less than or equal to" can be carried out using the operator '<='.

a<=b

The evaluation of the expression checks whether a is less than b or whether a is equal to b. If one of the conditions applies the expression returns the value 'true'.

Example:

$CMS_SET(a,2)$
$CMS_SET(b,a)$
$CMS_VALUE(a<=b)$

returns the value 'true'.

'>' operator

The check for "greater than" can be carried out using the operator '>'.

a<b

The evaluation of the expression checks whether a is greater than b.

Example:

$CMS_SET(a,2)$
$CMS_SET(b,a)$
$CMS_VALUE(a>b)$

returns the value 'false'.

'>=' operator

The check for "greater than or equal to" can be carried out using the operator '>='.

a>=b

The evaluation of the expression checks whether a is greater than b or whether a is equal to b. If one of the conditions applies the expression returns the value 'true'.

Example:

$CMS_SET(a,2)$
$CMS_SET(b,a)$
$CMS_VALUE(a>=b)$

returns the value '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 "||".

Important Before logical and comparative expressions are evaluated, first of all Arithmetical operations are performed.
Consequently the following operator precedence results:

  1. * / % (multiplication, division, modulo division)
  2. + - (addition, subtraction)
  3. < > <= >= (less than, greater than, less than or equal, greater than or equal)
  4. == != (equality, inequality)
  5. && (logical AND)
  6. || (logical OR)

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.

© 2005 - 2024 Crownpeak Technology GmbH | All rights reserved. | FirstSpirit 2024.12 | Data privacy