Dart Programming - Operators
Dart Programming - Operators - An expression is a special kind of statement that evaluates to a value
Dart provides some relatively simple operators to simplify the operation, most of them are the same as Java, the following introduces a few different operators
- Arithmetic operator
- Relational operator
- Logical Operators
- Assignment operator
- Conditional expression
Arithmetic operator
| 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 |
Arithmetic Operators Example
|
int a = 2;
print(a++);// 2 print(a--);//4 |
Relational operator
Relational Operators tests or defines the kind of relationship between two entities. Relational operators return a Boolean value i.e. true/ false
| 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 |
Relational operator Example
|
int a = 2; |
logic operation
Boolean operations
Logical operators are used to combine two or more conditions. Logical operators return a Boolean value
| 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. |
Example
|
bool isTrue = true; |
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. |
Example
|
int m = 10;
m -= 2; |
Conditional Expressions
1. Trinocular operator: condition? Expr1: expe2
If condition is true, the expression evaluates expr1 (and returns its value); otherwise, it evaluates and returns the value of expr2.
2. ?? Operator: expr1 ?? expr2
If expr1 is non-null, return its value; otherwise, calculate and return the value of expr2
Example
|
int age =10;
String z = x ?? y; |