Operator Precedence in X++ Programming Language

Rumman Ansari   Software Engineer   2023-08-24   597 Share
☰ Table of Contents

Table of Content:


Operator Precedence in X++ Programming Language

The sequence of evaluating a compound expression can carry significance. For instance, the outcome of (x + y / 100) varies based on whether the addition or division takes precedence.

To precisely guide the X++ compiler in evaluating an expression, parentheses ( ) come into play. For instance, (x + y)/ 100.

In the absence of explicit instructions for operation sequence, the compiler follows the hierarchy defined for operators' precedence. Take the example of the division operator possessing higher precedence than the addition operator. In the case of x + y / 100, the compiler computes y/100 as the initial step. Thus, x + y / 100 translates to x + (y / 100).

To enhance code clarity and manageability, it is advisable to be unambiguous. Employ parentheses to indicate which operators are to be given priority in evaluation.


Order of Operator Precedence

The operators in the following table are listed in precedence order—the higher in the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with a lower precedence. Note that the operator precedence of X++ is not the same as other languages, for example C# and Java.

Operators in precedence order

Syntax

unary operators

- ~ !

multiplicative, shift, bitwise AND, bitwise exclusive OR

* / % DIV << >> & ^

additive, bitwise inclusive OR

+ – |

relational, equality

< <= == != > >= like as is

logical operators (AND, OR)

&& ||

conditional

? :

Operators on the same line have equal precedence. If there is more than one of these operators in an expression, the expression is evaluated from left to right unless assignment operators are used (these are evaluated from right to left).

For example, && (logical AND) and || (logical OR) have the same precedence and are evaluated from left to right. This means that:

0&&0||1 == 1, and 1||0&&0 == 0