Basic Pascal Tutorial/Chapter 3/IF: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | Back to [[Reserved words]]. | |
| 2 | 2 | ||
| 3 | 3 | 3Ca - IF (author: Tao Yue, state: changed) | |
| 4 | 4 | ||
| 5 | 5 | The `IF` statement allows you to branch based on the result of a Boolean operation. | |
| 6 | 6 | ||
| 7 | 7 | ## Format | |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | 10 | **IF** *expression* THEN | |
| 11 | 11 | ||
| 12 | 12 | *statemrnt1* | |
| 13 | 13 | ||
| 14 | 14 | \[ ELSE | |
| 15 | 15 | ||
| 16 | 16 | *statement2*\] | |
| 17 | 17 | ||
| 18 | 18 | Where | |
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | 21 | *expression* is any comparison, constant or function which returns a [[boolen]] value, and | |
| 22 | 22 | ||
| 23 | 23 | *statement1* and *statement2* are either a single [[statement]], a [[begin|Begin]]-[[End|end]] [[Block|block]], a [[Repeat|repeat]]-until block, or the [[;|null statement]]. | |
| 24 | 24 | ||
| 25 | 25 | The **ELSE** clause is optional. | |
| 26 | 26 | ||
| 27 | 27 | There are two ways to use an `IF` statement, a one-way branch or a two-way branch/ | |
| 28 | 28 | ||
| 29 | 29 | ## One-way branch | |
| 30 | 30 | ||
| 31 | 31 | The one-way branch format is: | |
| 32 | 32 | ||
| 33 | 33 | ``` pascal | |
| 34 | 34 | if BooleanExpression then | |
| 35 | 35 | StatementIfTrue; | |
| 36 | 36 | ``` | |
| 37 | 37 | ||
| 38 | 38 | If the Boolean expression evaluates to `true`, the statement executes. Otherwise, it is skipped. | |
| 39 | 39 | ||
| 40 | 40 | The `IF` statement accepts only one statement. If you would like to use a [[compound statement]], you must use a `begin-end` [[Frame|frame]] to enclose the statements: | |
| 41 | 41 | ||
| 42 | 42 | ``` pascal | |
| 43 | 43 | if BooleanExpression then | |
| 44 | 44 | begin | |
| 45 | 45 | Statement1; | |
| 46 | 46 | Statement2; | |
| 47 | 47 | end; | |
| 48 | 48 | ``` | |
| 49 | 49 | ||
| 50 | 50 | ## Two-way branch | |
| 51 | 51 | ||
| 52 | 52 | There is also a two-way selection: | |
| 53 | 53 | ||
| 54 | 54 | ``` pascal | |
| 55 | 55 | if BooleanExpression then | |
| 56 | 56 | StatementIfTrue | |
| 57 | 57 | else | |
| 58 | 58 | StatementIfFalse; | |
| 59 | 59 | ``` | |
| 60 | 60 | ||
| 61 | 61 | Note there is never a semicolon `;` immediately before the `else`. | |
| 62 | 62 | ||
| 63 | 63 | ``` pascal | |
| 64 | 64 | if BooleanExpression then | |
| 65 | 65 | begin | |
| 66 | 66 | Statement1; // semicolon here is mandatory | |
| 67 | 67 | Statement2; // semicolon here is optional | |
| 68 | 68 | end // semicolon here is forbidden | |
| 69 | 69 | else | |
| 70 | 70 | begin | |
| 71 | 71 | Statement3; | |
| 72 | 72 | Statement4; | |
| 73 | 73 | end; | |
| 74 | 74 | ``` | |
| 75 | 75 | ||
| 76 | - | ||
| 76 | + | If the Boolean expression evaluates to `FALSE`, the statement following the `else` will be performed. Note that you may never use a semicolon after the statement preceding the `else`. That causes the computer to treat it as a one-way selection, leaving it to wonder where the else came from. And when a compiler wonders, it usually gets mad and throws a tantrum, or rather, it throws an error | |
| 77 | 77 | ||
| 78 | 78 | If you need multi-way selection, simply nest `if` statements: | |
| 79 | 79 | ||
| 80 | 80 | ``` pascal | |
| 81 | 81 | if Condition1 then | |
| 82 | 82 | Statement1 | |
| 83 | 83 | else | |
| 84 | 84 | if Condition2 then | |
| 85 | 85 | Statement2 | |
| 86 | 86 | else | |
| 87 | 87 | Statement3; | |
| 88 | 88 | ``` | |
| 89 | 89 | ||
| 90 | 90 | Be careful with nesting. Sometimes the computer won't do what you want it to do: | |
| 91 | 91 | ||
| 92 | 92 | ``` pascal | |
| 93 | 93 | if Condition1 then | |
| 94 | 94 | if Condition2 then | |
| 95 | 95 | Statement2 | |
| 96 | 96 | else | |
| 97 | 97 | Statement1; | |
| 98 | 98 | ``` | |
| 99 | 99 | ||
| 100 | 100 | The `else` is always matched with the most recent `if`, so the computer interprets the preceding block of code as: | |
| 101 | 101 | ||
| 102 | 102 | ``` pascal | |
| 103 | 103 | if Condition1 then | |
| 104 | 104 | if Condition2 then | |
| 105 | 105 | Statement2 | |
| 106 | 106 | else | |
| 107 | 107 | Statement1; | |
| 108 | 108 | ``` | |
| 109 | 109 | ||
| 110 | 110 | You can get by with a null statement: | |
| 111 | 111 | ||
| 112 | 112 | ``` pascal | |
| 113 | 113 | if Condition1 then | |
| 114 | 114 | if Condition2 then | |
| 115 | 115 | Statement2 | |
| 116 | 116 | else | |
| 117 | 117 | else | |
| 118 | 118 | Statement1; | |
| 119 | 119 | ``` | |
| 120 | 120 | ||
| 121 | 121 | Or you could use a `begin-end` block. | |
| 122 | 122 | ||
| 123 | 123 | The following proves a semicolon is *absolutely forbidden* before an else: | |
| 124 | 124 | ||
| 125 | 125 | ``` pascal | |
| 126 | 126 | // Paul Robinson 2020-12-16 | |
| 127 | 127 | ||
| 128 | 128 | // Compiler test program Err03.pas | |
| 129 | 129 | // tests the proposition that ; is | |
| 130 | 130 | // never legal before ELSE | |
| 131 | 131 | ||
| 132 | 132 | program err03; | |
| 133 | 133 | Var | |
| 134 | 134 | Test,test2: Boolean; | |
| 135 | 135 | ||
| 136 | 136 | Begin | |
| 137 | 137 | ||
| 138 | 138 | Test := True; | |
| 139 | 139 | Test2 := true; | |
| 140 | 140 | ||
| 141 | 141 | if test then | |
| 142 | 142 | if test2 then | |
| 143 | 143 | Writeln('Reached Part 1'); // semi-colon here should be illegal | |
| 144 | 144 | else | |
| 145 | 145 | Writeln('Reached Part 2'); | |
| 146 | 146 | ||
| 147 | 147 | end. | |
| 148 | 148 | ``` | |
| 149 | 149 | ||
| 150 | 150 | But the best way to clean up the code would be to rewrite the condition. | |
| 151 | 151 | ||
| 152 | 152 | ``` pascal | |
| 153 | 153 | if not Condition1 then | |
| 154 | 154 | Statement1 | |
| 155 | 155 | else | |
| 156 | 156 | if Condition2 then | |
| 157 | 157 | Statement2; | |
| 158 | 158 | ``` | |
| 159 | 159 | ||
| 160 | 160 | This example illustrates where the not operator comes in very handy. If Condition1 had been a Boolean like: `(not(a < b) or (c + 3 > 6)) and g`, reversing the expression would be more difficult than NOTting it. | |
| 161 | 161 | ||
| 162 | 162 | Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation. | |
| 163 | 163 | ||
| 164 | 164 | --- | |
| 165 | 165 | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 3/IF", 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).* |
