维基副本镜像

这是 pascal.chat 维基的镜像。编辑、历史记录和登录都在那里。

Basic Pascal Tutorial/Chapter 3/Boolean Expressions/zh CN

English · Français · Български · 中文(中国) · 日本語

3B - 布尔表达式 (原作者: Tao Yue, 状态: 未更改)

布尔表达式 用于比较两个值,得到 真 或者 假:

text
值1 关系运算符 值2

使用以下关系运算符:

text
<  小于
>  大于
=  等于
<= 小于等于
>= 大于等于
<> 不等于

你可以将运算结果赋值给变量,这里我们将一个 真 赋值给 some_bool。

pascal
some_bool := 3 < 5;

逻辑运算符:

text
not    逻辑非 (~)
and    逻辑与 (^)
or    逻辑或 (v)
xor    逻辑异或

NOT是一元运算符,用于取反。

AND,当两个值都为真是,结果为真,否则为假。

OR,当有一个值为真时,结果为真,否则为假。

XOR,任一值为真时,结果为真,但不能同时是,从而:

当关系运算符和逻辑运算符混合使用时,要小心使用括号。

pascal
(3>5) or (650<1)

逻辑运算符 优先级高于 关系运算符:

  1. not
    • / div mod and
  2. + - or
  3. < > <= >= = <>

所以,3 > 5 or 650,运算时,是这样:3 > (5 or 650),但这样没有意义,因为整数不适用逻辑运算。

逻辑运算符(AND、OR、NOT、XOR),可以很容易在逻辑表达式上使用。

如有可能,不要使用等号比较2个实数,因为,小数位会导致它们不相等。

上一页 目录 下一页

From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/Boolean Expressions/zh CN", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

版本4,最后由WikiArchive于修改。