当前位置: 首页 > 文档资料 > Pascal 中文教程 >

Show 例子 3

优质
小牛编辑
123浏览
2023-12-01

下表显示了Pascal语言支持的所有布尔运算符。 所有这些运算符都处理布尔操作数并生成布尔结果。 假设变量A保持为真,变量B保持为假,则 -

操作者描述
and称为布尔AND运算符。 如果两个操作数都为真,则条件成立。(A和B)是假的。
然后它类似于AND运算符,但它保证了编译器评估逻辑表达式的顺序。 从左到右,仅在必要时评估右操作数。(A和B)是假的。
or称为布尔OR运算符。 如果两个操作数中的任何一个为真,则条件成立。(A或B)是真的。
要不然它类似于布尔OR,但它保证了编译器评估逻辑表达式的顺序。 从左到右,仅在必要时评估右操作数。(A或B)是真的。
not称为布尔NOT运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则Logical NOT运算符将使其为false。不(A和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