Pascal - 布尔运算符
下表显示了 Pascal 语言支持的所有布尔运算符。 所有这些运算符都处理布尔操作数并产生布尔结果。 假设变量 A 为 true,变量 B 为 false,则 −
| 运算符 | 描述 | 示例 |
|---|---|---|
| and | 称为布尔 AND 运算符。 如果两个操作数都为 true,则条件为 true。 | (A and B)为 false。 |
| and then | 它类似于 AND 运算符,但是,它保证编译器计算逻辑表达式的顺序。 从左到右,仅在必要时才计算右操作数。 | (A and then B)为 false。 |
| or | 称为布尔 OR 运算符。 如果两个操作数中的任何一个为 true,则条件为 true。 | (A or B)为 true。 |
| or else | 它类似于布尔 OR,但它保证编译器计算逻辑表达式的顺序。 从左到右,仅在必要时才计算右操作数。 | (A or else B) 为 true。 |
| not | 称为布尔 NOT 运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则逻辑 NOT 运算符会将其设为假。 | not (A and B) 为 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

