Difference between revisions of "Basic Pascal Tutorial/Chapter 3/IF"
(Added ; where missing and addition of compound statement example.) |
m (bypass language bar/categorization template redirect [cf. discussion]) |
||
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {{IF}} | + | {{Basic Pascal Tutorial/Chapter 3/IF}} |
− | {{TYNavigator| | + | {{TYNavigator|Chapter 3/Boolean Expressions|Chapter 3/CASE}} |
− | |||
− | The <tt>IF</tt> statement allows you to branch based on the result of a Boolean operation. The one-way branch format is: | + | Back to [[Reserved words]]. |
− | <syntaxhighlight> | + | |
+ | |||
+ | 3Ca - IF (author: Tao Yue, state: changed) | ||
+ | |||
+ | The <tt>IF</tt> statement allows you to branch based on the result of a Boolean operation. | ||
+ | == Format == | ||
+ | : '''IF''' ''expression'' THEN | ||
+ | : ''statemrnt1'' | ||
+ | : [ ELSE | ||
+ | : ''statement2''] | ||
+ | |||
+ | Where | ||
+ | : ''expression'' is any comparison, constant or function which returns a [[boolen]] value, and | ||
+ | : ''statement1'' and ''statement2'' are either a single [[statement]], a [[begin|Begin]]-[[End|end]] [[Block|block]], a [[Repeat|repeat]]-until block, or the [[;#empty statement|null statement]]. | ||
+ | The '''ELSE''' clause is optional. | ||
+ | |||
+ | There are two ways to use an <tt>IF</tt> statement, a one-way branch or a two-way branch/ | ||
+ | |||
+ | ==One-way branch== | ||
+ | |||
+ | The one-way branch format is: | ||
+ | |||
+ | <syntaxhighlight lang=pascal> | ||
if BooleanExpression then | if BooleanExpression then | ||
StatementIfTrue; | StatementIfTrue; | ||
Line 12: | Line 33: | ||
If the Boolean expression evaluates to <tt>true</tt>, the statement executes. Otherwise, it is skipped. | If the Boolean expression evaluates to <tt>true</tt>, the statement executes. Otherwise, it is skipped. | ||
− | The <tt>IF</tt> statement accepts only one statement. If you would like to | + | The <tt>IF</tt> statement accepts only one statement. If you would like to use a [[compound statement]], you must use a <tt>begin-end</tt> [[Frame|frame]] to enclose the statements: |
− | <syntaxhighlight> | + | |
+ | <syntaxhighlight lang=pascal> | ||
if BooleanExpression then | if BooleanExpression then | ||
begin | begin | ||
Line 20: | Line 42: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | ==Two-way branch== | ||
+ | There is also a two-way selection: | ||
− | + | <syntaxhighlight lang=pascal> | |
− | <syntaxhighlight> | ||
if BooleanExpression then | if BooleanExpression then | ||
StatementIfTrue | StatementIfTrue | ||
Line 29: | Line 52: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Note there is | + | Note there is never a semicolon <tt>;</tt> immediately before the <tt>else</tt>. |
− | <syntaxhighlight> | + | |
+ | <syntaxhighlight lang=pascal> | ||
if BooleanExpression then | if BooleanExpression then | ||
begin | begin | ||
− | Statement1; | + | Statement1; // semicolon here is mandatory |
− | Statement2; | + | Statement2; // semicolon here is optional |
− | end | + | end // semicolon here is forbidden |
else | else | ||
begin | begin | ||
Line 43: | Line 67: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | If the Boolean expression evaluates to <tt>FALSE</tt>, the statement following the <tt>else</tt> will be performed. Note that you may <u>never</u> use a semicolon after the statement preceding the <tt>else</tt>. 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 | ||
− | If | + | If you need multi-way selection, simply nest <tt>if</tt> statements: |
− | + | <syntaxhighlight lang=pascal> | |
− | <syntaxhighlight> | ||
if Condition1 then | if Condition1 then | ||
Statement1 | Statement1 | ||
Line 58: | Line 82: | ||
Be careful with nesting. Sometimes the computer won't do what you want it to do: | Be careful with nesting. Sometimes the computer won't do what you want it to do: | ||
− | <syntaxhighlight> | + | |
+ | <syntaxhighlight lang=pascal> | ||
if Condition1 then | if Condition1 then | ||
if Condition2 then | if Condition2 then | ||
Line 67: | Line 92: | ||
The <tt>else</tt> is always matched with the most recent <tt>if</tt>, so the computer interprets the preceding block of code as: | The <tt>else</tt> is always matched with the most recent <tt>if</tt>, so the computer interprets the preceding block of code as: | ||
− | <syntaxhighlight> | + | |
+ | <syntaxhighlight lang=pascal> | ||
if Condition1 then | if Condition1 then | ||
if Condition2 then | if Condition2 then | ||
Line 76: | Line 102: | ||
You can get by with a null statement: | You can get by with a null statement: | ||
− | <syntaxhighlight> | + | |
+ | <syntaxhighlight lang=pascal> | ||
if Condition1 then | if Condition1 then | ||
if Condition2 then | if Condition2 then | ||
Line 84: | Line 111: | ||
Statement1; | Statement1; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | Or you could use a <tt>begin-end</tt> block. | ||
+ | |||
+ | The following proves a semicolon is ''absolutely forbidden'' before an else: | ||
+ | <syntaxhighlight lang=pascal> | ||
+ | // Paul Robinson 2020-12-16 | ||
+ | |||
+ | // Compiler test program Err03.pas | ||
+ | // tests the proposition that ; is | ||
+ | // never legal before ELSE | ||
+ | |||
+ | |||
+ | program err03; | ||
+ | Var | ||
+ | Test,test2: Boolean; | ||
+ | |||
+ | |||
+ | Begin | ||
+ | |||
+ | Test := True; | ||
+ | Test2 := true; | ||
+ | |||
+ | if test then | ||
+ | if test2 then | ||
+ | Writeln('Reached Part 1'); // semi-colon here should be illegal | ||
+ | else | ||
+ | Writeln('Reached Part 2'); | ||
+ | |||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | But the best way to clean up the code would be to rewrite the condition. | ||
− | + | <syntaxhighlight lang=pascal> | |
− | <syntaxhighlight> | ||
if not Condition1 then | if not Condition1 then | ||
Statement1 | Statement1 | ||
Line 98: | Line 154: | ||
Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation. | Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation. | ||
− | {{TYNavigator| | + | {{TYNavigator|Chapter 3/Boolean Expressions|Chapter 3/CASE}} |
Latest revision as of 16:18, 20 August 2022
│
български (bg) │
English (en) │
français (fr) │
日本語 (ja) │
中文(中国大陆) (zh_CN) │
Back to Reserved words.
3Ca - IF (author: Tao Yue, state: changed)
The IF statement allows you to branch based on the result of a Boolean operation.
Format
- IF expression THEN
- statemrnt1
- [ ELSE
- statement2]
Where
- expression is any comparison, constant or function which returns a boolen value, and
- statement1 and statement2 are either a single statement, a Begin-end block, a repeat-until block, or the null statement.
The ELSE clause is optional.
There are two ways to use an IF statement, a one-way branch or a two-way branch/
One-way branch
The one-way branch format is:
if BooleanExpression then
StatementIfTrue;
If the Boolean expression evaluates to true, the statement executes. Otherwise, it is skipped.
The IF statement accepts only one statement. If you would like to use a compound statement, you must use a begin-end frame to enclose the statements:
if BooleanExpression then
begin
Statement1;
Statement2;
end;
Two-way branch
There is also a two-way selection:
if BooleanExpression then
StatementIfTrue
else
StatementIfFalse;
Note there is never a semicolon ; immediately before the else.
if BooleanExpression then
begin
Statement1; // semicolon here is mandatory
Statement2; // semicolon here is optional
end // semicolon here is forbidden
else
begin
Statement3;
Statement4;
end;
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
If you need multi-way selection, simply nest if statements:
if Condition1 then
Statement1
else
if Condition2 then
Statement2
else
Statement3;
Be careful with nesting. Sometimes the computer won't do what you want it to do:
if Condition1 then
if Condition2 then
Statement2
else
Statement1;
The else is always matched with the most recent if, so the computer interprets the preceding block of code as:
if Condition1 then
if Condition2 then
Statement2
else
Statement1;
You can get by with a null statement:
if Condition1 then
if Condition2 then
Statement2
else
else
Statement1;
Or you could use a begin-end block.
The following proves a semicolon is absolutely forbidden before an else:
// Paul Robinson 2020-12-16
// Compiler test program Err03.pas
// tests the proposition that ; is
// never legal before ELSE
program err03;
Var
Test,test2: Boolean;
Begin
Test := True;
Test2 := true;
if test then
if test2 then
Writeln('Reached Part 1'); // semi-colon here should be illegal
else
Writeln('Reached Part 2');
end.
But the best way to clean up the code would be to rewrite the condition.
if not Condition1 then
Statement1
else
if Condition2 then
Statement2;
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.
Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.