< Prev
Next >

# C++ Compound Assignment Operators

In this article, we are going to discuss another operator provided by C++, known as compound assignment operator. There are four compound assignment operators in C++ such as -
• +=
• -=
• *=
• /=

## += operator

This operator performs two operations in sequence -
2. Assignment of the result of add operation.

Understanding += operator with a code -
``````int i=2;        //initializing i to 2
i+=2;		//equals to, i = i+2;
``````
• Statement i+=2 is equal to i=i+2, hence 2 will be added to the value of i, which gives us 4.
• Finally, the result of addition, 4 is assigned back to i, updating its original value from 2 to 4.

A special case scenario for all the compound assigned operators
``````int i=2;
i+=2*2;		//equals to, i = i+(2*2);
``````
In all the compound assignment operators, the expression on the right side of = is always calculated first and then the compound assignment operator will start its functioning. Hence in the last code, statement i+=2*2; is equal to i=i+(2*2), which results in i=i+4, and finally it returns 6 to i.

## Example with += operator

``````// Example with += operator in C++

#include<iostream

using namespace std;

int main()
{
char c='a';
cout<< "Original char : " << c << "\n";
c+=10; 		// c = c+10;
cout<< "char after +=10 operation : "<< c << "\n";

short s=10;
cout<< "Original short : " << s << "\n";
s+=5*10; 	// s = s+(5*10);
cout<< "short after +=5*10 operation : " << s <<"\n";

int i=10;
cout<< "Original int : " << i << "\n";
i+=10; 		// i = i+10;
cout<< "int after +=10 operation : "<< i <<"\n";

float f=10.5f;
cout<<"Original float : "<< f << "\n";
f+=20; 		// f = f+20;
cout<< "float after +=20 operation : "<< f << "\n";

double d=22.5;
cout<< "Original double : " << d << "\n";
d+=30; 		// d = d+30;
cout<< "double after +=30 operation : " << d;

return 0;
}``````
Output is
``````Original char : a
char after +=10 operation : k
Original short : 10
short after +=5*10 operation : 60
Original int : 10
int after +=10 operation : 20
Original float : 10.5
float after +=20 operation : 30.5
Original double : 22.5
double after +=30 operation : 52.5``````

## -= operator

This operator performs two operations in sequence -
1. Subtraction operation.
2. Assignment of the result of subtract operation.

Understanding -= operator with a code -
``````int i=2;
i-=2;
``````
• Statement i-=2 is equal to i=i-2, hence 2 will be subtracted from the value of i, which gives us 0.
• Finally, the result of subtraction i.e. 0 is assigned back to i, updating its value to 0.

• ## Example with -= operator

``````// Example with -= operator in C++

#include

using namespace std;

int main()
{
char c='k';
cout<< "Original char : "<< c << "\n";
c-=10; 		// c = c-10;
cout<< "char after -=10 operation : "<< c << "\n";

short s=10;
cout<< "Original short : "<< s << "\n";
s-=5*10; 	// s = s-(5*10);
cout<< "short after -=5*10 operation : "<< s << "\n";

int i=10;
cout<< "Original int : " << i << "\n";
i-=10; 		// i = i-10;
cout<< "int after -=10 operation : " << i << "\n";

float f=10.5f;
cout<< "Original float : " << f << "\n";
f-=20; 		// f = f-20;
cout<< "float after -=20 operation : " << f << "\n";

double d=22.5;
cout<< "Original double : " << d << "\n";
d-=30*2; 	// d = d-(30*2);
cout<< "double after -=30*2 operation : " << d;

return 0;
}``````
Output is
``````Original char : k
char after -=10 operation : a
Original short : 10
short after -=5*10 operation : -40
Original int : 10
int after -=10 operation : 0
Original float : 10.5
float after -=20 operation : -9.5
Original double : 22.5
double after -=30*2 operation : -37.5``````

## *= operator

This operator performs two operations in sequence -
1. Multiplication operation.
2. Assignment of the result of multiplication operation.

Understanding *= operator with a code -
``````int i=2;       //initializing i to 2
i*=2;
``````
• Statement i*=2 is equal to i=i*2, hence 2 will be multiplied with the value of i, which gives us 4.
• Finally, the result of multiplication, 4 is assigned back to i, updating its value to 4.

• ## Example with *= operator

``````// Example with *= operator in C++

#include<iostream>

using namespace std;

int main()
{
char c='a';
cout<<"Original char : " <<c <<"\n";
c*=1; 		// c = c*1;
cout<<"char after *=1 operation : " <<c <<"\n";

short s=10;
cout<<"Original short : "<<s <<"\n";
s*=5+10; 	// s = s*(5+10);
cout<<"short after *=5+10 operation : " <<s <<"\n";

int i=10;
cout<<"Original int : "<<i <<"\n";
i*=10; 		// i = i+10;
cout<<"int after *=10 operation : "<<i <<"\n";

long l=100000;
cout <<"Original int : " <<l <<"\n";
l*=1000; 	// l = l*1000;
cout<<"long after *=1000 operation : " <<l <<"\n";

float f=10.5f;
cout<<"Original float : " <<f <<"\n";
f*=20; 		// f = f*20;
cout<<"float after *=20 operation : " <<f <<"\n";

double d=22.5;
cout<<"Original double : "<<d <<"\n";
d*=30; 		// d = d*30;
cout<<"double after *=30 operation : " <<d;

return 0;
}``````

## Output -

``````Original char : a
char after *=1 operation : a
Original short : 10
short after *=5+10 operation : 150
Original int : 10
int after *=10 operation : 100
Original int : 100000
long after *=1000 operation : 100000000
Original float : 10.5
float after *=20 operation : 210
Original double : 22.5
double after *=30 operation : 675``````

## /= operator

This operator performs two operations in sequence -
1. Division operation.
2. Assignment of the result of division operation.

Understanding /= operator with a code -
``````int i=4;     //initializing i to 4
i/=2;
``````
• Statement i/=2 is equal to i=i/2, hence 4 will be divided by the value of i, which gives us 2.
• Finally, the result of division i.e. 2 is assigned back to i, updating its value from 4 to 2.

• ## Example with /= operator

``````// Example with /= operator in C++

#include<iostream*gt;

using namespace std;

int main()
{
char c='j';
cout<< "Original char : " << c << "\n";
c/=1; 		// c = c/1;
cout<< "char after /=1 operation : "<< c << "\n";

short s=10;
cout<< "Original short : "<< s << "\n";
s/=5-2; 	// s = s/(5-2);
cout<< "short after /=5-2 operation : " << s << "\n";

int i=10;
cout<< "Original int : " << i << "\n";
i/=10; 		// i = i/10;
cout<< "int after /=10 operation : " << i << "\n";

long l=999;
cout<< "Original int : " << l << "\n";
l/=2; 		// l = l/2;
cout<< "long after /=2 operation : " << l << "\n";

float f=80.5f;
cout<< "Original float : " << f << "\n";
f/=20; 		// f = f/20;
cout<< "float after /=20 operation : " << f << "\n";

double d=44.5;
cout<< "Original double : " << d <<"\n";
d/=22; 		// d = d/22;
cout<< "double after /=30 operation : " << d;

return 0;
}``````

## Output-

``````Original char : j
char after /=1 operation : j
Original short : 10
short after /=5-2 operation : 3
Original int : 10
int after /=10 operation : 1
Original int : 999
long after /=2 operation : 499
Original float : 80.5
float after /=20 operation : 4.025
Original double : 44.5
double after /=30 operation : 2.02273``````   