Difference between revisions of "If and Then"

From Lazarus wiki
m
(unify code style; rephrase sentence misusing the term “block” (as it is defined in the article Block); insert Usage)
 
(17 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{If}}
+
{{then}}
  
This [[Keyword|keyword]] precedes a condition, is required to be followed by [[Then|then]] and may optionally be followed by [[Else|else]].
+
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.
 +
This creates a binary [[Branch|branch]].
  
== If then ==
+
== <syntaxhighlight lang="pascal" inline>If then</syntaxhighlight> ==
 
+
<syntaxhighlight lang="pascal">
<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]].
+
<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>]].
true_statement is executed if condition evaluates to true.
+
<syntaxhighlight lang="pascal" inline>true_statement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" inline>condition</syntaxhighlight> evaluates to <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>.
false_statement is executed if condition evaluates to false.
+
<syntaxhighlight lang="pascal" inline>false_statement</syntaxhighlight> is executed if <syntaxhighlight lang="pascal" inline>condition</syntaxhighlight> evaluates to <syntaxhighlight lang="pascal" inline>false</syntaxhighlight>.
An error occurs if condition does not evaluate to a boolean 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" inline>if then</syntaxhighlight> branch ===
 +
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>]] frame (“compound statement”).
  
=== More statements in "if then" statement ===
+
<syntaxhighlight lang="pascal">
 
+
if boolean_condition then
If you need two or more statements for true_statement or false_statement, place them within a [[Begin|begin]] ... [[End|end]] [[Block]].
+
begin
 +
statement_zero;
 +
statement_one;
 +
statement_two;
 +
end;
 +
</syntaxhighlight>
  
<syntaxhighlight>
+
== Usage ==
  if boolean_condition then
+
In order to optimize for speed in an <syntaxhighlight lang="pascal" inline>if … then … else</syntaxhighlight> branch, try to write your expression so that the <syntaxhighlight lang="pascal" inline>then</syntaxhighlight>-part gets executed most often.
    begin
+
This improves the rate of successful <syntaxhighlight lang="asm" inline>jump</syntaxhighlight> predictions.
      statement_one;
 
      statement_two;
 
      statement_three;
 
    end;
 
</syntaxhighlight>
 
  
 +
== See also ==
 +
* 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 statement and semicolon|If statement and semicolon]]
 +
* [[Case|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>]]
  
 
{{Keywords}}
 
{{Keywords}}
[[Category:Pascal]]
 

Latest revision as of 08:37, 22 June 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. This creates a binary branch.

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 frame (“compound statement”).

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

Usage

In order to optimize for speed in an if  then  else branch, try to write your expression so that the then-part gets executed most often. This improves the rate of successful jump predictions.

See also


Keywords: begindoelseendforifrepeatthenuntilwhile