Preview
Assignment operators in C++ | Shorthand operators in C++
Assignment operators in C++ | Shorthand operators in C++
Assignment operators in C++ are also known as shorthand operators in C++. They are used to update the existing value of a variable in an easy way. Various Assignment operators in C++ are:
Operator | Description | Example |
= | It is used to assign some value to a variable. | N=10 |
+= | Adds some value to the existing value of a variable. | N=10 N+=3 N=10+3 =13 |
-= | Subtracts some value from the existing value of a variable. | N=10
N -= 3 N=10-3=7 |
*= | Multiplies existing value of a numeric variable by another value. | N=10
N *=3 N=10*3 =30 |
/= | Divides existing value of a numeric variable by another value. | N=10
N /=2 N=10/2 =5 |
%= | Divides existing value of a numeric variable by another value and gives remainder. | N=10
N %=3 N=10%3=1 |
&= | Applies bitwise AND operator on existing value of a numeric variable along with another value . | N=10
N &=7 N=10&7 =2 |
|= | Applies bitwise OR operator on existing value of a numeric variable along with another value . | N=10
N |=7 N=10|7 =15 |
^= | Applies bitwise XOR operator on existing value of a numeric variable along with another value . | N=10
N ^=7 N=10^7 =13 |
<<= | Applies bitwise LEFT SHIFT operator on existing value of a numeric variable . | N=10
N <<=1 N=10<<1 = 20 |
>>= | Applies bitwise RIGHT SHIFT operator on existing value of a numeric variable . | N=10
N >>=1 N=10>>1 = 5 |
//Program to demonstrate the use of shorthand operators.
#include<iostream> using namespace std; int main() { int n=10; n+=5; cout<<”\n n+=5 =”<<n; n-=3; cout<<”\n n-=3 =”<<n; n*=3; cout<<”\n n*=3 = ”<<n; n/=2; cout<<”\n n/=2 =”<<n; n%=3; cout<<”\n n%=3 =”<<n; return 0; }
Output
n+=5 = 15 n-=3 = 12 n*=3 = 36 n/=2 = 18 n%=3 = 0
Popular Books of Computer Science