Dart Operators
An operator is a symbol that is used for manipulating the values or performs operations on its operands. The given expression: 1+4, in this expression, 1 and 4 are operands and '+' is the operator. Dart programming language provides an extensive set of built-in operators to accomplish various types of operations. Operators can be unary or binary, which means unary take only on operand and binary take two operands along with operators.
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
Casecade notation(..) Operators
Arithmetic Operators
Arithmetic Operators are the most common operators that are used to perform addition, subtraction, multiplication, divide, etc.
Operator Name | Description |
---|---|
Addition(+) | It adds the left operand to the right operand. |
Subtraction(-) | It subtracts the right operand from the left operand. |
Divide(/) | It divides the first operand by the second operand and returns quotient. |
Multiplication(*) | It multiplies the one operand to another operand. |
Modulus(%) | It returns a reminder after dividing one operand to another. |
Division(~/) | It divides the first operand by the second operand and returns integer quotient. |
Unary Minus(-expr) | It is used with a single operand changes the sign of it. |
Assignment Operators
Assignment operators are used to assigning value to the variables. We can also use it combined with the arithmetic operators.
Operators Name | Description |
---|---|
= (Assignment Operator) | It assigns the right expression to the left operand. |
+=(Add and Assign) | It adds right operand value to the left operand and resultant assign back to the left operand. |
-=(Subtract and Assign) | It subtracts right operand value from left operand and resultant assign back to the left operand. |
*=(Multiply and Assign) | It multiplies the operands and resultant assign back to the left operand. |
/=(Divide and Assign) | It divides the left operand value by the right operand and resultant assign back to the left operand. |
~/=(Divide and Assign) | It divides the left operand value by the right operand and integer remainder quotient back to the left operand. |
%=(Mod and Assign) | It divides the left operand value by the right operand and remainder assign back to the left operand. |
«=(Left shift AND assign) | The expression a«=3 is equal to a = a«3 |
»=(Right shift AND assign) | The expression a»=3 is equal to a = a»3 |
Relational Operator
Relational operators or Comparison operators are used to making a comparison between two expressions and operands.
Operator | Description |
---|---|
>(greater than) | a>b will return TRUE. |
<(less than) | a<b will return FALSE. |
>=(greater than or equal to) | a>=b will return TRUE. |
<=(less than or equal to) | a<=b will return FALSE. |
==(is equal to) | a==b will return FALSE. |
!=(not equal to) | a!=b will return TRUE. |
Dart Type Test Operators
The Type Test Operators are used to testing the types of expressions at runtime. Consider the following table.
Operator | Description |
---|---|
as | It is used for typecast. |
is | It returns TRUE if the object has specified type. |
is! | It returns TRUE if the object has not specified type. |
Dart Logical Operators
The Logical Operators are used to evaluate the expressions and make the decision. Dart supports the following logical operators.
Operator | Description |
---|---|
&&(Logical AND) | It returns if all expressions are true. |
(Logical OR) | It returns TRUE if any expression is true. |
!(Logical NOT) | It returns the complement of expression. |
Dart Bitwise Operators
The Bitwise operators perform operation bit by bit on the value of the two operands. Following is the table of bitwise operators.
Operators | Description |
---|---|
&(Binary AND) | It returns 1 if both bits are 1. |
(Binary OR) | |
^(Binary XOR) | It returns 1 if both bits are different. |
~(Ones Compliment) | It returns the reverse of the bit. If bit is 0 then the compliment will be 1. |
«(Shift left) | The value of left operand moves left by the number of bits present in the right operand. |
»(Shift right) | The value of right operand moves left by the number of bits present in the left operand. |
Dart Conditional Operators (?:)
The Conditional Operator is same as if-else statement and provides similar functionality as conditional statement.
It is the second form of if-else statement. It is also identified as Ternary Operator
.
Syntax 1
condition ? exp1 : exp2
If the given condition is TRUE then it returns exp1 otherwise exp2.
Syntax 2
exp1 ?? expr2
Example
void main() {
var a = 30;
var output = a > 42 ? "value greater than 10":"value lesser than equal to 30";
print(output);
}
Output:
value lesser than or equal to 30
If the exp1 is not-null, returns its value, otherwise returns the exp2’s value.
Dart Cascade notation Operators
The Cascade notation Operators (..)
is used to evaluate a series of operation on the same object. It is an identical as the method chaining that avoids several of steps, and we don’t need store results in temporary variables.