Every programming language has a set of operators. They are used almost everywhere in code. They form the foundation for building functionalities. Dynamics Ax associate believes it is helpful to know what is offered in X++.
This post will cover the arithmetic and assignment operators. In the discussion, you will find the word expression. The expression here could be as simple as a number or a combination of various variables, operators, etc.
=
This operator assigns the result of expression to the right to the variable on the left. It is called the assignment operator.
Syntax
variable = expression
Example
int nVar = 2;
;
nVar = 12 - 3;
print nVar; // nVar is now 9.
pause;
?
This is called the ternary operator because it requires three expressions. The expression on the left is a condition. It will result in either true or false. The two expressions on the right separated by a colon are the result of the operation. If the condition is true, the expression on the left of the colon is returned. Otherwise, expression on the right of the colon is returned instead.
Syntax
expression1 ? expression2 : expression3
Example
int nVar = 2;
;
print nVar < 3 ? nVar + 1 : nVar - 1; // print output 3.
pause;
+=
This operator combines an arithmetic addition with an assignment operator. This operator assigns the current value of the variable to the left plus the expression on the right back to the variable on the left.
Syntax
variable += expression
Example
int nVar = 2;
;
nVar += 12 - 3;
print nVar; // nVar is now 11.
pause;
-=
This operator combines an arithmetic deduction with an assignment operator. This operator assigns the current value of the variable to the left minus the expression on the right back to the variable on the left.
Syntax
variable -= expression
Example
int nVar = 2;
;
nVar -= 12 - 3;
print nVar; // nVar is now -7.
pause;
++
This operator is used to increase a variable by one. Although this could be achieve with of other operators but this operator produces cleaner code.
Syntax
variable ++
Example
int nVar = 2;
;
nVar ++;
print nVar; // nVar is now 3.
pause;
--
This operator is used to decrease a variable by one.
Syntax
variable --
Example
int nVar = 2;
;
nVar --;
print nVar; // nVar is now 1.
pause;
+ (Plus)
This operator performs addition of two expressions.
Syntax
expression1 + expression2
Example
print 3 + 2; // Print output 5.
pause;
- (Minus)
This operator performs subtraction of expression on the right from expression on the left.
Syntax
expression1 - expression2
Example
print 3 - 2; // Print output 1.
pause;
* (Multiply)
This operator multiplies two expressions.
Syntax
expression1 * expression2
Example
print 3 * 2; // Print output 6.
pause;
/ (Divide)
This operator divides expression on the left with expression on the right.
Syntax
expression1 / expression2
Example
print 3 / 2; // Print output 1.5.
pause;
DIV
This operator performs integer division on expression on the left by expression on the right.
Syntax
expression1 DIV expression2
Example
print 5 DIV 2; // Print output 2.
pause;
MOD
This operator returns the remainder of an integer division of expression on the left by expression on the right.
Syntax
expression1 MOD expression2
Example
print 5 MOD 2; // Print output 1.
pause;
0 comment :
Add your opinion