an expression is a special kind of statement that evaluates to a value. every expression is composed of −
operands − represents the data
operator − defines how the operands will be processed to produce a value.
consider the following expression – "2 + 3". in this expression, 2 and 3 are operands and the symbol "+" (plus) is the operator.
in this chapter, we will discuss the operators that are available in dart.
- arithmetic operators
- equality and relational operators
- type test operators
- bitwise operators
- assignment operators
- logical operators
arithmetic operators
the following table shows the arithmetic operators supported by dart.
sr.no | operators & meaning |
---|---|
1 |
+
add |
2 |
−
subtract |
3 |
-expr
unary minus, also known as negation (reverse the sign of the expression) |
4 |
*
multiply |
5 |
/
divide |
6 |
~/
divide, returning an integer result |
7 |
%
get the remainder of an integer division (modulo) |
8 |
++
increment |
9 |
--
decrement |
equality and relational operators
relational operators tests or defines the kind of relationship between two entities. relational operators return a boolean value i.e. true/ false.
assume the value of a is 10 and b is 20.
operator | description | example |
---|---|---|
> | greater than | (a > b) is false |
< | lesser than | (a < b) is true |
>= | greater than or equal to | (a >= b) is false |
<= | lesser than or equal to | (a <= b) is true |
== | equality | (a==b) is false |
!= | not equal | (a!=b) is true |
type test operators
these operators are handy for checking types at runtime.
operator | meaning |
---|---|
is | true if the object has the specified type |
is! | false if the object has the specified type |
bitwise operators
the following table lists the bitwise operators available in dart and their role −
operator | description | example |
---|---|---|
bitwise and | a & b | returns a one in each bit position for which the corresponding bits of both operands are ones. |
bitwise or | a | b | returns a one in each bit position for which the corresponding bits of either or both operands are ones. |
bitwise xor | a ^ b | returns a one in each bit position for which the corresponding bits of either but not both operands are ones. |
bitwise not | ~ a | inverts the bits of its operand. |
left shift | a < b | shifts a in binary representation b (< 32) bits to the left, shifting in zeroes from the right. |
signpropagating right shift | a > b | shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off. |
assignment operators
the following table lists the assignment operators available in dart.
sr.no | operator & description |
---|---|
1 |
=(simple assignment )
assigns values from the right side operand to the left side operand ex:c = a + b will assign the value of a + b into c |
2 |
??=
assign the value only if the variable is null |
3 |
+=(add and assignment)
it adds the right operand to the left operand and assigns the result to the left operand. ex: c += a is equivalent to c = c + a |
4 |
─=(subtract and assignment)
it subtracts the right operand from the left operand and assigns the result to the left operand. ex: c -= a is equivalent to c = c – a |
5 |
*=(multiply and assignment)
it multiplies the right operand with the left operand and assigns the result to the left operand. ex: c *= a is equivalent to c = c * a |
6 |
/=(divide and assignment)
it divides the left operand with the right operand and assigns the result to the left operand. |
note − same logic applies to bitwise operators, so they will become <=, >=, >=, >=, |= and ^=.
logical operators
logical operators are used to combine two or more conditions. logical operators return a boolean value. assume the value of variable a is 10 and b is 20.
operator | description | example |
---|---|---|
&& | and − the operator returns true only if all the expressions specified return true |
(a > 10 && b > 10) is false. |
|| | or − the operator returns true if at least one of the expressions specified return true |
(a > 10 || b > 10) is true. |
! | not − the operator returns the inverse of the expression’s result. for e.g.: !(7>5) returns false |
!(a > 10) is true. |
conditional expressions
dart has two operators that let you evaluate expressions that might otherwise require ifelse statements −
condition ? expr1 : expr2
if condition is true, then the expression evaluates expr1 (and returns its value); otherwise, it evaluates and returns the value of expr2.
expr1 ?? expr2
if expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2
example
the following example shows how you can use conditional expression in dart −
void main() { var a = 10; var res = a > 12 ? "value greater than 10":"value lesser than or equal to 10"; print(res); }
it will produce the following output −
value lesser than or equal to 10
example
let’s take another example −
void main() { var a = null; var b = 12; var res = a ?? b; print(res); }
it will produce the following output −
12