Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/Boolean Expressions/zh CN: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
113B - 布尔表达式 (原作者: Tao Yue, 状态: 未更改)
22
33[[Boolean/zh CN|布尔表达式]] 用于比较两个值,得到 `真` 或者 `假`:
44
5-`值1 `**`关系运算符`**` 值2`
5+```
6+值1 关系运算符 值2
7+```
68
79使用以下关系运算符:
810
9-`<  小于`\
10-`>  大于`\
11-`=  等于`\
12-`<= 小于等于`\
13-`>= 大于等于`\
14-`<> 不等于`
11+```
12+< 小于
13+> 大于
14+= 等于
15+<= 小于等于
16+>= 大于等于
17+<> 不等于
18+```
1519
1620你可以将运算结果赋值给变量,这里我们将一个 `真` 赋值给 some_bool。
1721
18-`some_bool := 3 < 5;`
22+``` pascal
23+some_bool := 3 < 5;
24+```
1925
2026逻辑运算符:
2127
22-`not    逻辑非 (~)`\
23-`and    逻辑与 (^)`\
24-`or    逻辑或 (v)`\
25-`xor    逻辑异或`
28+```
29+not 逻辑非 (~)
30+and 逻辑与 (^)
31+or 逻辑或 (v)
32+xor 逻辑异或
33+```
2634
2735`NOT`是一元运算符,用于取反。
2836
2937- not 真 = 假
3038- not 假 = 真
3139
3240`AND`,当两个值都为真是,结果为真,否则为假。
3341
3442- 真 and 假 = 假
3543- 真 and 真 = 真
3644
3745`OR`,当有一个值为真时,结果为真,否则为假。
3846
3947- 真 or 真 = 真
4048- 真 or 假 = 真
4149- 假 or 真 = 真
4250- 假 or 假 = 假
4351
4452`XOR`,任一值为真时,结果为真,但不能同时是,从而:
4553
4654- 真 xor 真 = 假
4755- 真 xor 假 = 真
4856- 假 xor 真 = 真
4957- 假 xor 假 = 假
5058
5159当关系运算符和逻辑运算符混合使用时,要小心使用括号。
5260
5361``` pascal
5462(3>5) or (650<1)
5563```
5664
5765`逻辑运算符` 优先级高于 `关系运算符`:
5866
59671. not
60682. * / div mod and
61693. \+ - or
62704. \< \> \<= \>= = \<\>
6371
6472所以,`3 > 5 or 650`,运算时,是这样:`3 > (5 or 650)`,但这样没有意义,因为整数不适用逻辑运算。
6573
6674逻辑运算符(`AND、OR、NOT、XOR`),可以很容易在逻辑表达式上使用。
6775
6876如有可能,不要使用等号比较2个实数,因为,小数位会导致它们不相等。
6977
7078| | | |
7179|----|----|----|
7280| [[Basic Pascal Tutorial/Chapter 3/Sequential control/zh CN|上一页]] | [[Basic Pascal Tutorial/Contents/zh CN|目录]] | [[Basic Pascal Tutorial/Chapter 3/IF/zh CN|下一页]] |
7381
7482---
7583*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](/wiki-contact).*

Back to history