Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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