Decrement Operator

Dart Arithmetic Decrement Operator takes a numeric variable as operand and decrements (updates) its value by 1.

Based on the side at which the operand comes for the decrement operator, there are two kinds.

  • Post Decrement – The value in variable is decremented after the execution of the statement.
  • Pre Decrement – The value in variable is decremented first, and then the statement is executed.

Symbol

++ symbol is used for Decrement Operator.

Syntax

The syntax for Decrement Operator is

</>
Copy
operand++      //post decrement
++operand      //pre decrement

Examples

Post Decrement

In the following example, we take an integer value in a variable x, and decrement it using Arithmetic Post Decrement Operator.

main.dart

</>
Copy
void main() {
    var x = 5;
    x--;
    print('x = $x');
}

Output

x = 4

Let us try to print the value of x during post-decrement process, and observe how x is updated.

main.dart

</>
Copy
void main() {
    var x = 5;
    print('x = ${x--}');
    print('x = $x');
}

Output

x = 5
x = 4

Pre Decrement

In the following example, we take an integer value in a variable x, and decrement it using Arithmetic Pre Decrement Operator.

main.dart

</>
Copy
void main() {
    var x = 5;
    --x;
    print('x = $x');
}

Output

x = 4

Let us try to print the value of x during pre-decrement process, and observe how x is updated.

main.dart

</>
Copy
void main() {
    var x = 5;
    print('x = ${--x}');
    print('x = $x');
}

Output

x = 4
x = 4

Conclusion

In this Dart Tutorial, we learned how to use Decrement Operator to decrement the numeric value in a variable by one.