Basic Pascal Tutorial/Chapter 3/IF/zh CN: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Basic Pascal Tutorial/Chapter 3/IF/zh CN
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 3Ca - IF语句 (原作者: Tao Yue, 状态: 未更改) | |
| 2 | + | ||
| 3 | + | `IF` 语句,计算表达式结果进行分支。单行格式为: | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | if 表达式 then | |
| 7 | + | 语句; | |
| 8 | + | ``` | |
| 9 | + | ||
| 10 | + | 如果表达式结果为真,则执行该语句,否则跳过。 | |
| 11 | + | ||
| 12 | + | `IF` 只接受一条语句;如果需要执行多条语句,则必须使用 `begin-end`: | |
| 13 | + | ||
| 14 | + | ``` pascal | |
| 15 | + | if 表达式 then | |
| 16 | + | begin | |
| 17 | + | 语句1; | |
| 18 | + | 语句2 | |
| 19 | + | end; | |
| 20 | + | ``` | |
| 21 | + | ||
| 22 | + | 当不满足条件时,还有一个选择: | |
| 23 | + | ||
| 24 | + | ``` pascal | |
| 25 | + | if 表达式 then | |
| 26 | + | 为真 | |
| 27 | + | else | |
| 28 | + | 为假; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | 表达式运算结果为`假`,则执行 `else` 后面的语句。 注意 `else` 前面没有分号,因为分号是两个语句之间的分隔符,而 `else` 并非语句。 如果在该处添了分号,则在编译的时候就会认为 `if` 语句到此结束,而把 `else` 当作另一句的开头。 | |
| 32 | + | ||
| 33 | + | 如果有多个条件,只需要嵌套 `if` 语句: | |
| 34 | + | ||
| 35 | + | ``` pascal | |
| 36 | + | if 条件1 then | |
| 37 | + | 语句1 | |
| 38 | + | else | |
| 39 | + | if 条件1 then | |
| 40 | + | 语句2 | |
| 41 | + | else | |
| 42 | + | 语句3; | |
| 43 | + | ``` | |
| 44 | + | ||
| 45 | + | 小心使用嵌套,程序运行结果可能不是你想要的。 | |
| 46 | + | ||
| 47 | + | ``` pascal | |
| 48 | + | if 条件1 then | |
| 49 | + | if 条件2 then | |
| 50 | + | 语句2 | |
| 51 | + | else | |
| 52 | + | 语句1; | |
| 53 | + | ``` | |
| 54 | + | ||
| 55 | + | `else` 总是匹配与它最近的 `if`: | |
| 56 | + | ||
| 57 | + | ``` pascal | |
| 58 | + | if 条件1 then | |
| 59 | + | if 条件2 then | |
| 60 | + | 语句2 | |
| 61 | + | else | |
| 62 | + | 语句1; | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | 你可以使用空语句: | |
| 66 | + | ||
| 67 | + | ``` pascal | |
| 68 | + | if 条件1 then | |
| 69 | + | if 条件2 then | |
| 70 | + | 语句2 | |
| 71 | + | else | |
| 72 | + | else | |
| 73 | + | 语句1; | |
| 74 | + | ``` | |
| 75 | + | ||
| 76 | + | 或者你可以使用 `begin-end`,但代码整洁的最佳方法是将条件重写。 | |
| 77 | + | ||
| 78 | + | ``` pascal | |
| 79 | + | if not 条件1 then | |
| 80 | + | 语句1 | |
| 81 | + | else | |
| 82 | + | if 条件2 then | |
| 83 | + | 语句2; | |
| 84 | + | ``` | |
| 85 | + | ||
| 86 | + | 如果表达式像这样:`(not(a < b) or (c + 3 > 6)) and g`,理解起来会很费劲。 | |
| 87 | + | ||
| 88 | + | 仍需要注意,代码缩进便于阅读代码(编译器忽略缩进)。 | |
| 89 | + | ||
| 90 | + | | | | | | |
| 91 | + | |----|----|----| | |
| 92 | + | | [[Basic Pascal Tutorial/Chapter 3/Boolean Expressions/zh CN|上一页]] | [[Basic Pascal Tutorial/Contents/zh CN|目录]] | [[Basic Pascal Tutorial/Chapter 3/CASE/zh CN|下一页]] | | |
| 93 | + | ||
| 94 | + | --- | |
| 95 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/IF/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).* |
