Difference between revisions of "If and Then"

From Lazarus wiki
Jump to navigationJump to search
m (Text replacement - "Object Pascal Introduction" to "Basic Pascal Introduction")
(remove “back to <some random page>” link, as there are multiple pages linking here; substitute legacy syntaxhighlight syntax)
Line 1: Line 1:
 
{{then}}
 
{{then}}
  
 +
The <syntaxhighlight lang="pascal" inline>if</syntaxhighlight> [[Keyword|keyword]] precedes a condition, must be followed by <syntaxhighlight lang="pascal" inline>then</syntaxhighlight> and a statement.
 +
The statement may optionally be followed by [[Else|<syntaxhighlight lang="pascal" inline>else</syntaxhighlight>]] and another statement.
  
Back to [[Reserved words]].
+
== <syntaxhighlight lang="pascal" inline>If then</syntaxhighlight> ==
 
 
 
 
The <syntaxhighlight lang="pascal" enclose="none">if</syntaxhighlight> [[Keyword|keyword]] precedes a condition, must be followed by <syntaxhighlight lang="pascal" enclose="none">then</syntaxhighlight> and a statement.
 
The statement may optionally be followed by [[Else|<syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight>]] and another statement.
 
 
 
== <syntaxhighlight lang="pascal" enclose="none">If then</syntaxhighlight> ==
 
  
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
Line 16: Line 12:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<syntaxhighlight lang="pascal" enclose="none">condition</syntaxhighlight> is a [[Boolean|<syntaxhighlight lang="pascal" enclose="none">boolean</syntaxhighlight>]] expression that evaluates to [[True|<syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>]] xor [[False|<syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>]].
+
<syntaxhighlight lang="pascal" inline>condition</syntaxhighlight> is a [[Boolean|<syntaxhighlight lang="pascal" inline>boolean</syntaxhighlight>]] expression that evaluates to [[True|<syntaxhighlight lang="pascal" inline>true</syntaxhighlight>]] xor [[False|<syntaxhighlight lang="pascal" inline>false</syntaxhighlight>]].
<syntaxhighlight lang="pascal" enclose="none">true_statement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" enclose="none">condition</syntaxhighlight> evaluates to <syntaxhighlight lang="pascal" enclose="none">true</syntaxhighlight>.
+
<syntaxhighlight lang="pascal" inline>true_statement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" inline>condition</syntaxhighlight> evaluates to <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>.
<syntaxhighlight lang="pascal" enclose="none">false_statement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" enclose="none">condition</syntaxhighlight> evaluates to <syntaxhighlight lang="pascal" enclose="none">false</syntaxhighlight>.
+
<syntaxhighlight lang="pascal" inline>false_statement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" inline>condition</syntaxhighlight> evaluates to <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>.
A compile-time error occurs if the type of <syntaxhighlight lang="pascal" enclose="none">condition</syntaxhighlight> does not evaluate to a <syntaxhighlight lang="pascal" enclose="none">boolean</syntaxhighlight> value.
+
A compile-time error occurs if the type of <syntaxhighlight lang="pascal" inline>condition</syntaxhighlight> does not evaluate to a <syntaxhighlight lang="pascal" inline>boolean</syntaxhighlight> value.
  
=== Multiple statements in <syntaxhighlight lang="pascal" enclose="none">if then</syntaxhighlight> branch ===
+
=== Multiple statements in <syntaxhighlight lang="pascal" inline>if then</syntaxhighlight> branch ===
If you need two or more statements for <syntaxhighlight lang="pascal" enclose="none">true_statement</syntaxhighlight> or <syntaxhighlight lang="pascal" enclose="none">false_statement</syntaxhighlight>, enclose them within a [[Begin|<syntaxhighlight lang="pascal" enclose="none">begin</syntaxhighlight>]]&nbsp;…&nbsp;[[End|<syntaxhighlight lang="pascal" enclose="none">end</syntaxhighlight>]] [[Block]] (compound statement).
+
If you need two or more statements for <syntaxhighlight lang="pascal" inline>true_statement</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>false_statement</syntaxhighlight>, enclose them within a [[Begin|<syntaxhighlight lang="pascal" inline>begin</syntaxhighlight>]]&nbsp;…&nbsp;[[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]] [[Block]] (compound statement).
  
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
Line 35: Line 31:
 
== See also ==
 
== See also ==
  
* Official documentation: [https://www.freepascal.org/docs-html/ref/refsu57.html Reference guide: § “The <syntaxhighlight lang="pascal" enclose="none">If..then..else</syntaxhighlight> statement”]
+
* Official documentation: [https://www.freepascal.org/docs-html/ref/refsu57.html Reference guide: § “The <syntaxhighlight lang="pascal" inline>If..then..else</syntaxhighlight> statement”]
 
* [[IF]], Tao Yue, Basic Pascal Introduction
 
* [[IF]], Tao Yue, Basic Pascal Introduction
 
* [[;#If statement and semicolon|If statement and semicolon]]
 
* [[;#If statement and semicolon|If statement and semicolon]]
* [[Case|<syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>]]
+
* [[Case|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>]]
  
 
{{Keywords}}
 
{{Keywords}}

Revision as of 04:52, 16 May 2020

Deutsch (de) English (en) français (fr) русский (ru)

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;

See also


Keywords: begindoelseendforifrepeatthenuntilwhile