Difference between revisions of "If and Then"

From Free Pascal wiki
Jump to navigationJump to search
(better syntax highlighting)
Line 1: Line 1:
 
{{If}}
 
{{If}}
  
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.
+
The <syntaxhighlight lang="pascal" enclose="none">if</syntaxhighlight> [[Keyword|keyword]] precedes a condition, must be followed by [[Then|<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.
  
  
== <code>If then</code> ==
+
== <syntaxhighlight lang="pascal" enclose="none">If then</syntaxhighlight> ==
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
if condition
 
if condition
 
then true_statement
 
then true_statement
Line 12: Line 13:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<code>condition</code> is a [[Boolean|boolean]] expression that evaluates to [[True|true]] xor [[False|false]].
+
<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>]].
<code>true_statement</code> is executed if <code>condition</code> evaluates to <code>true</code>.
+
<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>.
<code>false_statement</code> is executed if <code>condition</code> evaluates to <code>false</code>.
+
<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>.
A compile-time error occurs if the type of <code>condition</code> does not evaluate to a <code>boolean</code> value.
+
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.
  
  
=== Multiple statements in <code>if then</code> branch ===
+
=== Multiple statements in <syntaxhighlight lang="pascal" enclose="none">if then</syntaxhighlight> branch ===
  
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).
+
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).
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
if boolean_condition then
 
if boolean_condition then
 
begin
 
begin
Line 34: Line 35:
 
== See also ==
 
== See also ==
  
* Official documentation: [https://www.freepascal.org/docs-html/ref/refsu57.html Reference guide: § “The <code>If..then..else</code> statement”]
+
* Official documentation: [https://www.freepascal.org/docs-html/ref/refsu57.html Reference guide: § “The <syntaxhighlight lang="pascal" enclose="none">If..then..else</syntaxhighlight> statement”]
 
* [[IF]], Tao Yue, Object Pascal Introduction
 
* [[IF]], Tao Yue, Object Pascal Introduction
 
* [[;#If statement and semicolon|If statement and semicolon]]
 
* [[;#If statement and semicolon|If statement and semicolon]]

Revision as of 18:42, 11 February 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;


See also


Keywords: begindoelseendforifrepeatthenuntilwhile