Operators and Expressions
An expression is a special kind of statements that gives a value. Every expresion is composed of two things:-
Here is the simple example:- let a= 5 and b= 10;
void main(){
a=5, b=10;
print(a+b);
print(b-a);
}
It produces the following output:-
15
5
The value of the expression changed when there are two operations on the same operands.
Dart supports various operators used for different operations, they are:-
Dart supports the various arithmetic operators, they are shown below:-
Operator | Description |
---|---|
+ | Add( they add the operands which are also called sum) |
- | Subtract |
* | Multiply |
/ | Divide |
~/ | Divide, returning to interger result |
% | Modulo(Get the remainder of an integer division) |
-expr | Unary minus, also known as negation (reverse the sign of the expression |
– | decrement |
++ | increment |
Example:-
void main(){
a=5 b=10;
print( a + b );
print( a - b );
print( a * b );
print( a / b );
print( a ~/ b );
print( a % b );
}
Can You guess what the produced output will be and try yourself doing it.
Dart also have both prefix and post fix increment and decrement operatos. ++a, a++ and –a, a– For the above prefix and postfix try yourself and summarized what happened.Dart supports the various equality and relational operators. It defines what kind of relationship two entities have.They return the value of true or false, they are shown below:-
Operator | Description |
---|---|
== | Equal |
!= | Not equal |
> | Grater than |
< | less than |
>= | Greater than or equal to |
<= | Less than or equal to |
To test expression, to represent if the operands have the same thing , we use == operator.
Example:-
void main(){
a=5 b=5;
print( a=b );
print( a != b );
print( a > b );
print( a < b );
print( a >= b );
print( a <= b );
}
Can You guess what the produced output will be and try yourself.
The type test operators are handy for checking types at runtime, they are shown below:-
Operator | Description |
---|---|
as | Typecast |
is | True if the object has the soecified type |
is! | True if the object doesn’t hvae the specified type |
The result of obj is T is true if obj implements the interface specified by T. |
Use as if you are sure that the object is of that type and use is if you are not sure.
The assignment operators assigns the value only if the assigned-to variable is null, they are shown below:-
Operator | Description |
---|---|
= | Simple Assignment (Assigns value from right to left operand) |
??= | Assigns only if the variable is null |
+= | Add and Assignment ( Add the operand and assign the value to another operand) |
-= | Subtract and Assignment |
*= | Multiply and Assignment |
/= | Divide and Assignment |
%= | Modulo and Assignment |
~/= | Divide with returning interger value and Assignment |
Same logic for bitwise operators »>=, ^= , <<=, »=, &=, |=
The logical operators are used to combine two or more conditions. It returns a boolean value i.e. true or false, they are shown below:-
Operator | Description | ||
---|---|---|---|
!expr | NOT inverts the expression (changes false to true, and vice versa) | ||
logical OR ( returns true if at least one expression is specified return true ) | |||
&& | logical AND ( returns true if all expression is specified return true ) |
Example
if (!a && ( b==0 || b==6)){
//statements
}
You can manipulate the individual bits of numbers in Dart,these operatorsare used with intergers. They are shown below:-
Operator | Description | |
---|---|---|
& | AND ( returns a one in each bit position for which corresponding bits of both operands are ones ) | |
OR ( returns a one in each bit position for which corresponding bits of either orboth operands are ones ) | ||
^ | XOR ( returns a one in each bit position for which corresponding bits of either but not both operands are ones | |
~exp | Unary bitwise complement ( 0s become 1s; 1s become 0s ) | |
<< | Shift left (shifts a binary represntation bits to the left, shifting in zeroes from the righ | |
» | Shift right (shifts a binary represntation bits to the right, discarding bits shifted off ) |
As discussed above dart has two operators that let you evaluate between expressions that might otherwise require if-else statements:
Syntax
condition? expr1 :exp2
If the condition is true that expresion 1 is executed and value is returned otherwise it returns the value of expresion 2.
exp 1 ?? exp2
If expresion 1 is non-null, return its value; otherwise, evaluates and returns the value of expresion2. Example
void main(){
var a= 5;
var result = a >4 ? " a is greater than 4": "a is less than 4";
print(result);
}
The following output is produced:-
a is greater than 4