## What are operators in Dart ?

In Dart, operators are symbols or keywords that perform specific operations on one or more operands (values or variables). Dart supports various types of operators that can be used for arithmetic, assignment, comparison, logical operations, and more. Here’s an explanation of some common operators in Dart with examples:

Every expression is composed of two parts:

They represents data.*Operand —*An operator is a symbol that tells the compiler to perform a specific mathematical, relational, or logical operation and produce a final result.*Operator —*

- For example, A + B where
- Here
and*A*are operands and*B*is an operator.*+*

# Types of Operators in Dart

- There are total of eight types of operators are there in Dart.

- Arithmetic Operator
- Assignment Operator
- Bitwise operator
- Equality Operator
- Logical Operator
- Relational Operator
- Short circuit operator
- Type Test Operator

# Arithmetic Operator

- There are nine types of an arithmetic operator are there in a dart.

Operators | Description |

+ | Add (a + b) |

– | Subtract (a – b) |

-expr | Unary Minus ( use to reverse the sign of the expression) |

* | Multiply (a * b) |

/ | Divide (a / b) |

~/ | Divides but return an integer result |

% | Modulus ( outputs remainder of two operands) |

++ | Increment |

— | Decrement |

```
void main()
{
int x = 14;
int y = 5;
// Using ~/ to divide x and y
var z = x ~/ y;
print("Quotient of x and y: $z");
// Remainder of a and b
var d = x % y;
print("Remainder of x and y: $d");
}
//
Output
Quotient of x and y: 2
Remainder of x and y: 4
```

# Relational Operator and Equality operator

- Relational operator shows the relation between the two operands.
- The relational operator can have only two values either
or*true*.*false* - There are six relational operators are there.
- Equality operator checks whether two objects are equal or not.
- Same as relational operator they contain Boolean value either
or*true*.*false*

Operators | Description |

> | Greater than (a > b) |

< | Less than (a < b) |

>= | Greater than or equal to (a >= b) |

<= | Less than or equal to (a <= b) |

== | Equal to (a == b) |

!= | Not equal to (a != b) |

```
void main()
{
int x = 14;
int y = 4;
// Greater between x and y
var z = x > y;
print("x is greater than y is $z");
// Equality between x and y
var d = x == y;
print("x and y are equal is $d");
}
Output
x is greater than y is true
x and y are equal is false
```

# Assignment operators

These operators are used to assign value to operands.

Operators | Description |

= | Equal to ( assign values to the expression) |

??= | Assignment operator ( assign the value only if it is null ) |

```
void main()
{
int x = 14;
int y = 5;
// Assigning value to variable z
var z = x / y;
print(z);
// Assigning value to variable i
var i; //i has a null value
i ??= x * y; // Value is assign to i as it is null
print(i);
// reassign value to i
i ??= x + y; // Value is not assign because i is not null
print(i);
}
Output
2.8
70
70
```

# Logical operators

These operators are used to combine two or more conditions. A Boolean value is returned by logical operators.

Operators used include:

Operators | Description |

&& | And operator ( returns true if both conditions are true) |

|| | Or operator ( returns true if one of the conditions is true) |

! | Not Operator ( reverse the output) |

```
void main()
{
int x = 14;
int y = 5;
// Using And Operator
bool z = x > 5 && y == 5;
print(z);
// Using Or Operator
bool i = x < 20 || y > 15;
print(i);
// Using Not Operator
bool d = !(x > 5);
print(d);
}
Output
true
true
false
```

# Ternary operator or Conditional operator

- Dart has two special operators which can help you to evaluate small expressions that might require an entire if-else statement.

## Condition ? expr1 : expr2

- Here if the condition is true then the
**expr1**is evaluated and returns its value. Otherwise,**expr2**is evaluated and returns the value.

```
void main()
{
int x = 5;
int y = 7;
// Conditional Statement
var z = (x * y > 30) ? "First expression is printed" : "Second expression is printed";
print(z);
}
Output
First expression is printed
```

# Type Test Operator

- Type test operators are used to check the data type.

These operators are used to perform operand comparisons and are useful for checking types at the runtime of a program. Such operators include:

Operators | Description |

is | is (output a Boolean value true if the object has its specific type) |

is! | is not (output a Boolean value false if the object has its specific type) |

```
void main() {
// Type Test Operator
String e = 'Operators in Dart';
// Using is to compare
print(e is String);
}
Output
true
```

# Bitwise operators

These operators perform bitwise operations on the operands. Below are the operators used:

Operators | Description |

& | Bitwise And (a & b) returns a one in each bit position where both operands’ corresponding bits are ones. |

| | Bitwise And (a | b) returns a one for each bit position where the matching bits of either or both operands are ones. |

^ | Bitwise XOR (a ^ b) returns a one in each bit location when the corresponding bits of either but not both operands are ones. |

~ | Bitwise NOT( ~a) inverts the operand’s bits. |

<< | Left shift (a << b) Shifts a in binary representation b (< 32) bits to the left, shifting in zeroes from the right. |

>> | Right shift (a >> b) shifts a in binary representation b (< 32) bits to the right, eliminating bits shifted off. |

```
void main() {
int c = 6;
// Bitwise operator
var bitwise_res = ~ c;
print("Bitwise result: $bitwise_res" );
}
Output
Bitwise result: -7
```

# Cascade notation operators

These operators allow you to perform a sequence of operations on the same element and also perform multiple methods on the same object.

Operator | Description |

.. | Cascading method (performs multiple methods on the same object) |

```
class FlutterFever{
var title;
var id;
var date;
void set(x, y, z)
{
this.title = x;
this.id = y;
this.date = z;
}
void shot()
{
var myShot = this.title + this.id + this.date;
print(myShot);
}
}
void main()
{
// Creating objects of class EducativeShot
FlutterFevershot1 = new FlutterFever();
FlutterFevershot2 = new FlutterFever();
// Without using Cascade Notation
shot1.set('Operators', ' 0017820', ' 15.10.2021');
shot1.shot();
// Using Cascade Notation
shot2..set('Operators in Dart programming by flutterfever.com', ' 0017820', ' 15.10.2021')
..shot();
}
Output
Operators 0017820 15.10.2021
Operators in Dart programming by flutterfever.com 0017820 15.10.2021
```

## What is precendence in dart ?

In Dart, operator precedence determines the order in which operators are evaluated when an expression contains multiple operators. It ensures that expressions are evaluated correctly and consistently. Here’s a summary of the operator precedence in Dart, from highest to lowest:

- Grouping:
`()`

(parentheses)- Parentheses are used to explicitly define the order of operations within an expression.

- Member Access:
`.`

(dot)- The dot operator is used to access members (properties and methods) of objects or classes.

- Indexing:
`[]`

(square brackets)- Square brackets are used for indexing, to access elements of lists, maps, or strings.

- Function Call:
`()`

- Parentheses are used to call functions and pass arguments.

- Unary Operators:
`!`

(logical negation)`-`

(negation or subtraction)

- Multiplicative Operators:
`*`

(multiplication)`/`

(division)`%`

(modulo)

- Additive Operators:
`+`

(addition)`-`

(subtraction)

- Relational Operators:
`<`

(less than)`>`

(greater than)`<=`

(less than or equal to)`>=`

(greater than or equal to)

- Equality Operators:
`==`

(equality)`!=`

(inequality)

- Logical AND:
`&&`

- Logical OR:
`||`

- Conditional Operator:
`? :`

(ternary operator) - Assignment Operators:

The higher the operator appears in the list, the higher its precedence. Operators with higher precedence are evaluated before operators with lower precedence. If operators have the same precedence, the evaluation order is determined by the associativity of the operators (left-to-right or right-to-left).

## What is associativity in dart ?

In Dart, associativity determines the order in which operators with the same precedence are evaluated when they appear consecutively in an expression. There are two types of associativity:

**Left-to-Right Associativity:**- Operators with left-to-right associativity are evaluated from left to right when they appear consecutively.
- Examples of left-to-right associative operators:
`+`

(addition),`-`

(subtraction),`*`

(multiplication),`/`

(division),`=`

(assignment), etc.

```
int result = 10 - 2 - 3; // Evaluates as (10 - 2) - 3 = 5
```

2.**Right-to-Left Associativity:**

- Operators with right-to-left associativity are evaluated from right to left when they appear consecutively.
- Example of right-to-left associative operator:
`=`

(assignment)

```
int x, y;
x = y = 10; // Evaluates as y = 10 and x = y
```

**Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it.**