Difference between revisions of "IF"

From Lazarus wiki
(Added ; where missing and addition of compound statement example.)
m (Fixed syntax highlighting)
Line 1: Line 1:
 
{{IF}}
 
{{IF}}
 
{{TYNavigator|Boolean_Expressions|CASE}}
 
{{TYNavigator|Boolean_Expressions|CASE}}
 +
 +
 +
Back to [[Reserved words]].
 +
  
 
3Ca - IF (author: Tao Yue, state: unchanged)
 
3Ca - IF (author: Tao Yue, state: unchanged)
  
 
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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
if BooleanExpression then
 
if BooleanExpression then
 
   StatementIfTrue;  
 
   StatementIfTrue;  
Line 13: Line 18:
  
 
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> [[block]] 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> [[block]] to enclose the statements:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
if BooleanExpression then
 
if BooleanExpression then
 
begin
 
begin
Line 22: Line 28:
  
 
There is also a two-way selection:
 
There is also a two-way selection:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
if BooleanExpression then
 
if BooleanExpression then
 
   StatementIfTrue
 
   StatementIfTrue
Line 30: Line 37:
  
 
Note there is no <tt>;</tt> following the statement before the <tt>else</tt>, even for the case with compound statements.
 
Note there is no <tt>;</tt> following the statement before the <tt>else</tt>, even for the case with compound statements.
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
if BooleanExpression then
 
if BooleanExpression then
 
begin
 
begin
Line 47: Line 55:
  
 
If you need multi-way selection, simply nest <tt>if</tt> statements:
 
If you need multi-way selection, simply nest <tt>if</tt> statements:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
if Condition1 then
 
if Condition1 then
 
   Statement1
 
   Statement1
Line 58: Line 67:
  
 
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 77:
  
 
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 87:
  
 
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 86: Line 98:
  
 
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.
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
if not Condition1 then
 
if not Condition1 then
 
   Statement1
 
   Statement1

Revision as of 11:08, 17 February 2020

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 


Back to Reserved words.


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:

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 block 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 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:

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. 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.

 ◄   ▲   ►