Compound Assignment Operators in C++
Compound Assignment Operators in C++
Let us see the Compound Assignment Operators in C++.
Operators | What the operators will do? |
<<= | Shift the value of the 1st operand left the number of bits specified by the value of the 2nd operand and then store the result in the Variable specified by the 1st operand. |
%= | Variable specified by the 1st operand = Take modulus of the 1st operand specified by the value of the 2nd operand |
^= | Obtain the bitwise exclusive OR of the 1st and 2nd operands; store the result in the Variable specified by the 1st operand. |
|= | Obtain the bitwise inclusive OR of the 1st and 2nd operands; store the result in the Variable specified by the 1st operand. |
&= | It can take the bitwise AND of the 1st value and the 2nd operands value and then store the result in the Variable that can be specified by the 1st operand. |
+= | Variable specified by the 1st operand = value of the 2nd operand + value of the 1st operand |
–= | Variable specified by the 1st operand = value of the 2nd operand – value of the 1st operand. |
>>= | Shift the value of the 1st operand right the number of bits specified by the value of the 2nd operand; store the result in the Variable specified by the 1st operand. |
* = | Variable specified by the 1st operand = value of the 1st operand * value of the 2nd operand |
/= | Variable specified by the 1st operand = 1st operand / value of the 2nd operand |
Compound Assignment Operators in C++ using For Loop
Let’s see the program of “Compound Assignment Operators in C++ using For Loop”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include<iostream> using namespace std; int main() { int a, b; cout<<"Enter the value of 'a'"<<endl; cin>>a; cout<<"Enter the value of 'b'"<<endl; cin>>b; for(int i=0; i<5; i++) { if(i==0) { a=a+b; cout<<"The value of 'a' after a+ is = "<<a<<endl; } else if(i==1) { a-=b; cout<<"The value of 'a' after a- is = "<<a<<endl; } else if(i==2) { a*=b; cout<<"The value of 'a' after a* is = "<<a<<endl; } else if(i==3) { a/=b; cout<<"The value of 'a' after a/ is = "<<a<<endl; } else { a%=b; cout<<"The value of 'a' after a% is = "<<a<<endl; } } } |
Compound Assignment Operators in C++ using While Loop
Let’s see the program of “Compound Assignment Operators in C++ using While Loop”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include<iostream> using namespace std; int main() { int a, b; cout<<"Enter the value of 'a'"<<endl; cin>>a; cout<<"Enter the value of 'b'"<<endl; cin>>b; int i=0; while(i<5) { if(i==0) { a=a+b; cout<<"The value of 'a' after a+ is = "<<a<<endl; } else if(i==1) { a-=b; cout<<"The value of 'a' after a- is = " <<a<<endl; } else if(i==2) { a*=b; cout<<"The value of 'a' after a* is = "<<a<<endl; } else if(i==3) { a/=b; cout<<"The value of 'a' after a/ is = "<<a<<endl; } else { a%=b; cout<<"The value of 'a' after a% is = "<<a<<endl; } i++; } } |
Compound Assignment Operators in C++ using the Array
Let’s see the program of “Compound Assignment Operators in C++ using the Array”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include<iostream> using namespace std; int main() { int a, b; int array[4]; cout<<"Enter the value of 'a'"<<endl; cin>>a; cout<<"Enter the value of 'b'"<<endl; cin>>b; int i=0; do { if(i==0) { a+=b; array[i]=a; } else if(i==1) { a-=b; array[i]=a; } else if(i ==2) { a*=b; array[i]=a; } else if(i==3) { a/=b; array[i]=a; } else { a%=b; array[i]=a; } i++; } while(i<5); int p=0; cout<<"The value of 'a' after a+ is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a- is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a* is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a/ is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a% is = "<<array[p]<<endl; } |
Compound Assignment Operators in C++ using the User Define Functions
Let’s see the program of “Compound Assignment Operators in C++ using the User Define Functions”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include<iostream> using namespace std; int compound(int&,int&,int[]); int main() { int a, b; int array[4]; cout<<"Enter the value of 'a'"<<endl; cin>>a; cout<<"Enter the value of 'b'"<<endl; cin>>b; compound(a,b,array); int p=0; cout<<"The value of 'a' after a+ is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a- is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a* is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a/ is = "<<array[p]<<endl; p++; cout<<"The value of 'a' after a% is = "<<array[p]<<endl; } int compound(int &a, int &b, int array[4]) { int i=0; do { if(i==0) { a+=b; array[i]=a; } else if(i==1) { a-=b; array[i]=a; } else if(i==2) { a*=b; array[i]=a; } else if(i==3) { a/=b; array[i]=a; } else { a%=b; array[i]=a; } i++; } while(i<5); } |