Flutter: Increment & decrement operators
++ and decrement
-- operators increase or decrease the value of the operand by one.
There are two ways we can use them:
- Prefix: The operand value is incremented by one, then the new value is returned.
- Postfix: The original value of the operand is returned, then the operand is incremented by 1.
When we use the operator as a postfix, we have to add it at the right side of the operand, for example:
int num = 5; print( num++ ); // prints 5 print( num ); // prints 6
In the previous code fragment, the line
print( num++) will print five because the original value of
num is returned and printed and then increased by one.
When we use the operator as a prefix, we have to add it at the left side of the operand, for example:
int num = 5; print( ++num ); // prints 6 print( num ); // prints 6
This time, the value of
num will be increased by one and then returned. That is why both
Example in DartPad
Let's run some examples in DartPad: