📅  最后修改于: 2020-11-05 04:14:56             🧑  作者: Mango
表达式是一种特殊的声明,可以求值。每个表达式都由-
操作数-表示数据
运算符-定义如何处理操作数以产生值。
考虑以下表达式–“ 2 + 3”。在此表达式中,2和3是操作数,符号“ +”(加号)是运算符。
在本章中,我们将讨论Dart中可用的运算符。
下表显示了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 |
关系运算符测试或定义两个实体之间的关系类型。关系运算符返回布尔值,即true / false。
假设A的值为10,B的值为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 |
这些运算符非常方便在运行时检查类型。
Operator | Meaning |
---|---|
is | True if the object has the specified type |
is! | False if the object has the specified type |
下表列出了Dart中可用的按位运算运算符及其作用-
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. |
下表列出了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. |
注–相同的逻辑适用于按位运算符,因此它们将变为&Lt; =,&Gt; =,&Gt; =,&Gt; =,| =和^ =。
逻辑运算符用于组合两个或多个条件。逻辑运算符返回布尔值。假设变量A的值为10,而B为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. |
Dart有两个运算符,可让您评估可能需要ifelse语句的表达式-
如果condition为true,则表达式对expr1求值(并返回其值);否则,表达式为0。否则,它求值并返回expr2的值。
如果expr1不为null,则返回其值;否则,返回0。否则,求值并返回expr2的值
以下示例显示了如何在Dart中使用条件表达式-
void main() {
var a = 10;
var res = a > 12 ? "value greater than 10":"value lesser than or equal to 10";
print(res);
}
它将产生以下输出-
value lesser than or equal to 10
让我们再举一个例子-
void main() {
var a = null;
var b = 12;
var res = a ?? b;
print(res);
}
它将产生以下输出-
12