We use increment operator to increment the value of a variable by 1.
- Pre-increment operator (++a)
- Post-increment operator(a++)
Pre-increment operator (++a)
Pre-increment operator, increments self first then assigns the value.
The above code would print 2, 2
Because, a incremented self first then assigned to the variable b.
Post-increment operator, assigns self first then increments self
The above code would print 2, 1
Because, the value of a is assigned first to the variable b then the value of a is incremented by 1.
So we have to careful while using pre-increment operator and post-increment operator.