If/Else control

Use 'if'statements to apply actions selectively. Use 'else' statements to perform a different set of actions when the expression is false.

The if statement enables rule actions to selectively execute other statements based on specific criteria.

For example, suppose that your rule prints debugging information based on the value of a Boolean variable named DEBUG. If DEBUG is true, your rule prints debugging information, such as the value of a variable x. Otherwise, your rule proceeds normally. A segment of code to implement the action might look like this:

then {
  if (?DEBUG) {
      System.out.println("DEBUG: x = " + ?x);
  }
}

This is the simplest version of the if statement: the block governed by the if is executed if a condition is true. Generally, the simple form of if can be written like this:

then {
  if (test) {
      statements
  }
}

If you want to perform a different set of statements if the expression is false, use the else statement. For example, suppose your rule needs to perform different actions depending on whether the user clicks the OK button or another button in an alert window. Your rule could do this by using an if and an else statement:

then {
  int ?i = 1;
  if ( ?i == 1 ) {
      System.out.println( " i = 1 " ) ;
    }
    else {
      System.out.println( " i <> 1 " ) ;
    }
}

The else block is executed only if the if part is false.