WHEN Complex Expressions

The XFD WHEN directive supports complex expressions such as those containing multiple AND and OR operations and parentheses to group sub-expressions for proper order of evaluation. For example:

$XFD WHEN (var1 = "abc" or var1 = "def") and \
$XFD      (var2 > "000" or var3 > "000") and \
$XFD       var4 != "xyzzy"

Here is a more complete example:

       fd idx1-file.
       01 idx1-record1.
          03 filler pic x(46).

       01 idx1-record2.
          03 idx1-key.
             05 idx1-num1  pic 9(3).
             05 idx1-num2  pic 9(3).
             05 idx1-num3  pic 9(3).
          03 filler pic x(37).

      $xfd when ((idx1-num1 = "000" or idx1-num1 = "009") and  \
      $xfd       (idx1-num2 = "000" or idx1-num2 = "009"))     \
      $xfd   tablename=one
       01 idx1-record3.
          03 filler pic x(6).
          03 rec3-name1  pic x(20).
      $xfd when idx1-num3 = "333" or idx1-num3 = "444", tablename=two
          03 rec3-name2  pic x(20).

      $xfd when idx1-num1 = "001" AND idx1-num2 = "001"
       01 idx1-record4.
          03 filler pic x(6).
          03 rec4-name1  pic x(30).
          03 rec4-name2  pic x(10).

      $xfd when idx1-num1 > "001" OR idx1-num2 > "001"
       01 idx1-record5.
          03 filler pic x(6).
          03 rec5-text  pic x(40).

WHEN Expression Rules

  1. The AND operation takes precedence over the OR operation. If sc1, sc2, and sc3 are simple conditions (as defined above) then the expression sc1 or sc2 and sc3 is interpreted as sc1 or (sc2 and sc3) meaning that the AND of sc2 and sc3 is performed first and the result of that operation is OR'ed with sc1. If you want the OR operation performed first then you must specify (sc1 or sc2) and sc3 using parentheses.
  2. Since expressions are likely to use parentheses it is strongly recommended that the "$XFD" syntax be used rather than the "*(( XFD" with trailing "))" syntax.
  3. Mismatched parentheses cause an error to be reported.
  4. The maximum number of simple conditions in one expression is 50. An error is reported if the maximum is exceeded.
  5. Only one WHEN expression directive is allowed on consecutive lines. For example, the following three lines cause a syntax error to be reported:
    $XFD WHEN var1 = "abc" or var2 = "def"
    $XFD WHEN var3 = "xyz"
    01  record-type-2.

    If what you meant was:

    $XFD WHEN (var1 = "abc" or var2 = "def") \
    $XFD   and var3 = "xyz"
    01  record-type-2.

    then you must code it that way (note the use of parentheses and the backslash XFD directive line continuation character).

    "The optional condition tablename at the end of a WHEN directive applies to all simple conditions within the expression and to the overall combined expression. For example:

    $XFD WHEN (var1 = "abc" or var1 = "def") and \
    $XFD      (var2 = "123" or var2 = "456") \
    $XFD      tablename = table-2
    01  record-type-2.

    The condition tablename table-2 is applied to all four simple conditions and propagated to the two OR conditions and to the final AND condition of the expression.