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.