×

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

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart Runes and Graphemes

Runes and Graphemes data types in Dart Runes and Graphemes Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each...

2 minutes read.

Dart Built-in Data Types

A data type defines the type of value a variable can hold, such as integer, double, or string.  Dart supports two data types:  1. Built-in data types : These are the data...

1 minute read.

Dart Static Members

Static Members in Dart Static members are the members of the class declared using the 'static' keyword. the static members have the following characteristics :  All the objects of the class having...

3 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 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 HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Isolates

Dart successfully supports asynchronous programming which runs our program without any hindrance. This asynchronous programming is used to achieve concurrency. Dart supports concurrent programming with features such as ‘ async-await...

9 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.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 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.

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.

Lexical Scope and Closure

Lexical Scope : Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to...

2 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes 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 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 Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.