Pascal-boolean-operators

提供:Dev Guides
移動先:案内検索

パスカル-ブール演算子

次の表は、Pascal言語でサポートされているすべてのブール演算子を示しています。 これらの演算子はすべてブール値オペランドで機能し、ブール値の結果を生成します。 変数 A が真を保持し、変数 B が偽を保持すると仮定します-

Operator Description Example
and Called Boolean AND operator. If both the operands are true, then condition becomes true. (A and B) is false.
and then It is similar to the AND operator, however, it guarantees the order in which the compiler evaluates the logical expression. Left to right and the right operands are evaluated only when necessary. (A and then B) is false.
or Called Boolean OR Operator. If any of the two operands is true, then condition becomes true. (A or B) is true.
or else It is similar to Boolean OR, however, it guarantees the order in which the compiler evaluates the logical expression. Left to right and the right operands are evaluated only when necessary. (A or else B) is true.
not Called Boolean NOT Operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. not (A and B) is true.

次の例は、概念を示しています-

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