The If statement can also be referred to as an If...Then statement or an If….Else statement.

The If statement is structured as If <condition> Then <TrueStatement> else <FalseStatement>.

The If statement provides for conditional processing, which means that the statement tests for a condition to be either True or False, then executes a different statement or group of statements based on the result.

So if the test is True, it executes the one statement specified for True. And if the test returns False, then a different specified statement will be executed. This allows you to provide totally independent code to handle these different situations.

Execute One Statement Only If Result Is True (If...Then)

The most basic form of the If statement provides only the response to the If statement returning True.

In this case, there is no False statement response provided and nothing occurs if the result is False.

By reversing the test, you can provide either a True or False statement response, and nothing for the other result.

This is also referred to as an If….Then statement - If the condition is met and is True, Then do this.

The following example tests a variable called Value to check whether the number it contains is greater than 10.

procedure ScriptEvent (var Value : variant);
begin
if Value > 10 then LogInfo('Value is greater than 10');
end;

If the number held by Value is greater than 10 then the statement condition is deemed to have been met and the statement response is True - therefore, the LogInfo statement is executed.

However, if the number held by Value is less than 10, then the statement condition is deemed to have not been met and the statement response is False - therefore nothing happens.

If you only wanted to execute a statement if the result is False , then you need to reverse the condition.

Execute Different Statements If Result Is True Or False (If...Else)

In this form of the If statement, there is a different response supplied depending on whether the statement condition response is True or whether it is False.

In this case one statement is executed if the result is True, and a different statement is executed if the result is False.

This is sometimes referred to as an If...Else statement - If the condition is True do this, Else where the condition is False do this.

An If…Else statement is considered to be one whole statement, therefore no semi-colon (;) is required at the end of the True statement (before the Else).

The following example tests a variable called Value to check whether the number it holds is greater than 10.

procedure ScriptEvent (var Value : variant);
begin
if Value > 10 then LogInfo('Value is greater than 10') else LogInfo('Value is NOT greater than 10');
end;

Depending of the result, only one of two available LogInfo statements will be executed.

Note that a semi-colon is only required at the end of the entire statement.

Execute Multiple Statements (If...Then and If...Else)

You can of course execute multiple statements within a Begin...End block within the If statement.

If the condition is True then execute these multiple statements, Else where the condition is False execute these multiple statements.

The following example tests a variable called Value to check whether the number it contains is greater than 10.

procedure ScriptEvent (var Value : variant);
begin
if Value > 10 then
begin
LogInfo('Value is greater than 10');
LogInfo('This is the second statement to execute if the result TRUE');
end
else
begin
LogInfo('Value is NOT greater than 10');
LogInfo('This is the second statement to execute if result is FALSE');
end;
end;

A Begin...End statement block wraps each group of statements, and depending on the result response of True or False, only one group of LogInfo statements will be executed.

A semi-colon is required once at the end of the entire If statement, as well as at the end of each statement contained within the blocks. In this case there are 5 statements in the above code - the If statement and its Begin...End statement blocks, and the 4 LogInfo statements within the blocks. All indicated by the arrows below.

In scripting terms, it makes no difference whether you have the end else on the same line, or on two separate lines as illustrated above. The same code with the end else on the same line follows.

procedure ScriptEvent (var Value : variant);
begin
if Value > 10 then
begin
LogInfo('Value is greater than 10');
LogInfo('This is the second statement to execute if the result TRUE');
end else
begin
LogInfo('Value is NOT greater than 10');
LogInfo('This is the second statement to execute if result is FALSE');
end;
end;

Nested If Statements

If statements can also be nested to provide further conditional checks.

In the following example, a check is made on the variable called MyVariable to ascertain whether the result is True or False.

procedure ScriptEvent (var Value : variant);
begin
if MyVariable = True then
begin
if Value > 10 then LogInfo('It's greater than 10!') else LogInfo('It's NOT greater than 10!');
end;
end;

If the result of this check on MyVariable returns True, then a further test is performed on the variable called Value to determine whether the contents within Value is greater than 10. But this second If statement is only executed if the first If statement from MyVariable results in True.

An alternative to nested If statements, would be to use the Case statement, however the Case statement can only be used with testing of ordinal values. For additional information please refer to Case .

Multiple Conditions

You can also check for multiple conditions within an If statement.

When doing so you need to enclose each condition within parentheses ( ), with each condition joined together through the use of a boolean operator such as and or not. For more information on these operators, please refer to

This example checks two conditions.

procedure ScriptEvent (var Value : variant);
begin
if (MyVariable=True) and (Value > 10) then
LogInfo('Value is greater than 10')
else
LogInfo('Value is NOT greater than 10');
end;

Only if the response from both conditions results in True will the True statement be executed - MyVariable must return True and the variable Value must hold a number greater than 10. If either condition is not True, then the False statement will be executed.

The word and is a boolean operator. If the boolean operator or was used then only one of the conditions needs to result in True for the True statement to be executed.