Pascal - 布林運算子



下表顯示了 Pascal 語言支援的所有布林運算子。所有這些運算子都作用於布林運算元併產生布爾結果。假設變數A為真,變數B為假,則 -

運算子 描述 示例
and 稱為布林 AND 運算子。如果兩個運算元都為真,則條件變為真。 (A and B) 為假。
and then 它類似於 AND 運算子,但是它保證了編譯器評估邏輯表示式的順序。從左到右,只有在必要時才評估右側運算元。 (A and then B) 為假。
or 稱為布林 OR 運算子。如果兩個運算元中的任何一個為真,則條件變為真。 (A or B) 為真。
or else 它類似於布林 OR,但是它保證了編譯器評估邏輯表示式的順序。從左到右,只有在必要時才評估右側運算元。 (A or else B) 為真。
not 稱為布林 NOT 運算子。用於反轉其運算元的邏輯狀態。如果條件為真,則邏輯 NOT 運算子將使其變為假。 not (A and B) 為真。

以下示例說明了該概念 -

program beLogical;
var
a, b: boolean;

begin
   a := true;
   b := false;

   if (a and b) then
      writeln('Line 1 - Condition is true' )
   else
      writeln('Line 1 - Condition is not true'); 
   if  (a or b) then
      writeln('Line 2 - Condition is true' );
   
   (* lets change the value of  a and b *)
   a := false;
   b := true;
   
   if  (a and b) then
      writeln('Line 3 - Condition is true' )
   else
      writeln('Line 3 - Condition is not true' );
   
   if not (a and b) then
   writeln('Line 4 - Condition is true' );
end.

當以上程式碼編譯並執行時,它會產生以下結果 -

Line 1 - Condition is not true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
pascal_operators.htm
廣告

© . All rights reserved.