Difference between revisions of "Basic Pascal Tutorial/Chapter 3/IF"

From Lazarus wiki
Jump to navigationJump to search
Line 2: Line 2:
  
 
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>
+
<delphi>
  StatementIfTrue<font color="#000000"><strong>;</strong></font>
+
if BooleanExpression then
 +
  StatementIfTrue;  
 +
</delphi>
  
 
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> to enclose the statements:
<font color="#006699"><strong>if</strong></font> BooleanExpression <font color="#006699"><strong>then</strong></font>
+
<delphi>
<font color="#006699"><strong>begin</strong></font>
+
if BooleanExpression then
  Statement1<font color="#000000"><strong>;</strong></font>
+
begin
  Statement2
+
  Statement1;
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
  Statement2
 +
end;  
 +
</delphi>
  
 
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>
+
<delphi>
  StatementIfTrue
+
if BooleanExpression then
<font color="#006699"><strong>else</strong></font>
+
  StatementIfTrue
  StatementIfFalse<font color="#000000"><strong>;</strong></font>
+
else
 +
  StatementIfFalse;  
 +
</delphi>
  
 
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.
 
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.
  
 
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>
+
<delphi>
  Statement1
+
if Condition1 then
<font color="#006699"><strong>else</strong></font>
+
  Statement1
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
else
    Statement2
+
  if Condition2 then
  <font color="#006699"><strong>else</strong></font>
+
    Statement2
    Statement3<font color="#000000"><strong>;</strong></font>
+
  else
 +
    Statement3;
 +
</delphi>
  
 
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>
+
<delphi>
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
if Condition1 then
    Statement2
+
  if Condition2 then
<font color="#006699"><strong>else</strong></font>
+
    Statement2
  Statement1<font color="#000000"><strong>;</strong></font>
+
else
 +
  Statement1;
 +
</delphi>
  
 
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>
+
<delphi>
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
if Condition1 then
    Statement2
+
  if Condition2 then
  <font color="#006699"><strong>else</strong></font>
+
    Statement2
    Statement1<font color="#000000"><strong>;</strong></font>
+
  else
 +
    Statement1;
 +
</delphi>
  
 
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>
+
<delphi>
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
if Condition1 then
    Statement2
+
  if Condition2 then
  <font color="#006699"><strong>else</strong></font>
+
    Statement2
<font color="#006699"><strong>else</strong></font>
+
  else
  Statement1<font color="#000000"><strong>;</strong></font>
+
else
 +
  Statement1;
 +
</delphi>
  
 
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.
 
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.
<font color="#006699"><strong>if</strong></font> <font color="#006699"><strong>not</strong></font> Condition1 <font color="#006699"><strong>then</strong></font>
+
<delphi>
  Statement1
+
if not Condition1 then
<font color="#006699"><strong>else</strong></font>
+
  Statement1
  <font color="#006699"><strong>if</strong></font> Condition2 <font color="#006699"><strong>then</strong></font>
+
else
    Statement2<font color="#000000"><strong>;</strong></font>
+
  if Condition2 then
 +
    Statement2;
 +
</delphi>
  
 
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.

Revision as of 15:42, 5 January 2010

3Ca - IF (author: Tao Yue, state: unchanged)

The IF statement allows you to branch based on the result of a Boolean operation. The one-way branch format is: <delphi> if BooleanExpression then

 StatementIfTrue; 

</delphi>

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 to enclose the statements: <delphi> if BooleanExpression then begin

 Statement1;
 Statement2

end; </delphi>

There is also a two-way selection: <delphi> if BooleanExpression then

 StatementIfTrue

else

 StatementIfFalse; 

</delphi>

If the Boolean expression evaluates to FALSE, the statement following the else will be performed. Note that you may not 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.

If you need multi-way selection, simply nest if statements: <delphi> if Condition1 then

 Statement1

else

 if Condition2 then
   Statement2
 else
   Statement3;

</delphi>

Be careful with nesting. Sometimes the computer won't do what you want it to do: <delphi> if Condition1 then

 if Condition2 then
   Statement2

else

 Statement1;

</delphi>

The else is always matched with the most recent if, so the computer interprets the preceding block of code as: <delphi> if Condition1 then

 if Condition2 then
   Statement2
 else
   Statement1;

</delphi>

You can get by with a null statement: <delphi> if Condition1 then

 if Condition2 then
   Statement2
 else

else

 Statement1;

</delphi>

Or you could use a begin-end block. But the best way to clean up the code would be to rewrite the condition. <delphi> if not Condition1 then

 Statement1

else

 if Condition2 then
   Statement2;

</delphi>

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.


previous contents next