thumbnail

Postfix and Prefix increment and decrement operators in Dart

Published on 29 March 2020
Last Updated on 29 March 2020

Table that describes various increment operators

Operator Description
++a a = a + 1 (current value of a is a + 1)
a++ a = a + 1 (current value of a is a)
–a a = a – 1 (current value of a is a – 1)
a– a = a – 1 (current value of a is a)

Program using increment operators

Refer to following code to understand how increment and decrement operators work in Dart.

void main(List<String> args) {
  var x, y;

  x = 0;
  y = ++x; // increment x before y gets its value.
  print('x:${x} y:${y}');

  x = 0;
  y = x++; // increment x AFTER y gets its value.
  print('x:${x} y:${y}');

  x = 0;
  y = --x; // decrement x before y gets its value.
  print('x:${x} y:${y}');

  x = 0;
  y = x--; // decrement x after y gets its value.
  print('x:${x} y:${y}');
}

Above program produces following output:

x:1 y:1
x:1 y:0
x:-1 y:-1
x:-1 y:0