×

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 Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

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

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.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute 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 URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

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 Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes 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 If-else-if statement

If - else - if statement enables to check the set of test expressions that evaluate to Boolean True or False, and based on this true or false the particular...

2 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart Objects

Apart from the built-in data types, Dart offers some other data types that have a special role to play. One of them is Objects.  Objects Objects are the foundation of the...

3 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 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 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 Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

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

4 minutes read.