In C and C + +, the self-increment (+ +) and decrement (-) operators are often used in expressions to increase the value of a variable by 1 or minus 1, for example:
++i (before using I, the value of I is added 1, if the original value of I is 3, after the execution of J=++i, the value of J is 4)
-I (Before using I, the value of I is reduced by 1, if the original value of I is 3, then after the execution of J=--i, the value of J is 2)
i++ (after using I, the value of I is added 1, if I is the original value of 3, then after the execution of j=i++, the value of J is 3, then I becomes 4)
i--(after using I, the value of I is reduced by 1, if the original value of I is 3, then after executing j=i--, the value of J is 3, then I becomes 2)
++i is the value of I after the i=i+1 is executed, and i++ is the value of I before the i=i+1 is executed.
Correct use of + + and--can make the program concise? clear, efficient. Please note:
- The increment operator (+ +) and the decrement operator (--) can be used only for variables, not constants or expressions.
- + + and--the combination of the direction is "from right to left."
- The self-increment operator (+ +) and the decrement operator (--) are very flexible to use, but in many cases there may be ambiguities that produce "unexpected" side effects.
- The increment (minus) operator is often seen in C + + programs and is often used in loop statements to automatically add 1 to the loop variable. Also used for pointer variables to point the pointer to the next address.
C + + self-increment and decrement operators (--and + +)