Operator in X++ Programming Language

Rumman Ansari   Software Engineer   2023-05-07   7471 Share
☰ Table of Contents

Table of Content:


An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result. This chapter will explain the concept of operators and it will take you through the important arithmetic and relational operators available in X++.

No Operators
1 Assignment Operators
2 Arithmetic Operators
4 Relational Operators

Operators

Operators are used to compute values. The different types of operators include assignment operators, arithmetic operators, and relational operators.

Assignment operators can change the value of the variable or field.

Operator Description Example
= Assigns the value on the right of the equal sign to the variable on the left. Int i = 1;
+= Assigns the current variable value plus the value on the right of the equal sign. int I ; I += 1;
++ Increments the variable by 1. int I; i++;
-= Assigns the current variable value minus the expression on the right of the equal sign. int I; I -= 1;
-- Decrements the variable by 1. int I; i--;

Arithmetic operators are used to make numeric calculations.

Operator Description Example
<< The left shift operator multiplies the left value by 2 by the number of times equal to the value on the right. int a = 1 << 2 (this equals 1*2*2.)
>> The right shift operator divides the left value by 2 by the number of times equal to the value on the right. int a = 1 >> 2 (this equals 1/2/2.)
* This operator multiplies operands. int a = 1*2
/ This operator divides the left value by the right value. int a = 1/2
div The integer division operator performs an integer division of the left value and the right value. int a = 20 div 6 (This returns a division of 3 with a remainder of 2. A = 3)
mod The integer remainder operator returns the remainder of an integer of the left value and the right value. int a = 20 mod 6 (This returns a division of 3 with a remainder of 2. A = 2)
~ The not operator performs a binary not operation. int a = ~1 (This returns -2.)
& The binary AND operator performs a binary operation on the left and right value. This returns the bits in common. int a = 1 & 3 (This returns 1.)
^ The binary XOR operator performs a binary XOR operation on the left and right value. This returns the bits set in one, but not the other. int a = 1 ^ 3 (This returns 2.)
| The binary OR operator performs a binary operation on the left and right value. This returns the bits set in either. int a = 1 | 3 (This returns 3.)
+ The plus operator adds operands. int a = 1 + 4 (This returns 5.)
- The minus operator subtracts the left or right values. int a = 4 - 1 (This returns 3.)
? The ternary operator takes three values. If value 1 is true, value 2 is returned. Else, value 3 is returned. int a = (x > 3) ? 1:5 (If x is greater than 3, then return 1, else return 5.)

Relational operators are used in X++ to compare the values between variable values of similar types. Some limitations with complex data types such as GUIDs apply.

Operator Description
Like This operator is exclusively used in SQL statements and returns true if the criteria are found within the second value. For example, select a table where a field such as ‘12*’ returns all rows from a table where the field values start with 12.
== Returns true if the values are equal.
!= Returns true if the first value is not equal to the second value.
>= Returns true if the first value is greater than or equal to the second value.
<= Returns true if the first value is less than or equal to the second value.
> Returns true if the first value is greater than the second value.
< Returns true if the first value is less than the second value.
&& Returns true if the first and second value are true.
|| Returns true if the first or second value is true.
! Returns true if the value is false, and it returns false if the value is true.

A ternary operator allows you to evaluate if an expression is true or false to get a result. This is like an if statement, which is discussed in the Use conditional and iterative statements and Exception handling units. However, where an if statement provides conditional branching for the code that is being run, the ternary operator result can be assigned to a variable. The following example checks if the variable x is equal to 5. If it is equal to 5, then the ternary operator assigns the value Yes to the result variable. If x does not equal 5, the ternary operator assigns the value No to the result variable.


result = (x==5) ? "Yes" : "No";

Sample Code


static void operatorsExample(Args _args)
{
    int a = 10;
    int b = 20;
    int c;

    // Assignment operator
    c = a + b;
    info(strFmt("Value of c after assignment: %1", c));

    // Arithmetic operators
    c = a + b;
    info(strFmt("Value of c after addition: %1", c));

    c = a - b;
    info(strFmt("Value of c after subtraction: %1", c));

    c = a * b;
    info(strFmt("Value of c after multiplication: %1", c));

    c = b / a;
    info(strFmt("Value of c after division: %1", c));

    c = b % a;
    info(strFmt("Value of c after modulo: %1", c));
 
    // Relational operators
    boolean result;
    result = a == b;
    info(strFmt("a == b: %1", result));

    result = a != b;
    info(strFmt("a != b: %1", result));

    result = a > b;
    info(strFmt("a > b: %1", result));

    result = a < b;
    info(strFmt("a < b: %1", result));

    result = a >= b;
    info(strFmt("a >= b: %1", result));

    result = a <= b;
    info(strFmt("a <= b: %1", result));
}