C++ Increment and decrement operators

The increment and decrement operators in C++ are used to increase or decrease a variable’s value by 1. To regulate the program’s flow, these operators are frequently employed in loops and other programming constructions.

Operator
Description
Example
Equivalent to
++
Increment by 1
a++
a = a + 1
--
Decrement by 1
a--
a = a - 1
++
Prefix increment by 1
++a
a = a + 1
--
Prefix decrement by 1
--a
a = a - 1

The increment operator (++) is used to increase a variable’s value by 1, and the decrement operator (–) is used to decrease a variable’s value by 1.

Example:

int x = 5;
x++;  // increment x by 1
int y = 10;
y--;  // decrement y by 1
cout << y;  // output: 9

This example gives the variable x a value of 5. The cout statement displays the new value of x, which is 6 after the x++ instruction increases the value of x by 1.

The decrement operator subtracts 1 from the value of the variable and stores the result in the same variable.

Example:

int y = 10;
y--;  // decrement y by 1
cout << y;  // output: 9