×

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

Related Topics

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Abstract Classes in Dart

Abstract classes in Dart are those classes that only contain abstract methods  (methods that do not contain any implementation). These classes are specifically designed for the purpose of inheritance. We...

4 minutes read.

Dart Keywords

Keywords are the reserved words of a programming language, and they have a special meaning that comes defined by the language. These cannot be used as an identifier, and all...

1 minute read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

Interfaces in Dart

An interface in Dart refers to the syntax or blueprint that any class must adhere to. It basically defines the array of methods available on the object. It provides the...

3 minutes read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Type System in Dart

Dart language is a type safety enabled language that uses static and dynamic type checking to match the value of the variable with its data type at the compile-time. This...

4 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Iterable

Iterable is a collection of elements that are accessed sequentially. These elements are accessible using the iterator getter and stepping through the values using this getter. Let us understand the setting...

6 minutes read.