C++ Increment and Decrement Operators
C++ provides two increment and two decrement operators to change the value of a variable by one. The increment operators are "++" and "--", and the decrement operators are "--" and "++".
Increment operators:
Prefix increment operator (++var): This operator increments the value of the variable before using it in the expression.
Example:
int a = 5; int b = ++a; // a is incremented to 6, and b is assigned the value 6Socrue:www.theitroad.com
- Postfix increment operator (var++): This operator increments the value of the variable after using it in the expression.
Example:
int a = 5; int b = a++; // a is incremented to 6, but b is assigned the original value of a, which is 5
Decrement operators:
Prefix decrement operator (--var): This operator decrements the value of the variable before using it in the expression.
Example:
int a = 5; int b = --a; // a is decremented to 4, and b is assigned the value 4
- Postfix decrement operator (var--): This operator decrements the value of the variable after using it in the expression.
Example:
int a = 5; int b = a--; // a is decremented to 4, but b is assigned the original value of a, which is 5
In summary, the increment and decrement operators in C++ are used to change the value of a variable by one. The prefix increment operator (++var) increments the value of the variable before using it in the expression, and the postfix increment operator (var++) increments the value of the variable after using it in the expression. Similarly, the prefix decrement operator (--var) decrements the value of the variable before using it in the expression, and the postfix decrement operator (var--) decrements the value of the variable after using it in the expression.