Basic Pascal Tutorial/Chapter 3/Boolean Expressions/zh CN
English · Français · Български · 中文(中国) · 日本語
3B - 布尔表达式 (原作者: Tao Yue, 状态: 未更改)
布尔表达式 用于比较两个值,得到 真 或者 假:
值1 关系运算符 值2使用以下关系运算符:
< 小于
> 大于
= 等于
<= 小于等于
>= 大于等于
<> 不等于你可以将运算结果赋值给变量,这里我们将一个 真 赋值给 some_bool。
some_bool := 3 < 5;逻辑运算符:
not 逻辑非 (~)
and 逻辑与 (^)
or 逻辑或 (v)
xor 逻辑异或NOT是一元运算符,用于取反。
- not 真 = 假
- not 假 = 真
AND,当两个值都为真是,结果为真,否则为假。
- 真 and 假 = 假
- 真 and 真 = 真
OR,当有一个值为真时,结果为真,否则为假。
- 真 or 真 = 真
- 真 or 假 = 真
- 假 or 真 = 真
- 假 or 假 = 假
XOR,任一值为真时,结果为真,但不能同时是,从而:
- 真 xor 真 = 假
- 真 xor 假 = 真
- 假 xor 真 = 真
- 假 xor 假 = 假
当关系运算符和逻辑运算符混合使用时,要小心使用括号。
(3>5) or (650<1)逻辑运算符 优先级高于 关系运算符:
- not
-
- / div mod and
- + - or
- < > <= >= = <>
所以,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.