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