Operators in Dart
Operators are used to perform mathematical and logical operations on the variables. Each operation in dart uses a symbol called the operator to denote the type of operation it performs. Before learning operators in the dart, you must understand the following things.
Operands : It represents the data. Operator It represents how the operands will be processed to produce a value.
Note
Suppose the given expression is 2 + 3. Here 2 and 3 are operands, and + is the operator.
Types Of Operatorsβ
- Arithmetic Operators
- Increment and Decrement Operators
- Assignment Operators
- Logical Operators
- Type Test Operators
Arithmetic Operatorsβ
Arithmetic operators are the most common types of operators. They perform operations like addition, subtraction, multiplication, division, etc.
Operator Symbol | Operation Name | Description |
---|---|---|
+ | Addition | For adding two operands |
- | Subtraction | For subtracting two operands |
-expr | Unary | Minus For reversing the sign of the expression |
* | Multiplication | For multiplying two operands |
/ | Division | For dividing two operands and give output in double |
~/ | Division | For dividing two operands and give output in integer |
% | Modulus | Remainder After Integer Division |
++ | Increment | Increase Value By 1. For E.g a++; |
-- | Decrement | Decrease Value By 1. For E.g aβ; |
Letβs look at how to perform arithmetic calculations in dart.
Increment and Decrement Operatorsβ
With increment and decrement operators, you can increase and decrease values. If ++
is used at the beginning, then it
is a prefix. If it is used at last, then it is postfix.
Operator Symbol | Operation Name | Description |
---|---|---|
++var | Pre Increment | Increase Value By 1. var = var + 1 Expression value is var+1 |
--var | Pre Decrement | Decrease Value By 1. var = var - 1 Expression value is var-1 |
var++ | Post Increment | Increase Value By 1. var = var + 1 Expression value is var |
var-- | Post Decrement | Decrease Value By 1. var = var - 1 Expression value is var |
Note
++var increases the value of operands, whereas var++ return the actual value of operands before the increment.
Assignment Operatorsβ
It is used to assign some values to variables. Here, we are assigning 24 to the age variable.
int age = 24;
Operator Type | Description |
---|---|
= | Assign a value to a variable |
+= | Adds a value to a variable |
-= | Reduces a value to a variable |
*= | Multiply value to a variable |
/= | Divided value by a variable |
Note
To assign only if the assign-to variable is null use ??
operator.
Relational Operatorsβ
Relational operators are also called comparison operators. They are used to make a comparison.
Operator Symbol | Operation Name | Description |
---|---|---|
> | Greater than | Used to check which operand is bigger and gives result as boolean |
< | Less than | Used to check which operand is smaller and gives result as boolean |
>= | Greater than or equal to | Used to check which operand is bigger or equal and gives result as boolean |
<= | Less than or equal to | Used to check which operand is smaller or equal and gives result as boolean |
== | Equal to | Used to check operands are equal to each other and gives result as boolean |
!= | Not equal to | Used to check operand are not equal to each other and gives result as boolean |
Logical Operatorsβ
It is used to compare values.
Operator Type | Description |
---|---|
&& | This is βandβ, return true if all conditions are true |
2 equal | This is βorβ. Return true if one of the conditions is true |
! | This is βnotβ. return false if the result is true and vice versa |
Type Test Operatorsβ
In dart, type test operators are useful for checking types at runtime.
Operator Symbol | Operation Name | Description |
---|---|---|
is | is | Gives boolean value true if the object has a specific type |
is! | is not | Gives boolean value false if the object has a specific type |