then expression works similarly to the common
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' else '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.
else clause and Nullability
Like with the
then Statement, the
else clause is optional. If it is omitted, the value of the
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
If the condition is a simple boolean, the
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
then statement treats them the same, and will execute the
else clause, if provided, in this case.