Difference between revisions of "If and Then"

From Lazarus wiki
(use specific documentation link)
(review, code highlighting)
Line 1: Line 1:
 
{{If}}
 
{{If}}
  
This [[Keyword|keyword]] precedes a condition, must be followed by [[Then|then]] and may optionally be followed by [[Else|else]].
+
The <code>if</code> [[Keyword|keyword]] precedes a condition, must be followed by <code>[[Then|then]]</code> and a statement. The statement may optionally be followed by <code>[[Else|else]]</code> and another statement.
  
  
== If then ==
+
== <code>If then</code> ==
  
 
<syntaxhighlight>
 
<syntaxhighlight>
  if condition
+
if condition
    then true_statement
+
then true_statement
    else false_statement;
+
else false_statement;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Condition is a [[Boolean]] expression that evaluates to [[True|true]] or [[False|false]].
+
<code>condition</code> is a [[boolean]] expression that evaluates to [[True|true]] xor [[False|false]].
true_statement is executed if condition evaluates to true.
+
<code>true_statement</code> is executed if <code>condition</code> evaluates to <code>true</code>.
false_statement is executed if condition evaluates to false.
+
<code>false_statement</code> is executed if <code>condition</code> evaluates to <code>false</code>.
An error (''What error? Run-time? Compile-time? What number? An exception perhaps? Please fix this'') occurs if condition does not evaluate to a boolean value.
+
A compile-time error occurs if the type of <code>condition</code> does not evaluate to a <code>boolean</code> value.
  
  
=== More statements in "if then" statement ===
+
=== multiple statements in <code>if then</code> branch ===
  
If you need two or more statements for true_statement or false_statement, place them within a [[Begin|begin]] ... [[End|end]] [[Block]].
+
If you need two or more statements for <code>true_statement</code> or <code>false_statement<code>, enclose them within a <code>[[Begin|begin]] … [[End|end]]</code> [[Block]] (compound statement).
  
 
<syntaxhighlight>
 
<syntaxhighlight>
  if boolean_condition then
+
if boolean_condition then
    begin
+
begin
      statement_one;
+
statement_zero;
      statement_two;
+
statement_one;
      statement_three;
+
statement_two;
    end;
+
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Documentation ==
+
 
Official documentation: [http://www.freepascal.org/docs-html/ref/refsu58.html#x163-18500013.2.3]
+
== external references ==
 +
 
 +
* official documentation: [https://www.freepascal.org/docs-html/ref/refsu57.html Reference guide: § “The <code>If..then..else</code> statement”]
  
 
{{Keywords}}
 
{{Keywords}}
 
[[Category:Pascal]]
 
[[Category:Pascal]]
 
[[Category:Control Structures]]
 
[[Category:Control Structures]]

Revision as of 02:26, 24 January 2018

English (en)

The if keyword precedes a condition, must be followed by then and a statement. The statement may optionally be followed by else and another statement.


If then

if condition
	then true_statement
	else false_statement;

condition is a boolean expression that evaluates to true xor false. true_statement is executed if condition evaluates to true. false_statement is executed if condition evaluates to false. A compile-time error occurs if the type of condition does not evaluate to a boolean value.


multiple statements in if then branch

If you need two or more statements for true_statement or false_statement, enclose them within a begin … end Block (compound statement).

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


external references


Keywords: begindoelseendforifrepeatthenuntilwhile