Assignment Operators and Increment Operator of C++

Assignment Operator:

The simple assignment operator (=) assigns the right side to left side.

C++ provides shorthand operators that have the capability of performing an operation and an assignment at the same time.
For example:

int x=5;
x+=3;  //It's mean x=x+3
x-=2;  //It's mean x=x-2
x*=2;  //It's mean x=x*2
x/=3;  //It's mean x=x/3

Increment Operator:

The increment operator is used to increase an integer value by one, and is a commonly used C++ operator.

x++;  //It's mean x=x+1

For example:

int x=10;
x++;
cout<<x;

Output:

11

The increment operator has two forms, prefix and postfix.

++x;  //prefix
x++;  //postfix

Prefix: Prefix increments the value, and then proceeds with the expression.

Postfix: Postfix evaluate the expression and then performs the increment.

Prefix example:

x=4;
y=++x;  //x is 4, y is 5

Decrement Operator:

The decrement operator (-) works in much the same way as the increment operator, but instead of increasing the value, it decreases it by one.

--x;  //prefix
x--;  //postfix

You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.