Difference between revisions of "IF"

From Lazarus wiki
(New page: 3Ca - IF The <tt>IF</tt> statement allows you to branch based on the result of a Boolean operation. The one-way branch format is: <font color="#006699"><strong>if</strong></font> Boolean...)
 
(“block” → “frame”)
 
(14 intermediate revisions by 8 users not shown)
Line 1: Line 1:
3Ca - IF
+
{{IF}}
 +
{{TYNavigator|Boolean_Expressions|CASE}}
 +
 
 +
 
 +
Back to [[Reserved words]].
 +
 
 +
 
 +
3Ca - IF (author: Tao Yue, state: changed)
  
 
The <tt>IF</tt> statement allows you to branch based on the result of a Boolean operation. The one-way branch format is:
 
The <tt>IF</tt> statement allows you to branch based on the result of a Boolean operation. The one-way branch format is:
<font color="#006699"><strong>if</strong></font> BooleanExpression <font color="#006699"><strong>then</strong></font>
+
 
  StatementIfTrue<font color="#000000"><strong>;</strong></font>
+
<syntaxhighlight lang=pascal>
 +
if BooleanExpression then
 +
  StatementIfTrue;  
 +
</syntaxhighlight>
  
 
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 branch to a compound statement, you must use a <tt>begin-end</tt> to enclose the statements:
+
The <tt>IF</tt> statement accepts only one statement. If you would like to branch to a compound statement, you must use a <tt>begin-end</tt> [[Frame|frame]] to enclose the statements:
<font color="#006699"><strong>if</strong></font> BooleanExpression <font color="#006699"><strong>then</strong></font>
+
 
<font color="#006699"><strong>begin</strong></font>
+
<syntaxhighlight lang=pascal>
  Statement1<font color="#000000"><strong>;</strong></font>
+
if BooleanExpression then
  Statement2
+
begin
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
  Statement1;
 +
  Statement2;
 +
end;  
 +
</syntaxhighlight>
  
 
There is also a two-way selection:
 
There is also a two-way selection:
<font color="#006699"><strong>if</strong></font> BooleanExpression <font color="#006699"><strong>then</strong></font>
 
  StatementIfTrue
 
<font color="#006699"><strong>else</strong></font>
 
  StatementIfFalse<font color="#000000"><strong>;</strong></font>
 
  
If the Boolean expression evaluates to <tt>FALSE</tt>, the statement following the <tt>else</tt> will be performed. Note that you may not 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.
+
<syntaxhighlight lang=pascal>
 +
if BooleanExpression then
 +
  StatementIfTrue
 +
else
 +
  StatementIfFalse;
 +
</syntaxhighlight>
 +
 
 +
Note there is no <tt>;</tt> following the statement before the <tt>else</tt>, even for the case with compound statements.
 +
 
 +
<syntaxhighlight lang=pascal>
 +
if BooleanExpression then
 +
begin
 +
  Statement1;
 +
  Statement2;
 +
end
 +
else
 +
begin
 +
  Statement3;
 +
  Statement4;
 +
end;
 +
</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 you need multi-way selection, simply nest <tt>if</tt> statements:
 
If you need multi-way selection, simply nest <tt>if</tt> statements:
<font color="#006699"><strong>if</strong></font> Condition1 <font color="#006699"><strong>then</strong></font>
+
 
  Statement1
+
<syntaxhighlight lang=pascal>
<font color="#006699"><strong>else</strong></font>
+
if Condition1 then
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
  Statement1
    Statement2
+
else
  <font color="#006699"><strong>else</strong></font>
+
  if Condition2 then
    Statement3<font color="#000000"><strong>;</strong></font>
+
    Statement2
 +
  else
 +
    Statement3;
 +
</syntaxhighlight>
  
 
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:
<font color="#006699"><strong>if</strong></font> Condition1 <font color="#006699"><strong>then</strong></font>
+
 
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
<syntaxhighlight lang=pascal>
    Statement2
+
if Condition1 then
<font color="#006699"><strong>else</strong></font>
+
  if Condition2 then
  Statement1<font color="#000000"><strong>;</strong></font>
+
    Statement2
 +
else
 +
  Statement1;
 +
</syntaxhighlight>
  
 
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:
<font color="#006699"><strong>if</strong></font> Condition1 <font color="#006699"><strong>then</strong></font>
+
 
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
<syntaxhighlight lang=pascal>
    Statement2
+
if Condition1 then
  <font color="#006699"><strong>else</strong></font>
+
  if Condition2 then
    Statement1<font color="#000000"><strong>;</strong></font>
+
    Statement2
 +
  else
 +
    Statement1;
 +
</syntaxhighlight>
  
 
You can get by with a null statement:
 
You can get by with a null statement:
<font color="#006699"><strong>if</strong></font> Condition1 <font color="#006699"><strong>then</strong></font>
 
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
 
    Statement2
 
  <font color="#006699"><strong>else</strong></font>
 
<font color="#006699"><strong>else</strong></font>
 
  Statement1<font color="#000000"><strong>;</strong></font>
 
  
Or you could use a <tt>begin-end</tt> block. But the best way to clean up the code would be to rewrite the condition.
+
<syntaxhighlight lang=pascal>
<font color="#006699"><strong>if</strong></font> <font color="#006699"><strong>not</strong></font> Condition1 <font color="#006699"><strong>then</strong></font>
+
if Condition1 then
  Statement1
+
  if Condition2 then
<font color="#006699"><strong>else</strong></font>
+
    Statement2
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
  else
    Statement2<font color="#000000"><strong>;</strong></font>
+
else
 +
  Statement1;
 +
</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>
 +
if not Condition1 then
 +
  Statement1
 +
else
 +
  if Condition2 then
 +
    Statement2;
 +
</syntaxhighlight>
  
 
This example illustrates where the not operator comes in very handy. If Condition1 had been a Boolean like: <tt>(not(a < b) or (c + 3 > 6)) and g</tt>, reversing the expression would be more difficult than NOTting it.
 
This example illustrates where the not operator comes in very handy. If Condition1 had been a Boolean like: <tt>(not(a < b) or (c + 3 > 6)) and g</tt>, reversing the expression would be more difficult than NOTting it.
Line 64: Line 139:
 
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|Boolean_Expressions|CASE}}
{|style=color-backgroud="white" cellspacing="20"
 
|[[Boolean_Expressions|previous]] 
 
|[[op_contents|contents]]
 
|[[CASE|next]]
 
|}
 

Latest revision as of 19:50, 24 January 2021

български (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. 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 branch to a compound statement, you must use a begin-end frame to enclose the statements:

if BooleanExpression then
begin
  Statement1;
  Statement2;
end;

There is also a two-way selection:

if BooleanExpression then
  StatementIfTrue
else
  StatementIfFalse;

Note there is no ; following the statement before the else, even for the case with compound statements.

if BooleanExpression then
begin
  Statement1;
  Statement2;
end
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.

 ◄   ▲   ►