else

This construct introduces actions that are executed if the conditions of a rule are not satisfied.

Purpose

You use this construct to declare actions that are executed if the if part of a rule has not been satisfied. These actions are ignored if all local variables are not correctly initialized due to preconditions in the definitions part failing to be satisfied.

Syntax

else
    <action>;*

Description

The else part of a rule is optional and allows you to specify one or more actions to do if the conditions in the if part of the rule are not met.

The actions in the else part of a rule are executed in the order they appear. Start each action statement on a new line and end each action statement with a semi-colon (;).

If one or more variables with preconditions are defined in the definitions part of a rule, the rule will only be processed if these preconditions are satisfied and all local variables are correctly initialized. This means that if these preconditions are not satisfied, the rule is not be processed at all, and the actions in the else part of the rule are never executed, whether or not the conditions in the if part of the rule are met.

Adding preconditions in the definitions part of a rule rather than in the if part of a rule can provide a small performance improvement, because it potentially reduces the number of conditions to be checked. However, because the logic of the rule is not the same in each case, the method to use depends on the result you want to obtain.

Example

In the following basic example, a customer will get a 10% discount if the customer is in the Gold category. Otherwise, the customer gets a 5% discount.

definitions
   set ’valued customer’ to a customer
if
   the category of ’valued customer’ is Gold
then
   apply a 10% discount;
else
   apply a 5% discount;

In the following example, a second condition has been added in the if part of the rule. Now, a customer receives a 10% discount only if the customer is in the Gold category and is aged over 20. All other customers receive a 5% discount.

definitions
   set ’valued customer’ to a customer
if
   the category of ’valued customer’ is Gold and ’valued customer’ is older than 20
then
   apply a 10% discount;
else
   apply a 5% discount;

Now consider a third example. In the following rule, a minimum age requirement is added as a pre-condition in the definitions part of the rule. This means that a customer must first be over 20 years old to be considered a valued customer. Otherwise, the ‘valued customer’ variable is not initialized and the rest of the rule is not executed. With this rule, a customer under 20 receives no discount at all. A customer who is over 20 and in the Gold category receives a 10% discount. The else part of the rule is executed and a 5% discount applied only if the customer is over 20 but not in the Gold category.

definitions
   set ’valued customer’ to a customer
      where this customer is older than 20;
if
   the category of ’valued customer’ is Gold
then
   apply a 10% discount;
else
   apply a 5% discount;