Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

7.15 Conditional Expressions

Conditional Expressions

When performing actions based upon how a condition evaluates, there are numerous ways to build the condition. For example, when writing an If statement, you can compare a field to a constant using a variety of relational operators and you can join multiple conditions using Boolean operators. This section shows you all the ways you can evaluate fields and variables to see whether they match a certain value or a range of values.

You can test against a single constant or variable using the standard relational operators: <, >, <=, >=, =, <>.

You can compare multiple expressions using the Boolean operators And, Or, Not. The And operator takes two expressions and states that both expressions must be true for the condition to return True. The Or operator takes two expressions and states that only one of the two expressions need to be true for it to return True. The Not operator only uses one expression and makes it the opposite. If the expression is true then it returns False. If the expression is false then it returns True.

The Not operator should always use parentheses around the conditional expression. This makes it clear to Crystal Reports what expression should be negated. This applies even if you are only negating a single expression. For example, the next two lines of code have completely different results.

//Reverse the result of the two comparisons
Not (A>B And C>D)
//Reverse the result of only the first comparison
Not (A>B) And C>D

A few operators that might be new to you are Xor, Eqv and Imp. Eqv is for logical equivalence. It determines when the two expressions are the same. It returns True when both are true or both are false. If they are not the same, it returns False. The syntax is as follows:

exp1 Eqv exp2

Xor is for logical exclusion. It determines when the two expressions are different. It returns True if one is true and the other false. If both expressions are the same (either both are true or both are false), then it returns False. The syntax is as follows:

exp1 Xor exp2

Imp is for logical implication. If the first expression is true, it implies that the second expression will also be true. If the second expression is also true, Imp returns True. If the second expression is false, Imp returns False because it didn’t meet what was implied. On the other hand, if the first expression is false, nothing is implied and the result will be always be True. Thus, there is only one instance where Imp ever returns False: when the first expression is true and the second expression is false. All other circumstances return True. The syntax is as follows:

exp1 Imp exp2

The Is operator is used with the Select statement when you want to use the relational operators (e.g. >, <, etc.). An example demonstrating this was already shown in the discussion on the Select statement.

The In operator is used for testing if a field or variable exists as an element in an array or if it falls within a range of values. For more information, see the previous sections Array Data Types and Range Data Types.