当我们想通过在表达式之间放置条件语句来评估表达式时,会使用 dart 中的逻辑运算符,这最终会产生一个布尔值。
逻辑运算符仅适用于布尔操作数。
Dart 中存在三种类型的逻辑运算符。在下表中,所有这些都连同它们的名称以及它们在两个布尔操作数上使用时产生的结果一起被提及。
让我们考虑两个名为 x 和 y 的布尔变量,其值分别为 true 和 false。
考虑下表 -
运算符 | 名称 | 描述 | 结果 |
---|---|---|---|
&& | 逻辑与 | Returns true if all expressions are true | x && y = 假 |
|| | 逻辑或 | Returns true is any expression is true | ×|| y = 真 |
! | 逻辑非 | Returns compliment of the expression | !x = 假 |
让我们在 Dart 程序中使用上面提到的所有逻辑运算符。
考虑下面显示的例子 -
void main(){ var x = true, y = false; print("x && y is: ${x && y}"); print("x || y is: ${x || y}"); print("!x is: ${!x}"); }输出结果
x && y is: false x || y is: true !x is: false