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 "++".

  1. Increment operators:

  2. 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 6
So‮cru‬e:www.theitroad.com
  1. 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
  1. Decrement operators:

  2. 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
  1. 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.