If/Then/Else Expressions

The if/then expression works similarly to the common if/then Statement , except that is an expression that presents a value – namely that of either the then sub-expression or the (optional) else sub-expression, depending on whether the condition is true or false.

var lDescriptiveText := if x < 10 then
                          'less than ten'
                          'ten or more'

In the above example, the variable lDescriptiveText is assigned one of two values, depending on whether the condition x < 10 is true or false.

Optional else clause and Nullability

Like with the if/then Statement, the else clause is optional. If it is omitted, the value of the if/then expression for a false condition will be nil. This implies that if the type of the then expression is a non-nullable type, such as a value type, the type of the whole expression will be upgraded to be nullable.

var lCount := if assigned(s) then s.Length; // lCount will be a nullable Integer

Nullable Conditions

The condition expression for the if/then expression must be of Boolean or Nullable Boolean type.

If the condition is a simple boolean, the if/then statement will execute the then clause if the condition is true, and the (optional) else clause if the condition is false.

If the condition is a Nullable Boolean type, then the additional case of the condition evaluating to nil needs to be considered. While a nil nullable boolean strictly speaking is not equivalent to false, the if/then statement treats them the same, and will execute the else clause, if provided, in this case.

See Also