Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/IF: what changed

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

OldNewLine
1-Back to [[Reserved words]].
2-
3-3Ca - IF (author: Tao Yue, state: changed)
4-
5-The `IF` statement allows you to branch based on the result of a Boolean operation.
6-
7-## Format
8-
1+[[Basic Pascal Tutorial/Chapter 3/Boolean Expressions|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 3/CASE|►]]
2+---|---|---
93
10-**IF** *expression* THEN
4+
5+Back to [[Reserved words]].
116
12-*statemrnt1*
7+
8+3Ca - IF (author: Tao Yue, state: changed)
139
14-\[ ELSE
10+The `IF` statement allows you to branch based on the result of a Boolean operation.
1511
16-*statement2*\]
12+## Format
1713
18-Where
14+ **IF** _expression_ THEN
15+ _statement1_
16+ [ ELSE
17+ _statement2_]
1918
20-
21-*expression* is any comparison, constant or function which returns a [[boolen]] value, and
19+Where
2220
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]].
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]].
2423
25-The **ELSE** clause is optional.
24+The **ELSE** clause is optional.
2625
27-There are two ways to use an `IF` statement, a one-way branch or a two-way branch/
26+There are two ways to use an `IF` statement, a one-way branch or a two-way branch/
2827
2928## One-way branch
3029
31-The one-way branch format is:
32-
33-``` pascal
30+The one-way branch format is:
31+```
3432if BooleanExpression then
35- StatementIfTrue;
33+ StatementIfTrue;
3634```
3735
38-If the Boolean expression evaluates to `true`, the statement executes. Otherwise, it is skipped.
39-
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:
36+If the Boolean expression evaluates to `true`, the statement executes. Otherwise, it is skipped.
4137
42-``` pascal
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+```
4340if BooleanExpression then
44-begin
45- Statement1;
46- Statement2;
47-end;
41+ begin
42+ Statement1;
43+ Statement2;
44+ end;
4845```
4946
5047## Two-way branch
5148
52-There is also a two-way selection:
53-
54-``` pascal
49+There is also a two-way selection:
50+```
5551if BooleanExpression then
56- StatementIfTrue
57-else
58- StatementIfFalse;
52+ StatementIfTrue
53+ else
54+ StatementIfFalse;
5955```
6056
61-Note there is never a semicolon `;` immediately before the `else`.
62-
63-``` pascal
57+Note there is never a semicolon `;` immediately before the `else`.
58+```
6459if BooleanExpression then
65-begin
66- Statement1; // semicolon here is mandatory
67- Statement2; // semicolon here is optional
68-end // semicolon here is forbidden
69-else
70-begin
71- Statement3;
72- Statement4;
73-end;
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;
7469```
7570
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-
78-If you need multi-way selection, simply nest `if` statements:
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
7972
80-``` pascal
73+If you need multi-way selection, simply nest `if` statements:
74+```
8175if Condition1 then
82- Statement1
83-else
84- if Condition2 then
85- Statement2
86- else
87- Statement3;
76+ Statement1
77+ else
78+ if Condition2 then
79+ Statement2
80+ else
81+ Statement3;
8882```
8983
90-Be careful with nesting. Sometimes the computer won't do what you want it to do:
91-
92-``` pascal
84+Be careful with nesting. Sometimes the computer won't do what you want it to do:
85+```
9386if Condition1 then
94- if Condition2 then
95- Statement2
96-else
97- Statement1;
87+ if Condition2 then
88+ Statement2
89+ else
90+ Statement1;
9891```
9992
100-The `else` is always matched with the most recent `if`, so the computer interprets the preceding block of code as:
101-
102-``` pascal
93+The `else` is always matched with the most recent `if`, so the computer interprets the preceding block of code as:
94+```
10395if Condition1 then
104- if Condition2 then
105- Statement2
106- else
107- Statement1;
96+ if Condition2 then
97+ Statement2
98+ else
99+ Statement1;
108100```
109101
110-You can get by with a null statement:
111-
112-``` pascal
102+You can get by with a null statement:
103+```
113104if Condition1 then
114- if Condition2 then
115- Statement2
116- else
117-else
118- Statement1;
105+ if Condition2 then
106+ Statement2
107+ else
108+ else
109+ Statement1;
119110```
120111
121-Or you could use a `begin-end` block.
122-
123-The following proves a semicolon is *absolutely forbidden* before an else:
112+Or you could use a `begin-end` block.
124113
125-``` pascal
114+The following proves a semicolon is _absolutely forbidden_ before an else:
115+```
126116// Paul Robinson 2020-12-16
127-
128-// Compiler test program Err03.pas
129-// tests the proposition that ; is
130-// never legal before ELSE
131-
132-program err03;
133-Var
134- Test,test2: Boolean;
135-
136-Begin
137-
138- Test := True;
139- Test2 := true;
140-
141- if test then
142- if test2 then
143- Writeln('Reached Part 1'); // semi-colon here should be illegal
144- else
145- Writeln('Reached Part 2');
146-
147-end.
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.
148140```
149141
150-But the best way to clean up the code would be to rewrite the condition.
151-
152-``` pascal
142+But the best way to clean up the code would be to rewrite the condition.
143+```
153144if not Condition1 then
154- Statement1
155-else
156- if Condition2 then
157- Statement2;
145+ Statement1
146+ else
147+ if Condition2 then
148+ Statement2;
158149```
159150
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.
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.
161152
162-Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.
153+Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.
163154
155+[[Basic Pascal Tutorial/Chapter 3/Boolean Expressions|◄]] | [[Basic Pascal Tutorial/Contents|▲]] | [[Basic Pascal Tutorial/Chapter 3/CASE|►]]
156+---|---|---
157+
164158---
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).*
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).*

Back to history