Advertisement
int i=2; //initializing i to 2
i+=2; //equals to, i = i+2;
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 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
int i=2;
i-=2;
// 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
Advertisement
int i=2; //initializing i to 2
i*=2;
// 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;
}
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
int i=4; //initializing i to 4
i/=2;
// 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;
}
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
Advertisement
Advertisement
Please check our latest addition
PYTHON
Advertisement