Test conditions with ‘if-then-else’

The most common way to control which part of an expression should be active and which should be ignored is to use the if … then statement. This statement checks whether a certain condition is true. If it is, it tells the integration engine to activate a specific part of the expression that would be otherwise ignored.

The if…then statement looks as follows:

if \invoice\date > '2005-12-31'
and \invoice\date < '2007-01-01' then
{
    %invoices2006 = %invoices2006 + 1
}

When processing this expression, the integration engine evaluates the Boolean expression between the if and then reserved words. If the evaluation returns true the list of statements enclosed between the curly brackets { and } is evaluated, otherwise it is ignored. This is to test whether the date of the invoice is within year 2006. If so, one is added to the contents of the variable invoices2006; as a result, the number of invoices of 2006 is determined.

While the if…then statement is a way to activate some part of an expression only when a certain condition is true, the if … then … else statement tells the integration engine to evaluate one set of instructions when the condition is true and another set of instructions when the condition is false. Here is an example of this statement:

if \water\quantity > 1000 then
{
    (\water\quantity / 1000) & " m3"
}
else
{
    \water\quantity & " l"
}

This statement builds the string "32 m3" when the input quantity of water is 32,768 and "234 l" when the input quantity is 234. The list of statements that is enclosed between the curly brackets { and } can be any expression. Specifically, it can contain additional if…then…else statements. In computer terminology, it is said that if…then…else statements can be nested.

Related Links