# How to use If Expressions using Crystal Reports

The If expression is one of the most useful control structures. It allows you to evaluate an expression if a condition is true and evaluate a different expression otherwise.

Note   When formatting with conditional formulas, always include the Else keyword; otherwise, values that don’t meet the If condition may not retain their original format. To prevent this, use the DefaultAttribute function (If…Else DefaultAttribute).

Example

A company plans to pay a bonus of 4 percent to its employees except for those who work in Sales who will receive 6 percent. The following formula using an If expression would accomplish this:

```//If example 1
If {Employee.Dept} = "Sales" Then
{Employee.Salary} * 0.06
Else
{Employee.Salary} * 0.04```

In this example, if the condition {Employee.Dept} = “Sales” evaluates as true, then the

`{Employee.Salary} * 0.06`

expression is processed. Otherwise the expression following the Else, namely the

`{Employee.Salary} * 0.04`

is processed.

Suppose another company wants to give employees a 4% bonus, but with a minimum bonus of \$1,000. Notice that the Else clause is not included; it is optional, and not needed in this case.

```//If example 2
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
bonus := 1000;
//The final expression is just the variable 'bonus'.
//This returns the value of the variable and is the
//result of the formula
bonus```

Another way of accomplishing example 2 is to use an Else clause:

```//If example 3
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
1000
Else
bonus```

Now suppose that the previous company also wants a maximum bonus of \$5,000. You now need to use an Else If clause. The following example has only one Else If clause, but you can add as many as you need.

Note   There is a maximum of one Else clause per If expression.

The Else clause is executed if none of the If or Else If conditions are true.

```//If example 4
Local CurrencyVar bonus := {Employee.Salary} * 0.04;
If bonus < 1000 Then
1000
Else If bonus > 5000 Then
5000
Else
bonus;```

#### Example

Suppose that a company wants to compute an estimate of the amount of tax an employee needs to pay and write a suitable message. Income below \$8,000 is not taxed, income between \$8,000 to \$20,000 is taxed at 20%, income between \$20,000 to \$35,000 is taxed at 29%, and income above \$35,000 is taxed at 40%.

```//If example 5
Local CurrencyVar tax := 0;
Local CurrencyVar income := {Employee.Salary};
Local StringVar message := "";
If income < 8000 Then
(
message := "no";
tax := 0
)
Else If income >= 8000 And income < 20000 Then
(
message := "lowest";
tax := (income - 8000)*0.20
)
Else If income >= 20000 And income < 35000 Then
(
message := "middle";
tax := (20000 - 8000)*0.20 + (income - 20000)*0.29
)
Else
(
message := "highest";
tax := (20000 - 8000)*0.20 + (35000 - 20000)*0.29 +
(income - 35000)*0.40
);
//Use 2 decimal places and the comma as a
//thousands separator
Local StringVar taxStr := CStr (tax, 2, ",");
"You are in the " & message & " tax bracket. " &
"Your estimated tax is " & taxStr & "."```

Note   The use of variables is to simplify the logic of the computation. Also, there are two expressions that are executed when one of the conditions are met; one assigns the tax variable, and the other assigns the message variable. It is often useful to have multiple expressions executed as a result of a condition.

Credit: http://msdn.microsoft.com/en-us/library/ms225356%28v=vs.80%29.aspx