Dart Programming - Assignment Operators



Assignment Operators

The following table lists the assignment operators available in Dart.

Sr.No Operator & Description
1 =(Simple Assignment )

Assigns values from the right side operand to the left side operand

Ex:C = A + B will assign the value of A + B into C

2 ??=

Assign the value only if the variable is null

3 +=(Add and Assignment)

It adds the right operand to the left operand and assigns the result to the left operand.

Ex: C += A is equivalent to C = C + A

4 =(Subtract and Assignment)

It subtracts the right operand from the left operand and assigns the result to the left operand.

Ex: C -= A is equivalent to C = C - A

5 *=(Multiply and Assignment)

It multiplies the right operand with the left operand and assigns the result to the left operand.

Ex: C *= A is equivalent to C = C * A

6 /=(Divide and Assignment)

It divides the left operand with the right operand and assigns the result to the left operand.

Note − Same logic applies to Bitwise operators, so they will become <=, >=, |= and ^=.

Usage of Assignment Operators

Example

The following example shows how you can use the assignment operators in Dart −

void main() { 
   var a = 12; 
   var b = 3; 
     
   a+=b; 
   print("a+=b : ${a}"); 
     
   a = 12; b = 13; 
   a-=b; 
   print("a-=b : ${a}"); 
     
   a = 12; b = 13; 
   a*=b; 
   print("a*=b' : ${a}"); 
     
   a = 12; b = 13; 
   a/=b;
   print("a/=b : ${a}"); 
     
   a = 12; b = 13; 
   a%=b; 
   print("a%=b : ${a}"); 
}    

Output

It will produce the following output

a+=b : 15                         
a-=b : -1                             
a*=b' : 156                                    
a/=b :0.9230769230769231                       
a%=b : 12
dart_programming_operators.htm
Advertisements