F# - 布尔运算符

下表显示了 F# 语言支持的所有布尔运算符。 假设变量 A 为 true,变量 B 为 false,则 −

运算符 描述 示例
&& 称为布尔 AND 运算符。 如果两个操作数均非零,则条件为 true。 (A && B) 为 false。
|| 称为布尔 OR 运算符。 如果两个操作数中有任何一个非零,则条件为 true。 (A || B) 为 true。
not 称为布尔 NOT 运算符。 用于反转其操作数的逻辑状态。 如果条件为 true,则逻辑 NOT 运算符将返回 false。 not (A && B) 为 true。

示例

let mutable a : bool = true;
let mutable b : bool = true;

if ( a && b ) then
   printfn "Line 1 - Condition is true"
else
   printfn "Line 1 - Condition is not true"

if ( a || b ) then
   printfn "Line 2 - Condition is true"
else
   printfn "Line 2 - Condition is not true"

(* lets change the value of a *)

a <- false
if ( a && b ) then
   printfn "Line 3 - Condition is true"
else
   printfn "Line 3 - Condition is not true"

if ( a || b ) then
   printfn "Line 4 - Condition is true"
else
   printfn "Line 4 - Condition is not true"

当您编译并执行该程序时,它会产生以下输出 −

Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

❮ fsharp_operators.html