Else: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Else
| Old | New | Line | |
|---|---|---|---|
| 1 | + | `else` is a [[Reserved word|reserved word]] which starts a fallback-branch if all other *named* cases do not apply. It can occur in | |
| 2 | + | ||
| 3 | + | - [[If and Then|if … then … else-branches]], and | |
| 4 | + | - [[Case|case-statements]]. | |
| 5 | + | ||
| 6 | + | ## semantics | |
| 7 | + | ||
| 8 | + | An `else`-branch obtains program flow, if no other condition has been met. It cannot be paired with an explicit [[expression]], but depends on expressions stated at others place, so an `else` per se does not have a condition. In `if … then … else`-statements, instructions are executed to the following scheme: | |
| 9 | + | ||
| 10 | + | ``` pascal | |
| 11 | + | if expression | |
| 12 | + | then trueStatement | |
| 13 | + | else falseStatement; | |
| 14 | + | ``` | |
| 15 | + | ||
| 16 | + | Where `falseStatement` is executed if `expression` evaluates to [[false and true|false]]. | |
| 17 | + | ||
| 18 | + | In `case`-statements an `else`-branch assumes program flow, if no `case`-labels matched `expression`. | |
| 19 | + | ||
| 20 | + | ``` pascal | |
| 21 | + | case expression of | |
| 22 | + | value0: action0; | |
| 23 | + | value1: action1; | |
| 24 | + | else action2; | |
| 25 | + | end; | |
| 26 | + | ``` | |
| 27 | + | ||
| 28 | + | Only if `expression` neither evaluates to `value0` nor `value1`, `action2` is executed. | |
| 29 | + | ||
| 30 | + | ### comparative remarks | |
| 31 | + | ||
| 32 | + | In [[Pascal]] there is no `elsif` or `elif`. However writing `else` and `if` back to back does not pose a problem. Note, that the second `if … then` constitutes on its own a single [[statement]]. The requirement that `else` is followed by a statement is therefore fulfilled. | |
| 33 | + | ||
| 34 | + | ``` pascal | |
| 35 | + | if expression0 then | |
| 36 | + | begin | |
| 37 | + | action0; | |
| 38 | + | end | |
| 39 | + | else if expression1 then | |
| 40 | + | begin | |
| 41 | + | action1; | |
| 42 | + | end; | |
| 43 | + | ``` | |
| 44 | + | ||
| 45 | + | ### nested `if … then … else` | |
| 46 | + | ||
| 47 | + | `if … then … else` are prone to semantic errors if no compound statements by enclosing a block with [[Begin|begin]] and [[End|end]] are used. | |
| 48 | + | ||
| 49 | + | ``` pascal | |
| 50 | + | if itIsMorning() then | |
| 51 | + | if itIsAHoliday() then | |
| 52 | + | begin | |
| 53 | + | sleep; | |
| 54 | + | end | |
| 55 | + | else | |
| 56 | + | begin | |
| 57 | + | wakeUp; | |
| 58 | + | dress; | |
| 59 | + | brushTeeth; | |
| 60 | + | …; | |
| 61 | + | end | |
| 62 | + | else | |
| 63 | + | begin | |
| 64 | + | …; | |
| 65 | + | end; | |
| 66 | + | ``` | |
| 67 | + | ||
| 68 | + | > **Note:** In front of an `else` as part of an `if … then … else`-statement no [[;|semicolon]] is permitted. | |
| 69 | + | ||
| 70 | + | The reference guide explains, quote: | |
| 71 | + | ||
| 72 | + | > In nested `If.. then .. else` constructs, some ambiguity may \[arise\] as to which `else` statement pairs with which `if` statement. The rule is that the `else` [[Keyword|keyword]] matches the first `if` keyword (searching backwards) not already matched by an `else` keyword. | |
| 73 | + | ||
| 74 | + | ## see also | |
| 75 | + | ||
| 76 | + | - [“The `If..then..else` statement in “Free Pascal reference guide”](https://www.freepascal.org/docs-html/ref/refsu56.html) | |
| 77 | + | ||
| 78 | + | --- | |
| 79 | + | *From the Free Pascal wiki, page "Else", as it stood on 2020-12-10 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).* |
