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