Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/IF: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11[[Basic Pascal Tutorial/Chapter 3/Boolean Expressions|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 3/CASE|►]]
22---|---|---
33
44
55Back to [[Reserved words]].
66
77
883Ca - IF (author: Tao Yue, state: changed)
99
1010The `IF` statement allows you to branch based on the result of a Boolean operation.
1111
1212## Format
1313
14- **IF** _expression_ THEN
15- _statement1_
16- [ ELSE
17- _statement2_]
14+**IF** _expression_ THEN
1815
16+_statement1_
17+
18+[ ELSE
19+
20+_statement2_]
21+
1922Where
2023
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+_expression_ is any comparison, constant or function which returns a [[boolean]] value, and
2325
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+
2428The **ELSE** clause is optional.
2529
2630There are two ways to use an `IF` statement, a one-way branch or a two-way branch/
2731
2832## One-way branch
2933
3034The one-way branch format is:
3135```
3236if BooleanExpression then
3337 StatementIfTrue;
3438```
3539
3640If the Boolean expression evaluates to `true`, the statement executes. Otherwise, it is skipped.
3741
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:
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:
3943```
4044if BooleanExpression then
4145 begin
4246 Statement1;
4347 Statement2;
4448 end;
4549```
4650
4751## Two-way branch
4852
4953There is also a two-way selection:
5054```
5155if BooleanExpression then
5256 StatementIfTrue
5357 else
5458 StatementIfFalse;
5559```
5660
5761Note there is never a semicolon `;` immediately before the `else`.
5862```
5963if BooleanExpression then
6064 begin
6165 Statement1; // semicolon here is mandatory
6266 Statement2; // semicolon here is optional
6367 end // semicolon here is forbidden
6468 else
6569 begin
6670 Statement3;
6771 Statement4;
6872 end;
6973```
7074
7175If 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
7276
7377If you need multi-way selection, simply nest `if` statements:
7478```
7579if Condition1 then
7680 Statement1
7781 else
7882 if Condition2 then
7983 Statement2
8084 else
8185 Statement3;
8286```
8387
8488Be careful with nesting. Sometimes the computer won't do what you want it to do:
8589```
8690if Condition1 then
8791 if Condition2 then
8892 Statement2
8993 else
9094 Statement1;
9195```
9296
9397The `else` is always matched with the most recent `if`, so the computer interprets the preceding block of code as:
9498```
9599if Condition1 then
96100 if Condition2 then
97101 Statement2
98102 else
99103 Statement1;
100104```
101105
102106You can get by with a null statement:
103107```
104108if Condition1 then
105109 if Condition2 then
106110 Statement2
107111 else
108112 else
109113 Statement1;
110114```
111115
112116Or you could use a `begin-end` block.
113117
114118The following proves a semicolon is _absolutely forbidden_ before an else:
115119```
116120// Paul Robinson 2020-12-16
117121
118122 // Compiler test program Err03.pas
119123 // tests the proposition that ; is
120124 // never legal before ELSE
121125
122126
123127 program Err03;
124128 Var
125129 Test,Test2: Boolean;
126130
127131
128132 Begin
129133
130134 Test := True;
131135 Test2 := True;
132136
133137 if Test then
134138 if Test2 then
135139 Writeln('Reached Part 1'); // semi-colon here should be illegal
136140 else
137141 Writeln('Reached Part 2');
138142
139143 end.
140144```
141145
142146But the best way to clean up the code would be to rewrite the condition.
143147```
144148if not Condition1 then
145149 Statement1
146150 else
147151 if Condition2 then
148152 Statement2;
149153```
150154
151155This 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.
152156
153157Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.
154158
155159[[Basic Pascal Tutorial/Chapter 3/Boolean Expressions|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 3/CASE|►]]
156160---|---|---
157161
158162---
159163*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).*

Back to history