ePrivacy and GPDR Cookie Consent by CookieConsent.com
menu

Dart Programming - Operators in dart

An expression in dart is made of variables, number, strings, operators which are created with the syntax of the language. Expressions are made up of two things 
  1. Operands: Operands are the data of the expression.
  2. Operators: Operators give orders to the operands about what to do.
In dart, there are the following types of operators.
  • Arithmetic operators
  • Logical operators
  • Assignment operators
  • Equality and relational operator
  • Type test operator
  • Bitwise operator

Arithmetic operators:

Arithmetic operators are used for performing any arithmetic operation on the operands. They are the following.
+-----------------+----------------+-----------------------------------------+
| Operator Symbol | Operator Name  | Operator Description                    |
+-----------------+----------------+-----------------------------------------+
|        +        | Addition       | Adds two numbers                        |
+-----------------+----------------+-----------------------------------------+
|        -        | Subtraction    | Subtracts two numbers                   |
+-----------------+----------------+-----------------------------------------+
|        *        | Multiplication | Multiplies two numbers                  |
+-----------------+----------------+-----------------------------------------+
|        /        | Division       | Divides two numbers                     |
+-----------------+----------------+-----------------------------------------+
|       ~/        | Division       | Divide to number and gives the results  |
|                 |                | in integer                              |
+-----------------+----------------+-----------------------------------------+
|        %        | Modulus        | Calculates the remainder of two numbers |
+-----------------+----------------+-----------------------------------------+



Relational Operator

Relational operators in dart are used to test true and false. Relational operators are used to finding the relation between the operands. Dart relational operators are following

+-----------------+---------------+-------------------------------------+
| Operator Symbol | Operator Name | Operator Description                |
+-----------------+---------------+-------------------------------------+
|        >        | Greater than  | Checks the bigger operand           |
+-----------------+---------------+-------------------------------------+
|        <        | Less than     | Checks the smaller operand          |
+-----------------+---------------+-------------------------------------+
|        >=       | Greater than, | Checks the bigger or equal operand  |
|                 | equal to      |                                     |
+-----------------+---------------+-------------------------------------+
|        <=       | Less than,    | Checks the smaller or equal operand |
|                 | equal to      |                                     |
+-----------------+---------------+-------------------------------------+
|        ==       | Equal to      | Checks two operands and return      |
|                 |               | true if both are equal              |
+-----------------+---------------+-------------------------------------+
|        !=       | Not equal to  | Checks two operands and return      |
|                 |               | true if both are not equal          |
+-----------------+---------------+-------------------------------------+



Logical operator

Logical operators are used to defining logic between the operands. Logical operators are following
+-----------------+---------------+---------------------------------------------+
| Operator Symbol | Operator Name | Operator Description                        |
+-----------------+---------------+---------------------------------------------+
|        &&       | AND           | Returns true if both operators are true     |
+-----------------+---------------+---------------------------------------------+
|        ||       | OR            | Returns true if one of any operator is true |
+-----------------+---------------+---------------------------------------------+
|        !        | NOT           | It reverses the return result               |
+-----------------+---------------+---------------------------------------------+




Assignment operator

The assignment operator as the name suggests is used to assign some data to another variable. Assignment operators in dart are following.

+-----------------+---------------------+----------------------+
| Operator Symbol | Operator Name       | Operator Description |
+-----------------+---------------------+----------------------+
|        =        | Equal to            | Used to assign some  |
|                 |                     | Data to the variable |
+-----------------+---------------------+----------------------+
|       ??=       | Assignment operator | Assign the value if  |
|                 |                     | the variable is null |
+-----------------+---------------------+----------------------+



Type test operator

Type test operators are used to test the type of variable. 
+-----------------+---------------+----------------------------------------+
| Operator Symbol | Operator Name | Operator Description                   |
+-----------------+---------------+----------------------------------------+
|        is       | is operator   | returns true is the given variable     |
|                 |               | is equal to your desired variable      |
+-----------------+---------------+----------------------------------------+
|       is!       | is! operator  | returns true if the given variable is  |
|                 |               | not equal to you desired variable      |
+-----------------+---------------+----------------------------------------+

Bitwise operator

Bitwise operators are used for performing actions on binary numbers at the bit level. In dart language, bitwise operators are following.
 
+-----------------+---------------+--------------------------------------------------------------------+---------------------+
| Operator Symbol | Operator Name | Operator Description                                               | Operator Example    |
+-----------------+---------------+--------------------------------------------------------------------+---------------------+
|        &        | Bitwise AND   | sets each bit to one if both sides are one.                        | 0101 & 1011 == 0001 |
+-----------------+---------------+--------------------------------------------------------------------+---------------------+
|        |        | Bitwise OR    | Sets each bit to 1 if one of two bits is 1                         | 0001 | 1100 == 1101 |
+-----------------+---------------+--------------------------------------------------------------------+---------------------+
|        ^        | Bitwise XOR   | Sets each bit to 1 if only one of two bits is 1                    | 0101 ^ 0011 == 0110 |
+-----------------+---------------+--------------------------------------------------------------------+---------------------+
|        ~        | Bitwise NOT   | Inverts all the bits                                               | ~0110 == 1001       |
+-----------------+---------------+--------------------------------------------------------------------+---------------------+
|        <<       | Left shift    | Shifts left by pushing zeros in from the                           | 0101 << 1           |
|                 |               | right and let the leftmost bits fall off                           |                     |
+-----------------+---------------+--------------------------------------------------------------------+---------------------+
|        >>       | right shift   | Shifts right by pushing copies of the                              | 0101 << 1           |
|                 |               | leftmost bit in from the left, and let the rightmost bits fall off |                     |
+-----------------+---------------+--------------------------------------------------------------------+---------------------+



Dart operator precedence

+-----------------------+--------------------------------+---------------+------------+
| Description Operator  |            Operator            | Associativity | Precedence |
+-----------------------+--------------------------------+---------------+------------+
|     Unary postfix     | e., e?., e++, e--, e1[e2], e() |      None     |     16     |
+-----------------------+--------------------------------+---------------+------------+
|     Unary postfix     |  -e, !e, ˜e, ++e, --e, await e |      None     |     15     |
+-----------------------+--------------------------------+---------------+------------+
|     Multiplicative    |           *, /, ˜/, %          |      Left     |     14     |
+-----------------------+--------------------------------+---------------+------------+
|        Additive       |               +,-              |      Left     |     13     |
+-----------------------+--------------------------------+---------------+------------+
|         Shift         |            <<,>>,>>>           |      Left     |     12     |
+-----------------------+--------------------------------+---------------+------------+
|      Bitwise AND      |                &               |      Left     |     11     |
+-----------------------+--------------------------------+---------------+------------+
|      Bitwise XOR      |                ^               |      Left     |     10     |
+-----------------------+--------------------------------+---------------+------------+
|       Bitwise OR      |                |               |      Left     |      9     |
+-----------------------+--------------------------------+---------------+------------+
|       Relational      |    <, >, <=, >=, as, is, is!   |      None     |      8     |
+-----------------------+--------------------------------+---------------+------------+
|        Equality       |             ==, !=             |      None     |      7     |
+-----------------------+--------------------------------+---------------+------------+
|      Logical AND      |               &&               |      Left     |      6     |
+-----------------------+--------------------------------+---------------+------------+
|       Logical OR      |               ||               |      Left     |      5     |
+-----------------------+--------------------------------+---------------+------------+
|        if-null        |               ??               |      Left     |      4     |
+-----------------------+--------------------------------+---------------+------------+
|      Conditional      |          e1 ? e2 : e3          |     Right     |      3     |
+-----------------------+--------------------------------+---------------+------------+
|        Cascade        |               ..               |      Left     |      2     |
+-----------------------+--------------------------------+---------------+------------+
|       Assignment      | =, *=, /=, +=, -=, &=, ˆ=, etc |     Right     |      1     |
+-----------------------+--------------------------------+---------------+------------+


Comments