Unary Postfix Expressions in Dart

What are Unary Postfix Expressions?

Unary expressions invoke unary operators on objects.

A unary postfix expression is an invocation of a postfix operator on an expression.

Postfix Increment

The Postfix increment operator (++) is to increase the operand`s value by one unit.

Evaluation

  1. Increment takes place after the value is used in expression evaluation
  2. So the value of the expression is the same as the value of the operand.

Example:

var x = 0;
  print(x++); // x = x + 1 (expression value is x)

Example:

var x = 0;
  var y = 0;

  x = 0;

  y = x++; // Increment x AFTER y gets its value.

  print(x != y); // 1 != 0

Postfix decrement

The Postfix decrement operator (--) is to decrease the operand`s value by one unit.

Evaluation

  1. Decrement takes place after the value is used in expression evaluation.
  2. So the value of the expression is the same as the value of the operand.

Example:

var x = 0;
  print(x--); // x = x - 1 (expression value is x)

Example:

var x, y;

  x = 0;

  y = x--; // decrement x AFTER y gets its value.

  assert(x != y); // -1 != 0