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

Arithmetic Expressions

Evaluation of arithmetic expressions

In general, the term arithmetic means calculating with (whole) numbers. These include the basic arithmetical operations:

These basic arithmetical operations can be used within the template syntax by means of arithmetic expressions. Several expressions also allow calculation with floating (decimal) point numbers. One option for output arithmetical expressions is using the instruction $CMS_VALUE(...)$.

Addition

Numerical values can be added using the operator '+'.

The expression:

1 + 1 

returns the value '2'.

Apart from integers, floating point numbers can also be used here:

3.1234 + 3.1234

The evaluation returns the value '6.2468'.

Subtraction

Numerical values can be subtracted using the operator '-'.

The expression:

1 - 1 

returns the value '0'.

Apart from integers, floating point numbers can also be used here:

3.1234 - 2.1

The evaluation returns the value '1.0234'.

Multiplication

Numerical values can be multiplied using the operator '*'.

The expression:

2 * 2 

returns the value '4'.

Analogously:

2 * x

Insofar:

$CMS_SET(x, 2)$

Apart from integers, floating point numbers can also be used here:

3.1234 * 2.1

The evaluation returns the value '6.55914'.

Division

Numerical values can be divided using the operator '/'.

The expression:

2 / 2 

returns the value '1'.

Analogously:

2 / x

Insofar:

$CMS_SET(x, 2)$

Apart from integers, floating point numbers can also be used here:

3.1234 / 2.1

The evaluation returns the value '1.4873'.

Modulo

Modulo (remainder) is a widely used function which gives the remainder of the division of two integers. In expressions the operator '%' is used for this.

The expression:

7 % 2 

returns the value '1'.

Analogously:

7 % x

Insofar:

$CMS_SET(x, 2)$

Combination of different arithmetical expressions

The arithmetical expressions introduced above can be combined with each other in any way required. Multiplication, division and modulo division are performed first, addition and subtraction are performed second (i.e. multiplicative operations take precedence over additive operations). This means, that multiplication, division and modulo operators have a higher precedence than addition and subtraction operators.

This means, in the following expression:

2 + 3 * 4

3 * 4

is evaluated first and then the result of this evaluation ('12') is linked with the next value:

2 + 12

returns the value '14'.

If operators have the same precedence they are evaluated from left to right.

However, the evaluation order can be influenced by inserting parentheses:

(2 + 3) * 4

returns the value '20'.

A further example:

(2 * 3 + 6) / (2 + 2) 

returns the value '3'.

On the other hand:

(2 * (3 + 6)) / (2 + 2)

returns the value '4.5'. This means, in expressions with nested parentheses: inner parentheses are evaluated first.

Important In combination with logical and / or comparative expressions arithmetical expressions are carried out preferentially.
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)

Using parentheses can alter the order of evaluation determined by operator precedence as described above.

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