Increment operator, decrement operator

It's not that difficult on its own, but it's complicated when it comes in bundles, so it's a summary.

What are increment operator and decrement operator?

--Increment "++" ・ ・ ・ Add 1 to the value of the variable --Decrement "-" ・ ・ ・ Subtract 1 to the value of the variable

Prefix and postfix

Prefix: Calculate first and substitute the resulting value

Example

int a = 5 int b = ++a
↑ Add 1 to a and then substitute for b (b = 6, a = 6)

Postscript: Substitute a value and then calculate later

Example

int a = 5 int b = a++
↑ Substitute in b and then add 1 to a (b = 5, a = 6)

Examples of complicated calculations

int a=5 int b = a++ + ++a + --a - a-- -a

Or something b = 5 + 7 + 6 - 6 - 5

And the answer is 7

Recommended Posts

Increment operator, decrement operator
Self-assignment operator