If and Then
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
- Official documentation: Reference guide: § “The
If..then..elsestatement” - Basic Pascal Tutorial/Chapter 3/IF, Tao Yue, Basic Pascal Introduction
- If statement and semicolon
- case
From the Free Pascal wiki, page "If and Then", as it stood on 2023-01-25 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 10 people, 2006–2023
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Rfc1394 | ||
| Swen | (minor) | |
| Swen | (minor) | |
| Djzepi | ||
| Djzepi | ||
| Djzepi | ||
| Rfc1394 | ||
| Swen | (minor) | |
| Vincent | Text replace - "delphi>" to "syntaxhighlight>" (minor) | |
| Olaf | (minor) | |
| Jwdietrich | ||
| BigChimp | language. BTW: the "an error occurs" line is vague. Run-time? Design-time? Error? What number? Exception? | |
| BigChimp | link to documentation | |
| BigChimp | what error? Suggest removing the entire sentence actually | |
| FPC user | use specific documentation link | |
| Kai Burghardt | review, code highlighting | |
| Djzepi | ||
| Djzepi | ||
| Kai Burghardt | better syntax highlighting | |
| Kai Burghardt | Kai Burghardt moved page If to If and Then: “if” and “then” only occur together (minor) | |
| Kai Burghardt | unlink Then (minor) | |
| Kai Burghardt | link Case (minor) | |
| Kai Burghardt | usage of Template:Translate; eliminate excessive space | |
| Trev | Fixed template loop (minor) | |
| Trev | Fixed syntax highlighting; deleted category included in page template (minor) | |
| Trev | Text replacement - "Object Pascal Introduction" to "Basic Pascal Introduction" (minor) | |
| Kai Burghardt | remove “back to <some random page>” link, as there are multiple pages linking here; substitute legacy syntaxhighlight syntax | |
| Kai Burghardt | unify code style; rephrase sentence misusing the term “block” (as it is defined in the article Block); insert Usage | |
| Kai Burghardt | bypass redirect to Basic Pascal Tutorial/Chapter 3/IF |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
