Conditional expression

A conditional expression chooses between two values according to a condition, inside an expression. Templates and rules also have their own ways to choose what to display, or when a rule applies. You find them in the template and rule entries listed under See also.

Conditional expression

A conditional expression gives one of two values, depending on whether a condition is TRUE or FALSE.

Syntax
if <condition> then <value-if-true> else <value-if-false>
<condition>

an expression that gives a single indicator.

<value-if-true>

the value of the conditional expression when the condition is TRUE.

<value-if-false>

the value of the conditional expression when the condition is FALSE. It can itself be a conditional expression.

EFX tests the condition first. When the condition is TRUE, the conditional expression gives <value-if-true>. Otherwise, it gives <value-if-false>. A comparison with a field that is not present in the notice is FALSE (see Comparison operators). In that case, the conditional expression gives <value-if-false>.

The two values must have the same data type: text, number, indicator, date, time or duration. The conditional expression has that type. When both values are single values, the conditional expression is a single value. When one of them is a sequence, the conditional expression is a sequence, and you can use it only where a sequence is expected.

To test several conditions in turn, write another conditional expression after else, as in the fourth example below.

A conditional expression has the lowest precedence: <value-if-false> extends as far as possible to the right (see Parentheses and precedence). To use a conditional expression as an operand, put it in parentheses: (if BT-00-Number > 0 then BT-00-Number else 0) + 1. Without the parentheses, + 1 would belong to the value after else.

Restrictions

  • You must write both values: if TRUE then 1 is an error.

  • The two values must have the same type: if BT-00-Text is present then BT-00-Text else BT-00-Number is an error.

  • The condition must be a single indicator. if BT-00-Number then 1 else 2 is an error, and so is if BT-13-Indicator then 1 else 2 from ND-Root.

  • You write if, then and else in lower case: IF is an error.

Examples

if BT-00-Number > 1000 then 'large' else 'small'
if BT-00-Text is present then BT-00-Text else 'none'
if BT-00-Indicator == TRUE then BT-00-StartDate else BT-00-EndDate

A date: BT-00-StartDate or BT-00-EndDate, depending on BT-00-Indicator.

if BT-00-Number > 100 then 'high' else if BT-00-Number > 10 then 'medium' else 'low'

A text with three possible values.

if ?is-large(BT-00-Number) then 'large' else 'small'

A condition given by a user-defined function. ?is-large returns an indicator. The template file declares it as LET indicator:?is-large(number:$n) = $n > 1000;.

concat(if BT-00-Number > 1 then 'several' else 'one', ' item')

A conditional expression that gives a text, used as an argument of a function.

(if BT-00-Indicator == TRUE then BT-00-StartDate else BT-00-EndDate) > 2024-01-01Z

A conditional expression in parentheses, used as an operand of a comparison.

count(if BT-00-Indicator == TRUE then BT-13-Text else BT-14-Text)

Each value is a sequence, so the conditional expression is a sequence, which count accepts.

The syntax has not changed. EFX-2 also accepts sequences as the two values.