Dart Assignment Operator
Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use them by combining with the arithmetic or bitwise operators.
List of Assignment Operators
Suppose a holds value 20 and b holds 10.
Operators |
Name |
= ( Assignment Operator ) |
This operator assigns the value of the right expression to the left
operand. |
+= ( Add and Assign ) |
This operator adds the value of the right operand to the value of the left
operand and the sum is assigned back to the operand on the left. For example : – a += b → a = a + b → 30 |
-= ( Subtract and Assign ) |
This operator subtracts the value of the right operand value from the
value of the left operand and the difference is assigned back to the left
operand. For example : – a -= b → a = a - b → 10 |
*= ( Multiply and Assign ) |
This operator multiplies the value of both the operands and the
product is assigned back to the left operand. For example : – a *= b → a = a * b → 200 |
/= ( Divide and Assign ) |
This operator divides the value of the left operand by the value of
the right operand and the quotient of float type is assigned back to the left
operand. For example : – a /= b → a = a / b → 2.0 |
~/= ( Divide and Assign ) |
This operator divides the value of the left operand by the value of
the right operand and the quotient of integer type is returned back to the
left operand. For example : – a ~/= b → a = a ~/ b → 2 |
%= ( Mod and Assign ) |
This operator divides the value of the left operand by the value of
the right operand and the remainder is assigned back to the left operand. For example : – a %= b → a = a % b → 0 |
<<= ( Left shift AND assign ) |
This operator shifts the value of the left operand to the left by the
number of times denoted by the value of the right operand. For eample : - a
<<= 3 → a = a << 3 → 60 |
>>= ( Right shift AND assign ) |
This operator shifts the value of the left operand to the right by the
number of times denoted by the value of the right operand. For eample : - a
>>= 3 → a = a << 3 → 6 |
&= ( Bitwise AND assign ) |
This operator performs Bitwise AND on the two operands and assign the
resultant value to the operand on the left. For example : - a &= 3
→ a = a & 3 |
^= ( Bitwise exclusive OR and assign ) |
This operator performs Bitwise OR on the two operands and assign the
resultant value to the operand on the left. For example : - a ^= 3 → a
= a ^ 3 |
|= ( Bitwise inclusive OR and assign ) |
This operator performs Bitwise OR on the two operands and assign the
resultant value to the operand on the left. For example : - a |= 3 → a
= a | 3 |
??= |
Assigns the value only if the variable is null. |
Program
void main( )
{
// initializing n1 and n2 with assignment operator
var n1 = 10 ;
var n2 = 5 ;
// Add and Assign
n1 += n2 ;
print( " n1 += n2 = ${ n1 } " ) ;
// Subtract and Assign
n1 -= n2 ;
print( " n1 -= n2 = ${ n1 } " ) ;
// Multiply and Assign
n1 *= n2 ;
print( " n1 *= n2 = ${ n1 } " ) ;
// Divide and Assign
n1 ~/= n2 ;
print( " n1 ~/= n2 = ${ n1 } " ) ;
// Mode and Assign
n1 %= n2 ;
print( " n1 %= n2 = ${ n1 } " ) ;
// Bitwise inclusive OR and assign
n1 |= n2 ;
print( " n1 |= n2 = ${ n1 } " ) ;
// Bitwise exclusive OR and assign
n1 ^= n2 ;
print( " n1 ^= n2 = ${ n1 } " ) ;
// Bitwise AND assign
n1 &= n2 ;
print( " n1 &= n2 = ${ n1 } " ) ;
// Right shift AND assign
n1 >>= n2 ;
print( " n1 >>= n2 = ${ n1 } " ) ;
// Left shift AND assign
n1 <<= n2 ;
print( " n1 <<= n2 = ${ n1 } " ) ;
}
Output:
n1 += n2 = 15
n1 -= n2 = 10
n1 *= n2 = 50
n1 ~/= n2 = 10
n1 %= n2 = 0
n1 |= n2 = 5
n1 ^= n2 = 0
n1 &= n2 = 0
n1 >>= n2 = 0 n1 <<= n2 = 0