You are currently viewing Dart Assignment Operators

Dart Assignment Operators

Dart, a versatile and modern programming language, has gained popularity for its simplicity and effectiveness in developing both mobile and web applications. One essential aspect of Dart programming is its assignment operators, which are important in managing and manipulating variables. In this article, we’ll explore Dart assignment operators, and providing code examples to solidify your understanding.

Introduction to Assignment Operators

In Dart, assignment operators are used to assign values to variables. While the basic assignment operator (=) accomplishes this task, Dart provides several compound assignment operators that combine assignment with other operations. These compound operators not only make code more concise but also enhance readability.

The Basic Assignment Operator (=)

Let’s start by revisiting the basic assignment operator. In Dart, you use = to assign a value to a variable:

void main() {

	int a = 10;
	String message = "Hello, Dart!";

	print(a); // Output: 10
	print(message); // Output: 'Hello, Dart!'

}

The variable a now holds the value 10, and message contains the string “Hello, Dart!”.

Compound Assignment Operators

Dart offers compound assignment operators that enable you to perform an operation and assign the result in a single step. These operators are shortcuts to streamline code and enhance efficiency.

Addition Assignment Operator (+=)

The addition assignment operator (+=) adds the right operand to the left operand and assigns the result to the left operand:

void main() {

	int b = 5;
	b += 3; // Equivalent to: b = b + 3
	
	print(b); // Output: 8
	
}

In this example, the value of b is initially 5. After using += with the value 3, b is updated to 8.

Subtraction Assignment Operator (-=)

The subtraction assignment operator (-=) subtracts the right operand from the left operand and assigns the result to the left operand:

void main() {

	int c = 10;
	c -= 4; // Equivalent to: c = c - 4
	
	print(c); // Output: 6
	
}

Here, the value of c is initially 10, and after using -= with the value 4, c becomes 6.

Multiplication Assignment Operator (*=)

The multiplication assignment operator (*=) multiplies the left operand by the right operand and assigns the result to the left operand:

void main() {

	int d = 3;
	d *= 2; // Equivalent to: d = d * 2

	print(d); // Output: 6
	
}

Initially, the value of d is 3, and after using *= with the value 2, d is updated to 6.

Division Assignment Operator (/=)

The division assignment operator (/=) divides the left operand by the right operand and assigns the result to the left operand:

void main() {

	double e = 8.0;
	e /= 4; // Equivalent to: e = e / 4

	print(e); // Output: 2.0
	
}

In this example, the value of e is initially 8.0, and after using /= with the value 4, e becomes 2.0.

Modulus Assignment Operator (%=)

The modulus assignment operator (%=) divides the left operand by the right operand and assigns the remainder to the left operand:

void main() {

	int f = 17;
	f %= 5; // Equivalent to: f = f % 5

	print(f); // Output: 2

}

Here, the value of f is initially 17, and after using %= with the value 5, f holds the remainder, which is 2.

Integer Division Assignment Operator (~/=)

The integer division operator (~/) divides two numbers and returns the integer result, discarding any remainder. The ~/= operator combines this division with assignment.

void main() {

    int g = 17;
    g ~/= 4; // Equivalent to g = g ~/ 4

    print(g); // Output: 4

}

In this example, 17 divided by 4 results in 4.25, but the ~/= operator truncates the decimal part, leaving 4 as the final value of g.

Bitwise AND Assignment Operator (&=)

The bitwise AND operator (&) performs a bitwise AND operation on the corresponding bits of two numbers. The &= operator combines AND with assignment.

void main() {

	int h = 7;
	h &= 3; // Equivalent to h = h & 3

	print(h); // Output: 3
  
}

The binary representation of h (which is 0111) ANDed with 3 (which is 0011) results in 0011, which is 3 in decimal.

Bitwise OR Assignment (|=)

The bitwise OR operator (|) performs a bitwise OR operation on the corresponding bits of two numbers. The |= operator combines OR with assignment.

void main() {

	int i = 5;
	i |= 3; // Equivalent to i = i | 3

	print(i); // Output: 7
  
}

The binary representation of i (which is 0101) ORed with 3 (which is 0011) results in 0111, which is 7 in decimal.

Bitwise XOR Assignment (^=)

The bitwise XOR operator (^) performs an exclusive OR operation on the corresponding bits of two numbers. The ^= operator combines XOR with assignment.

void main() {

	int j = 5;
	j ^= 3; // Equivalent to j = j ^ 3

	print(j); // Output: 6
  
}

The binary representation of j (which is 0101) XORed with 3 (which is 0011) results in 0110, which is 6 in decimal.

Left Shift Assignment (<<=)

The left shift operator (<<) shifts the bits of a number to the left. The <<= operator combines this shifting operation with assignment.

void main() {

  int k = 3;
  k <<= 2; // Equivalent to k = k << 2
  
  print(k); // Output: 12
  
}

The binary representation of k (which is 0011) is shifted two positions to the left, resulting in 1100, which is 12 in decimal.

Right Shift Assignment Operator (>>=)

The >>= operator performs a signed right shift operation and assigns the result to the left operand. It shifts the bits to the right, filling the leftmost bits with the sign bit (0 for positive numbers, 1 for negative numbers).

void main() {

  int l = -16;
  l >>= 2; // Equivalent to l = l >> 2
  
  print(l); // Output: -4
  
}

The binary representation of -16 (1111 0000 in two’s complement) is shifted two positions to the right, resulting in -4 (1111 1100).

Unsigned Right Shift Assignment (>>>=)

The unsigned right shift operator (>>>) shifts the bits of a number to the right, filling the leftmost bits with zeros. The >>>= operator combines this shifting operation with assignment.

void main() {

  int m = 16;
  m >>>= 2; // Equivalent to m = m >>> 2
  
  print(m); // Output: 4
  
}

In this example, the binary representation of m (which is 0001 0000) is shifted two positions to the right, resulting in 0000 0100, which is 4 in decimal.

Null-aware Assignment Operators

Introduced in Dart 2.12, null-aware assignment operators provide a concise way to assign a value to a variable only if the variable is currently null. These operators include ??= and ??:

??= (Assign if Null)

The ??= operator assigns the value on the right to the variable on the left only if the variable is currently null.

void main() {

  int? n; // Nullable variable
  n ??= 10; // Equivalent to n = n ?? 10
  
  print(n); // Output: 10
  
}

In this example, n is assigned the value 10 only if its current value is null. Otherwise, the existing value remains unchanged.

?? (Null-aware Conditional Operator)

The ?? operator is known as the null-aware conditional operator. It returns the expression on the left if it is non-null; otherwise, it returns the expression on the right.

void main() {

  int? x; // Nullable variable
  int y = 5;
  
  int result = x ?? y;
  
  print(result); // Output: 5
  
}

Here, result is assigned the value of x if x is non-null; otherwise, it is assigned the value of y.

Conclusion

In conclusion, Dart assignment operators are essential tools for manipulating variables efficiently and concisely. From basic assignment to compound and null-aware assignment operators, Dart provides a rich set of options for developers. Understanding these operators and their use cases is essential for writing clean, readable, and efficient Dart code.

References:

Leave a Reply