Define different expression types

Completed

An expression is a formula that specifies how to generate a desired value and then evaluates to a value. An expression uses terms and an operator, which can be unary or binary.

With a unary operator, the operator impacts the term that directly follows it. A binary operator impacts on either side of the operator.

You can categorize operators in one of following operator types:

  • Unary

  • String

  • Arithmetic

  • Relational

  • Logical

Unary operators

A unary operator only impacts the term that directly follows the operator. The following are unary operator values:

  • +

  • -

  • NOT

When you use the minus sign (-), it creates a negative value of the term that comes after the minus sign. Therefore, it only has an impact on the term that follows the operator.

var
    CustomerExists: Boolean;
    MyBoolean: Boolean;
begin
    CustomerExists := true;
    MyBoolean := NOT CustomerExists;
    // Will result in MyBoolean: false
end;

The next operators are all binary operators, which impact terms on either side of the operator.

String operators

The only string operator is the plus sign (+), which is a binary operator because it impacts terms on either side of the operator. The string operator concatenates two strings.

Example of using a String operator in AL.

Arithmetic operators

Arithmetic operators are used in numeric expressions. A numeric expression is a formula that results in a numeric value.

The following arithmetic operators are available:

  • Plus (+)

  • Minus (-)

  • Times (*)

  • Divide (/)

  • Integer divide (DIV)

  • Modulus (MOD)

var
    MyExpression: Integer;
begin
    MyExpression:= 5 + 2 * 3;
    // Will result in MyExpression: 11
end;

Depending on the data types that you use with arithmetic operators, you can get other data types as a result.

17/8 = 2.125
17/9 = 1.88

The divide operator, or division, is the normal mathematical division that you are accustomed to working with. If you divide two integer values, you'll always end with a Decimal value.

The integer divide (DIV) operator is returning the integer quotient of the integer division. It's a binary operator that results in an integer value. You can calculate the result of an integer divide by dropping all the decimals of the result.

17 DIV 8 = 2   The decimal portion 125 is dropped
17 DIV 9 = 1   The decimal portion 88 is dropped

The modulus operator (MOD) finds the remainder of the division of one number by another number. The modulus operator is used a lot when you are validating numbers like bank account numbers.

5 MOD 2 = 1   5 = (2x2) + 1
17 MOD 9 = 8   17 = (9x1) + 8

If you compare the operators, the following results occur:

5 / 2 = 2.5
5 DIV 2 = 2
5 MOD 2 = 1

The order in which operators are applied could have an effect that can significantly influence the outcomes of your calculations.

  • Highest level:

    Unary Operator

  • Second level:

    * , / , DIV, MOD

  • Lowest level:

    + , - (binary)

You can change the operator precedence effects by using SubExpressions (). SubExpressions are always evaluated first, and you can use them by adding parentheses ().

5 + 3 * 6 – 2 DIV -2 = ?

First Level:
5 + 3 * 6 – 2 DIV -2

Second Level:
5 + 3 * 6 – 2 DIV -2
= 5 + 18 – (-1)

Lowest Level:
+ and –
23 -(-1) = 24

Relational expressions

The following Relational expressions are available:

  • = (equal to)

  • < (less than)

  • > (greater than)

  • <= (less than or equal to)

  • >= (greater than or equal to)

  • <> (not equal to)

  • IN (include in set)

Relational operators have the lowest precedence of all operators. With relational expressions, you can compare values in terms of smaller or larger, equal, less than, or greater than. You can use relational operators for the following situations:

  • Numeric comparisons - Numeric comparisons are clear; they compare on the numeric value.

  • String comparisons - Strings are compared with the Unicode date to determine which value comes first.

  • Date and time comparisons - The calendar is used to determine which value comes first.

  • Boolean comparisons - The false value will come before the true value.

The Set Inclusion (with the IN operator) is a list of values, as shown in the following example:

[2,4,6,8,10]
[1..9,11..19]

The IN operator verifies whether a specific value is included in a set or not.

5 IN [2,4,6,8,10] is FALSE
5 IN [1..9,11..19] is TRUE
5 IN [2,4..6,8,10] is TRUE
10 IN [1..9,11..19] is FALSE
'M' IN ['A'..'Z'] is TRUE

Logical expressions

The following Logical expressions are available:

  • AND

  • OR

  • XOR

  • NOT

Logical operators also evaluate in true and false values (they result in Boolean values). Logical operators don't compare regular values like strings, numbers, or dates; they compare Boolean values, while relational operators compare values based on differences in their values.

The following example shows a comparison between the different logical operators.

Tables showing the differnces between Logical operators.

  • With the NOT operator, all logical values are reverted. A NOT True results in False, and a NOT False results in True.

  • With the OR operator, one of the values must be True to result in True; otherwise, the value is False.

  • With the AND operator, both values must be True to result in True; otherwise, the value is False.

  • With the XOR (Exclusive OR) operator, only one value can be True to result in True; otherwise, the value is False.

The following example puts all the operators together.

Example showing the Operator precedence effects.

Boolean expressions

When programming or using application language, you'll also encounter Boolean expressions. A Boolean expression is an expression that results in a Boolean value and, for that, you can use a Boolean variable or constant. Typically, relational and logical expressions evaluate in Boolean values.