# Operator Precedence [AX 2012]

*
Updated: September 23, 2011*

*
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012*

The order in which a compound expression is evaluated can be important. For example, (x + y / 100) gives a different result depending on whether the addition or the division is performed first.

You can use parentheses ( ) to explicitly tell the X++ compiler how you want an expression to be evaluated. For example, (x + y)/ 100.

If you do not explicitly tell the compiler the order that you want operations to be performed in, the order is based on the precedence assigned to the operators. For example, the division operator has a higher precedence than the addition operator. For x + y / 100, the compiler would evaluate y/100 first. So, x + y / 100 is equivalent to x + (y / 100).

To make your code easy to read and maintain, be explicit. Indicate with parentheses which operators should be evaluated first.

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.

Operators in precedence order |
Syntax |
---|---|

postfix operators |
[] . ( params ) expr ++ expr -- |

unary operators |
++ expr -- expr + expr - expr ~ ! |

creation |
new ( type ) expr |

multiplicative |
* / % |

additive |
+ - |

relational |
< > <= >= |

equality |
== != |

bitwise AND |
& |

shift |
<< >> |

bitwise exclusive OR |
^ |

bitwise inclusive OR |
| |

logical operators (AND, OR) |
&& || |

conditional |
? : |

assignment |
= += -= |

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

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.