Difference between revisions of "If and Then"

From Lazarus wiki
Line 1: Line 1:
This [[Keyword|keyword]] preciedes a condition, is required to be followed by [[Then|then]] and may be followed by [[Else|else]].
+
This [[Keyword|keyword]] precedes a condition, is required to be followed by [[Then|then]] and may optionally be followed by [[Else|else]].
  
 
== If then ==
 
== If then ==
Line 5: Line 5:
 
<delphi>
 
<delphi>
 
   if condition
 
   if condition
     then action1
+
     then true_statement
     else action2;
+
     else false_statement;
 
</delphi>
 
</delphi>
  
Condition is an expression that evaluates to [[True|true]] or [[False|false]].
+
Condition is a [[Boolean]] expression that evaluates to [[True|true]] or [[False|false]].
Action1 is executed if condition evaluates to true.
+
true_statement is executed if condition evaluates to true.
Action2 is executed if condition evaluates to false.
+
false_statement is executed if condition evaluates to false.
 +
An error occurs if condition does not evaluate to a boolean value.
 +
 
  
 
=== More statements in "if then" statement ===
 
=== More statements in "if then" statement ===
  
If you need two or more statements in "if then" statement. In that case, you need the [[Begin|begin]] ... [[End|end]] around the statements.
+
If you need two or more statements for true_statement or false_statement, place them within a [[Begin|begin]] ... [[End|end]] [[Block]].
  
 
<delphi>
 
<delphi>

Revision as of 22:58, 26 October 2010

This keyword precedes a condition, is required to be followed by then and may optionally be followed by else.

If then

<delphi>

 if condition
   then true_statement
   else false_statement;

</delphi>

Condition is a Boolean expression that evaluates to true or false. true_statement is executed if condition evaluates to true. false_statement is executed if condition evaluates to false. An error occurs if condition does not evaluate to a boolean value.


More statements in "if then" statement

If you need two or more statements for true_statement or false_statement, place them within a begin ... end Block.

<delphi>

 if boolean_condition then
   begin
     statement_one;
     statement_two;
     statement_three;
   end;

</delphi>



Keywords: begindoelseendforifrepeatthenuntilwhile