Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/IF: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Back to [[Reserved words]].
22
333Ca - IF (author: Tao Yue, state: changed)
44
55The `IF` statement allows you to branch based on the result of a Boolean operation.
66
77## Format
88
99
1010**IF** *expression* THEN
1111
1212*statemrnt1*
1313
1414\[ ELSE
1515
1616*statement2*\]
1717
1818Where
1919
2020
2121*expression* is any comparison, constant or function which returns a [[boolen]] value, and
2222
2323*statement1* and *statement2* are either a single [[statement]], a [[begin|Begin]]-[[End|end]] [[Block|block]], a [[Repeat|repeat]]-until block, or the [[;|null statement]].
2424
2525The **ELSE** clause is optional.
2626
2727There are two ways to use an `IF` statement, a one-way branch or a two-way branch/
2828
2929## One-way branch
3030
3131The one-way branch format is:
3232
3333``` pascal
3434if BooleanExpression then
3535 StatementIfTrue;
3636```
3737
3838If the Boolean expression evaluates to `true`, the statement executes. Otherwise, it is skipped.
3939
4040The `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:
4141
4242``` pascal
4343if BooleanExpression then
4444begin
4545 Statement1;
4646 Statement2;
4747end;
4848```
4949
5050## Two-way branch
5151
5252There is also a two-way selection:
5353
5454``` pascal
5555if BooleanExpression then
5656 StatementIfTrue
5757else
5858 StatementIfFalse;
5959```
6060
6161Note there is never a semicolon `;` immediately before the `else`.
6262
6363``` pascal
6464if BooleanExpression then
6565begin
6666 Statement1; // semicolon here is mandatory
6767 Statement2; // semicolon here is optional
6868end // semicolon here is forbidden
6969else
7070begin
7171 Statement3;
7272 Statement4;
7373end;
7474```
7575
76-If the Boolean expression evaluates to `FALSE`, the statement following the `else` will be performed. Note that you may <u>never</u> 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
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
7777
7878If you need multi-way selection, simply nest `if` statements:
7979
8080``` pascal
8181if Condition1 then
8282 Statement1
8383else
8484 if Condition2 then
8585 Statement2
8686 else
8787 Statement3;
8888```
8989
9090Be careful with nesting. Sometimes the computer won't do what you want it to do:
9191
9292``` pascal
9393if Condition1 then
9494 if Condition2 then
9595 Statement2
9696else
9797 Statement1;
9898```
9999
100100The `else` is always matched with the most recent `if`, so the computer interprets the preceding block of code as:
101101
102102``` pascal
103103if Condition1 then
104104 if Condition2 then
105105 Statement2
106106 else
107107 Statement1;
108108```
109109
110110You can get by with a null statement:
111111
112112``` pascal
113113if Condition1 then
114114 if Condition2 then
115115 Statement2
116116 else
117117else
118118 Statement1;
119119```
120120
121121Or you could use a `begin-end` block.
122122
123123The following proves a semicolon is *absolutely forbidden* before an else:
124124
125125``` pascal
126126// Paul Robinson 2020-12-16
127127
128128// Compiler test program Err03.pas
129129// tests the proposition that ; is
130130// never legal before ELSE
131131
132132program err03;
133133Var
134134 Test,test2: Boolean;
135135
136136Begin
137137
138138 Test := True;
139139 Test2 := true;
140140
141141 if test then
142142 if test2 then
143143 Writeln('Reached Part 1'); // semi-colon here should be illegal
144144 else
145145 Writeln('Reached Part 2');
146146
147147end.
148148```
149149
150150But the best way to clean up the code would be to rewrite the condition.
151151
152152``` pascal
153153if not Condition1 then
154154 Statement1
155155else
156156 if Condition2 then
157157 Statement2;
158158```
159159
160160This 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.
161161
162162Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.
163163
164164---
165165*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).*

Back to history