Expressions in ST

An expression is a language construct that consists of a defined combination of operands (like →literals, →variables, →calls) and operators (like +, -, *, /) and yields one value of a particular data type.

logi.CAD 3 applies the following rules when evaluating the expressions:

  • The operators apply the operands in a sequence defined by the operator precedence: The operator with highest precedence in an expression is applied first, followed by the operator of next lower precedence, etc., until evaluation is complete. Operators of equal precedence are applied from left to right.
    Example: If A, B, C are of type INT with values 1, 2 and 4, respectively, then the expression A+B-C*(C / B) is evaluated as follows:

    1. The parentheses (C / B) is evaluated at first, due to the highest precedence: It equals to (4 / 2) and returns value 2.

    2. The multiplication C*(2) is evaluated next, due to the next lower precedence: It equals to 4*(2) and returns value 8.

    3. The addition A+B and subtraction B-8 are operators of equal precedence. As addition is left of subtraction, addition is applied first. Addition equals to 1+2 and returns value 3.

    4. At last, the subtraction 3-8 is applied: Hence, the result of the expression is the value -5.

  • When an expression has two operands, the leftmost operand is evaluated first.
    Example: In the expression EQ(A,B)=AND(A,B) the call of the EQ-block EQ(A,B) is evaluated first, followed by the call of the AND-block AND(A,B), followed by the equality operation (due to operator =).

  • →Boolean expressions are evaluated only to the extent necessary to determine the resultant value including possible side effects.
    Example: If A<=B, it is sufficient for the expression (A>B)&(C<D) to evaluate only (A>B) to decide that the result of the expression is value FALSE (or an equivalent) .

  • Expressions without a concrete data type are typed. See "Typing of expressions" for explanations on this.

No detection of mathematical errors

If you assign expressions in logi.CAD 3 , it is possible that mathematical errors are not detected and reported

Examples of assignments with expressions in the ST-code for which a mathematical error is not recognized/reported
Var1 := 0**0 = 1
Var2 := 0 / 0 = 1
Var3 := 0 MOD 0 = 1

Additional information: If →constant expressions are used when initializing variables, errors are no longer detected.

Workaround: Check with suitable tools whether the specified expression can be mathematically evaluated.

Avoid using complex expressions with more than 20 operands/operators in order to avoid considerable implications on the performance when saving or when importing. See the troubleshooting article "C/C++ Indexer is not concluded and prevents the saving or importing of an object" for details.