Increment Operator

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

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

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

Symbol

++ symbol is used for Increment Operator.

Syntax

The syntax for Increment Operator is

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

Examples

Post Increment

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

main.dart

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

Output

x = 6

Let us try to print the value of x during post-increment 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 = 6

Pre Increment

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

main.dart

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

Output

x = 6

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

main.dart

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

Output

x = 6
x = 6

Conclusion

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