Using Arithmetic Operators

Standard Mathematical Operators:
  • Addition ( + )
  • Subtraction ( - )
  • Multiplication ( * )
  • Division ( / )
  • Remainder ( % )

Increment ( ++ ) and Decrement ( -- ) Operators:

  • Pre-Increment and Pre-Decrement - the operation is applied before any subsequent calculations or assignments are performed.
  • Post-Increment and Post-Decrement - the operation is applied after any subsequent calculations or assignments are performed.

Examples:

Pre-Increment:
int x = 6;
int y = ++x;
x is 7, y is 7

Post-Increment:
int x = 6;
int y = x++;
x is 7, y is 6


Pre-Decrement:
int x = 6;
int y = --x;
x is 5, y is 5


Post-Decrement:
int x = 6;
int y = x--;
x is 5, y is 6


Operator Precedence Order:

  • Parentheses
  • Increment and Decrement Operators
  • Multiplication and Division Operators (Evaluated from Left to Right)
  • Addition and Subtraction Operators (Evaluated from Left to Right)

0 comments:

Post a Comment