Assignment Operators are used for assigning values to the variables in the program. There are two types of Assignment operators used. The first one being the Simple Assignment Operator and the other one being Shorthand Operators or Compound Assignment Operators. Expressions or Values can be assigned to the variable using Shorthand Assignment Operators. C++ supports both types of Assignment Operators. Let us look at the Shorthand Assignment Operators and their different types in this article below.
Definition
- Shorthand Assignment Operators combines one of the arithmetic or bitwise operators with the assignment operator. They are also called as compound assignment operators.
- A Shorthand Assignment Operator is a shorter way of expressing something that is already available in the programming statements.
Shorthand Operators
C++ language offers its users a few special shorthand’s that simplifies the coding of certain expressions. It also saves coding time by using these Shorthand Operators in the program. Shorthand Assignment Operators have the lowest order of precedence i.e., they are the last to be evaluated in an expression.
Shorthand Assignment Operators follow the following Syntax –
variable_name operator = expression/value ;
which is equivalent to :
variable_name = variable_name operator expression/value ;
Example
int a = 20 ;
a += 10 ; // which means a = a + 10 , Output is 30
int z = 10 ;
z *= 10 ; // which means z = z * 10 , Output is 100
Note: The variable data type and the value assigned to it should match. Or else the compiler will give an error while running the program.
Browse all the Topics Under Operator and Expressions: Operators
- Arithmetic Operators
- Assignment Operators
- Unary Operator
- Increment and Decrement Operators
- Relation Operator
- Logical Operators
Types of Shorthand Assignment Operators
Shorthand Assignment Operators are Binary Operators which require 2 values or 1 variable and another value/expression. One on the left and the other on the right side.
The following are types of Shorthand Assignment Operators
-
+=
This type of Operator is a combination of Arithmetic Operator ‘+’ and Assignment Operator ‘=’. This operator adds the variable on the left with the value on the right and then assigns/saves the result to the variable on the left.
Example
int a = 6 ;
int b = 8 ;
a += b ; // a = a + b ; i.e., a = 6 + 8 = 14
Output
a = 14
-
-=
These types of Shorthand Operators are used with a combination of Subtraction Arithmetic Operator ‘-‘ and Assignment operator ‘=’.
Example
int a = 10 ;
int b = 5 ;
a -= b ; // a = a – b ; i.e., a = 10 – 5 = 5
Output
a = 5
-
*=
These Shorthand Operators use a combination of Multiplication type of Arithmetic Operator ‘*’ and Simple Assignment operator ‘=‘. The variable to the left is multiplied with the value or expression to the right side and then the result is stored into the variable defined to the left.
Example
int x = 10 ;
int y = 5 ;
x *= y ; // x = x * y ; i.e., x = 10 * 5 = 50
Output
x= 50
-
/=
Here, a combination of Division Arithmetic operator ‘/’ and Simple Assignment Operator ‘=’ is seen. The variable is divided by the value first and then the value is stored to the left, in the variable.
Example
float a = 10.25 ;
float b = 5.0 ;
a /= b ; // a = a / b ; i.e., a = 10.25 / 5.0 = 2.05
Output
a = 2.05
-
%=
This type of operator uses Modulus Operator ‘%’ and an Assignment Operator ‘=’ in its syntax. It returns the Remainder of the two operands and stores the value in the variable to the left side of the statement.
Example
int count = 10 ;
int num = 3 ;
count %= num ; // count = count % num ; i.e., count = 10 % 3 = 1 (Remainder)
Output
count = 1
Apart from the Assignment Operators, the Shorthand Operators are also used to define the Bitwise Operators.
Some of them are:
Operator | Description |
|= | Shorthand Bitwise Inclusive OR |
&= | Shorthand Bitwise AND |
^= | Shorthand Bitwise XOR |
<<= | Shorthand Bitwise Left Shift |
>>= | Shorthand Bitwise Right Shift |
~= | Shorthand Bitwise Compliment |
!= | Shorthand Inequality |
FAQs on Shorthand Operators
Q1. Which of the following is a valid assignment operator?
- *=
- +=
- %=
- All of the Above
Answer. Option D
Q2. What does the *= assignment operator do?
- Multiplies the value twice
- Multiplies variable with value once
- Used as exponent like 2*3 = 8
- None of the Above
Answer. Option B
Q3. Do the given two equations mean the same?
count += 1 ;
count = count + 1 ;
- True
- False
Answer. Option A
Q4. What is the final output of variable ‘a’ in the below program?
int a = 10 ;
int x = 5 ;
a *= 4 ;
x %= 2 ;
a += x ;
cout<<”Value of a = ” << a << endl ;
- 40
- 39
- 41
- 50
Answer. Option C.
Solution.
Step 1: a = a * 4 ;
a = 10 * 4 = 40
Step 2: x = x % 2 ;
x = 5 % 2 = 1 (Remainder)
Step 3: a = a + x
a = 40 + 1 = 41
Leave a Reply