Difference between revisions of "If and Then"

From Free Pascal wiki
Jump to navigationJump to search
(unify code style; rephrase sentence misusing the term “block” (as it is defined in the article Block); insert →‎Usage)
 
Line 35: Line 35:
 
== See also ==
 
== 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”]
 
* 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
+
* [[Basic Pascal Tutorial/Chapter 3/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" inline>case</syntaxhighlight>]]
 
* [[Case|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>]]
  
 
{{Keywords}}
 
{{Keywords}}

Latest revision as of 05:06, 25 January 2023

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