# DART – Operators

**An operator** is a symbol that tells of performing specific mathematical, relational or logical operations.

The Dart has numerous built-in operators which can be used to carry out different functions, for example, ‘+’ is used to add two operands. Operators are meant to carry out operations on one or two operands.

## Types of Operators

Dart supports the following types of operators.

- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Type test Operators
- Logical Operators
- Bitwise Operator
- Conditional Operators
- Cascade notation

## Arithmetic Operators

Arithmetic operators perform addition, subtraction, multiplication, division, etc.

Operator Name | Description | ||
---|---|---|---|

1. | Addition(+) | It adds the left operand to the right operand. | |

2. | Subtraction(-) | It subtracts the right operand from the left operand. | |

3 | Divide(/) | It divides the first operand by the second operand and returns the quotient. | |

4. | Multiplication(*) | It multiplies one operand to another operand. | |

5. | Modulus(%) | It returns a reminder after dividing one operand into another. | |

6. | Division(/) | It divides the first operand by the second operand and returns an integer quotient. | |

7. | Unary Minus(-expr) | It is used with a single operand that changes its sign of it. |

void main(){ var a = 10; var b = 5; print("a+b = ${a+b}"); print("a-b = ${a-b}"); print("a*b = ${a*b}"); print("a/b = ${a/b}"); print("a%b = ${a%b}"); }

## Assignment Operator

Assignment operators are used to assign value to the variables.

Operators | Name | |
---|---|---|

= (Assignment Operator) | It assigns the right expression to the left operand. | |

+=(Add and Assign) | It adds the right operand value to the left operand and the resultant assigned it back to the left operand. For example – a+=b → a = a+b → 30 | |

-=(Subtract and Assign) | It subtracts the right operand value from the left operand and the resultant assigns it back to the left operand. For example – a-=b → a = a-b → 10 | |

*=(Multiply and Assign) | It multiplies the operands and resultant assigns them back to the left operand. For example – a*=b → a = a*b → 200 | |

/=(Divide and Assign) | It divides the left operand value by the right operand and the resultant assigned back to the left operand. For example – a%=b → a = a%b → 2.0 | |

~/=(Divide and Assign) | It divides the left operand value by the right operand and integer remainder quotient back to the left operand. For example – a%=b → a = a%b → 2 | |

%=(Mod and Assign) | It divides the left operand value by the right operand and the remainder assigns it back to the left operand. For example – a%=b → a = a%b → 0 |

void main(){ var a = 10; var b= 5; a+=b; print("a+=b= ${a}"); a-=b; print("a-=b= ${a}"); a*=b; print("a*=b = ${a}"); a~/=b; print("a~/=b= ${a}"); a%=b; print("a%=b= ${a}"); }

## Relational Operator

Relational operators or Comparison operators are used to make a comparison between two expressions and operands. The comparison of two expressions returns the Boolean true and false.

Operator | Meaning |
---|---|

`==` | Equal; see discussion below |

`!=` | Not equal |

`>` | Greater than |

`<` | Less than |

`>=` | Greater than or equal to |

`<=` | Less than or equal to |

## Type test operators

The `as`

, `is`

, and `is!`

operators are handy for checking types at runtime.

Operator | Meaning |
---|---|

`as` | Typecast (also used to specify library prefixes) |

`is` | True if the object has the specified type |

`is!` | True if the object doesn’t have the specified type |

## Logical operators

You can invert or combine boolean expressions using logical operators.

Operator | Meaning |
---|---|

`!` | inverts the following expression (changes false to true, and vice versa) |

`||` | logical OR |

`&&` | logical AND |

## Dart Bitwise Operators

The Bitwise operators perform operations bit by bit on the value of the two operands.

Operator | Meaning |
---|---|

`&` | AND |

`|` | OR |

`^` | XOR |

`~` | Unary bitwise complement (0s become 1s; 1s become 0s) |

`<<` | Shift left |

`>>` | Shift right |

`>>>` | Unsigned shift right |

## Dart Conditional Operators (?:)

The Conditional Operator is the same as the if-else statement and provides similar functionality as a conditional statement. It is the second form of an if-else statement. It is also identified as a **“Ternary Operator”**. The syntax is given below.

**Syntax 1 –**

`condition ? exp1 : exp2`

If the given condition is TRUE then it returns exp1 otherwise exp2.

`exp1 ?? expr2`

If the exp1 is not-null, returns its value, otherwise returns the exp2’s value.

## Cascade notation

Cascades (`..`

, `?..`

) allow you to make a sequence of operations on the same object. In addition to accessing instance members, you can also call instance methods on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.

## Average Rating