Starts a conditional expression to determine what to do next
1 keyword If(if Condition then Statement;
2 if Condition then Statement else Statement;
The If keyword is used to control the flow of code depending on the logical result of the given condition.
There are two forms of the If statement - one with an else clause, the other not.
If works as follows :
If the condition is true, then the first statement is executed. If false, then this statement is bypassed. If there is an else statement, it is executed instead.
In all cases, the Statement clause must be a contained in a begin/end block if it is longer than one statement in length.
It is an easy mistake to make, but Delphi insists on no ';' after the then statement if an else statement follows.
Related commands
BooleanAllows just True and False values
ElseStarts false section of if, case and try statements
EndKeyword that terminates statement blocks
ThenPart of an if statement - starts the true clause
 Download this web site as a Windows program.

Example code : Illustrate the different flavours of the if statement
  // Illustrate a simple if statement that executes true
  If True then ShowMessage('True!');

  // Illustrate the same, but with multiple actions
  If 1 = 1 then
    ShowMessage('We now have');
    ShowMessage('multiple lines');

  // Illustrate a simple if statement that fails
  If 1 = 2 then ShowMessage('1 = 2');

  // Illustrate an if then else statement
  // Note the lack of a ';' after the 'then' clause
  If False
  then ShowMessage('True')
  else ShowMessage('False');

  // Nested if statements - Delphi sensibly manages associations
  If true then
    If false then
      ShowMessage('Inner then satisfied')
      ShowMessage('Inner else satisfied')
    ShowMessage('Outer else satisfied')
Show full unit code
  We now have
  multiple lines
  Inner else satisfied
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page