#### More details on If expressions

The If expression is an expression. In other words it evaluates to a value of a given type. If there is no Else clause, and the condition is not true, then the value is the default value for the type. For example:

    If Length ({Employee.First Name}) < 5 Then
       "short"

The above If expression returns a String value. The string value is "short" if the Employee's first name has fewer than 5 letters and the empty String "" otherwise.

Consider the formula:

    If Year({Orders.Order Date}) >= 1995 Then
       {Orders.Order Date}

For order dates before 1995, the above If expression returns the null DateTime value. It is a DateTime value rather than a Date value since {Orders.Order Date} is a DateTime database field. The null DateTime value is not printed by Seagate Crystal Reports so if the above formula is placed in a report, the formula field would be blank for order dates before 1995. Null Time values and null Date values behave similarly.

Here is an example that illustrates the use of parentheses to have more than one expression executed as the outcome of an If condition. A company charges a 5 percent fee for orders shipped within 3 days and a 2 percent fee otherwise. It wants to print messages such as "Rush shipping is $100.00" or "Regular shipping is $20.00" as appropriate.

    Local StringVar message;
    Local CurrencyVar ship;
    If {Orders.Ship Date} - {Orders.Order Date} <= 3 Then
    (
       message := "Rush";
       //A semicolon at the end of the next line
       //is optional
       ship := {Orders.Order Amount} * 0.05
    ) //A semicolon cannot be placed here
    Else
    (
       message := "Regular";
       ship := {Orders.Order Amount} * 0.02;
    );
    //The preceding semicolon is required to separate the
    //If expression from the final expression below
    message & " shipping is " & CStr (ship)

When expressions are grouped together with parentheses, the whole group is considered as a single expression, and its value and type are the value and type of the final expression inside the parentheses.

    //The parentheses group expression as a whole has
    //Currency type
    (
       //The first expression in the parentheses has
       //String type
       message := "Rush";
       //The second and final expression in parentheses
       //has Currency type
       ship := {Orders.Order Amount} * 0.05;
    )

Thus, for example, the following formula gives an error. The reason is that the Then part of the If expression returns a Currency value while the Else part returns a String value. This is not allowed, since the If expression is an expression and so must always return a value of a single type.

    //An erroneous formula
    Local StringVar message;
    Local CurrencyVar ship;
    If {Orders.Ship Date} - {Orders.Order Date} <= 3 Then
    (
       message := "Rush";
       ship := {Orders.Order Amount} * 0.05
    )
    Else
    (
       //The following 2 lines were interchanged
       ship := {Orders.Order Amount} * 0.02;
       message := "Regular";
    );
    message & " shipping is " & CStr (ship)

One way of fixing up the erroneous formula without being careful about expression order is just to make the If expression return a constant value of the same type in every branch. For example, the If expression now returns the Number value 0:

    //Repaired the erroneous formula
    Local StringVar message;
    Local CurrencyVar ship;
    If {Orders.Ship Date} - {Orders.Order Date} <= 3 Then
    (
       message := "Rush";
       ship := {Orders.Order Amount} * 0.05;
       0
    )
    Else
    (
       ship := {Orders.Order Amount} * 0.02;
       message := "Regular";
       0
    );
    message & " shipping is " & CStr (ship)

Seagate Software, Inc. http://www.seagatesoftware.com Please send comments to: techpubs@seagatesoftware.com