Sign in Join
Wiki copytemporary

Basic Pascal Tutorial/Chapter 3/IF: what changed

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

Title: Basic Pascal Tutorial/Chapter 3/IF

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+
9+
10+**IF** *expression* THEN
11+
12+*statemrnt1*
13+
14+\[ ELSE
15+
16+*statement2*\]
17+
18+Where
19+
20+
21+*expression* is any comparison, constant or function which returns a [[boolen]] value, and
22+
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]].
24+
25+The **ELSE** clause is optional.
26+
27+There are two ways to use an `IF` statement, a one-way branch or a two-way branch/
28+
29+## One-way branch
30+
31+The one-way branch format is:
32+
33+``` pascal
34+if BooleanExpression then
35+ StatementIfTrue;
36+```
37+
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:
41+
42+``` pascal
43+if BooleanExpression then
44+begin
45+ Statement1;
46+ Statement2;
47+end;
48+```
49+
50+## Two-way branch
51+
52+There is also a two-way selection:
53+
54+``` pascal
55+if BooleanExpression then
56+ StatementIfTrue
57+else
58+ StatementIfFalse;
59+```
60+
61+Note there is never a semicolon `;` immediately before the `else`.
62+
63+``` pascal
64+if 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;
74+```
75+
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
77+
78+If you need multi-way selection, simply nest `if` statements:
79+
80+``` pascal
81+if Condition1 then
82+ Statement1
83+else
84+ if Condition2 then
85+ Statement2
86+ else
87+ Statement3;
88+```
89+
90+Be careful with nesting. Sometimes the computer won't do what you want it to do:
91+
92+``` pascal
93+if Condition1 then
94+ if Condition2 then
95+ Statement2
96+else
97+ Statement1;
98+```
99+
100+The `else` is always matched with the most recent `if`, so the computer interprets the preceding block of code as:
101+
102+``` pascal
103+if Condition1 then
104+ if Condition2 then
105+ Statement2
106+ else
107+ Statement1;
108+```
109+
110+You can get by with a null statement:
111+
112+``` pascal
113+if Condition1 then
114+ if Condition2 then
115+ Statement2
116+ else
117+else
118+ Statement1;
119+```
120+
121+Or you could use a `begin-end` block.
122+
123+The following proves a semicolon is *absolutely forbidden* before an else:
124+
125+``` pascal
126+// 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.
148+```
149+
150+But the best way to clean up the code would be to rewrite the condition.
151+
152+``` pascal
153+if not Condition1 then
154+ Statement1
155+else
156+ if Condition2 then
157+ Statement2;
158+```
159+
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.
161+
162+Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.
163+
164+---
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).*

Back to history