If and Then: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: If and Then
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The `if` [[Keyword|keyword]] precedes a condition, must be followed by `then` and a [[statement]]. The statement may optionally be followed by [[Else|else]] and another statement. This creates a binary [[Branch|branch]]. | |
| 2 | + | ||
| 3 | + | ## `If then` | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | if condition | |
| 7 | + | then true_statement | |
| 8 | + | else false_statement; | |
| 9 | + | ``` | |
| 10 | + | ||
| 11 | + | `condition` is a [[Boolean]] expression that evaluates to [[True|true]] xor [[False|false]]. `true_statement` is executed if `condition` evaluates to `true`. `false_statement` is executed if `condition` evaluates to `false`. A [[compile-time error]] occurs if the type of `condition` does not evaluate to a `Boolean` value. | |
| 12 | + | ||
| 13 | + | ### Multiple statements in `if then` branch | |
| 14 | + | ||
| 15 | + | If you need two or more statements for `true_statement` or `false_statement`, enclose them within a [[Begin|begin]] … [[End|end]] frame (“compound statement”). | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | if boolean_condition then | |
| 19 | + | begin | |
| 20 | + | statement_zero; | |
| 21 | + | statement_one; | |
| 22 | + | statement_two; | |
| 23 | + | end; | |
| 24 | + | ``` | |
| 25 | + | ||
| 26 | + | ## Usage | |
| 27 | + | ||
| 28 | + | In order to optimize for speed in an `if … then … else` branch, try to write your expression so that the `then`-part gets executed most often. This improves the rate of successful `jump` predictions. | |
| 29 | + | ||
| 30 | + | ## See also | |
| 31 | + | ||
| 32 | + | - Official documentation: [Reference guide: § “The `If..then..else` statement”](https://www.freepascal.org/docs-html/ref/refsu57.html) | |
| 33 | + | - [[Basic Pascal Tutorial/Chapter 3/IF]], Tao Yue, Basic Pascal Introduction | |
| 34 | + | - [[;|If statement and semicolon]] | |
| 35 | + | - [[Case|case]] | |
| 36 | + | ||
| 37 | + | --- | |
| 38 | + | *From the Free Pascal wiki, page "If and Then", as it stood on 2023-01-25 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).* |
