Reference > Rule Languages > Business Action Language > BAL Constructs > else

Summary

Introduces the part of the rule in which actions are executed if the if part of a rule has not been satisfied.

Syntax

else
    <action>;*

Description

The else part of a rule is optional and allows you to specify one or more actions to perform 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 correctly initialized. This means that if these preconditions are not satisfied, the rule will not be processed at all, and the actions in the else part of the rule will never be 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, since it potentially reduces the number of conditions to be checked. However, since 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 they are in the Gold category. Otherwise, the customer will get 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 will only receive a 10% discount if they are in the Gold category and aged over 20. All other customers will 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 has been 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 `valuable 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 will receive no discount at all. A customer who is over 20 and in the Gold category will receive a 10% discount. The else part of the rule will only be executed and a 5% discount applied 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;

Related Concepts

Else Part

Related Tasks

Creating Business Rules

Related Reference

then
for each <object> in <list>
set <variable> to <value>

Related Samples and Tutorials

Tutorial: Creating Business Rules