Dart Assignment Operators


Assignment operators in Dart are used to assign values to variables. These operators not only allow you to set the initial value of a variable but also modify its value using various arithmetic operations. Here’s a detailed overview of the assignment operators available in Dart:

Overview of Assignment Operators

  1. Simple Assignment (=)
  2. Add and Assign (+=)
  3. Subtract and Assign (-=)
  4. Multiply and Assign (*=)
  5. Divide and Assign (/=)
  6. Integer Divide and Assign (~/=)
  7. Modulus and Assign (%=)

1. Simple Assignment (=)

  • Description: Assigns the value on the right to the variable on the left.
  • Usage: This is the basic assignment operator.

Example:

void main() { int a = 5; // Assigning 5 to variable a print('Value of a: $a'); // Output: Value of a: 5 }

2. Add and Assign (+=)

  • Description: Adds the right operand to the left operand and assigns the result to the left operand.
  • Usage: Commonly used to increment a variable.

Example:

void main() { int a = 5; a += 3; // Equivalent to a = a + 3 print('Value of a after +=: $a'); // Output: Value of a after +=: 8 }

3. Subtract and Assign (-=)

  • Description: Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Usage: Useful for decrementing a variable.

Example:

void main() { int a = 5; a -= 2; // Equivalent to a = a - 2 print('Value of a after -=: $a'); // Output: Value of a after -=: 3 }

4. Multiply and Assign (*=)

  • Description: Multiplies the left operand by the right operand and assigns the result to the left operand.
  • Usage: Often used for scaling a value.

Example:

void main() { int a = 5; a *= 4; // Equivalent to a = a * 4 print('Value of a after *=: $a'); // Output: Value of a after *=: 20 }

5. Divide and Assign (/=)

  • Description: Divides the left operand by the right operand and assigns the result to the left operand. The result is always a double.
  • Usage: Used for halving or dividing a value.

Example:

void main() { double a = 20; a /= 4; // Equivalent to a = a / 4 print('Value of a after /=: $a'); // Output: Value of a after /=: 5.0 }

6. Integer Divide and Assign (~/=)

  • Description: Performs integer division on the left operand by the right operand and assigns the result to the left operand. The result is an int.
  • Usage: Useful for scenarios where you need a whole number from a division.

Example:

void main() { int a = 20; a ~/= 6; // Equivalent to a = a ~/ 6 print('Value of a after ~/=: $a'); // Output: Value of a after ~/=: 3 }

7. Modulus and Assign (%=)

  • Description: Calculates the remainder of the left operand divided by the right operand and assigns the result to the left operand.
  • Usage: Often used to keep a value within a certain range.

Example:

void main() { int a = 20; a %= 6; // Equivalent to a = a % 6 print('Value of a after %=: $a'); // Output: Value of a after %=: 2 }

Summary of Assignment Operators

OperatorDescriptionExample
=Simple assignmenta = 5 (assigns 5 to a)
+=Add and assigna += 3 (equivalent to a = a + 3)
-=Subtract and assigna -= 2 (equivalent to a = a - 2)
*=Multiply and assigna *= 4 (equivalent to a = a * 4)
/=Divide and assigna /= 4 (equivalent to a = a / 4)
~/=Integer divide and assigna ~/= 6 (equivalent to a = a ~/ 6)
%=Modulus and assigna %= 6 (equivalent to a = a % 6)

Conclusion

Assignment operators in Dart provide a concise way to modify the values of variables using arithmetic operations. They enhance code readability and reduce the need for repetitive code by combining assignment and arithmetic in a single operator. Understanding how to use assignment operators effectively is essential for writing clean and efficient Dart applications.