F# - 布尔运算符



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

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

示例

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.htm
广告

© . All rights reserved.