### If statements

The If statement is one of the most useful control structures. It enables you to evaluate a sequence of statements if a condition is true and evaluate a different sequence of statements if it is not true.

##### 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 statement would accomplish this:

    Rem Multi-line If example 1
    If {Employee.Dept} = "Sales" Then
       formula = {Employee.Salary} * 0.06
    Else
       formula = {Employee.Salary} * 0.04
    End If

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

`formula = {Employee.Salary} * 0.06`

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

`formula = {Employee.Salary} * 0.04`

is processed.

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

    Rem Multi-line If example 2
    formula = {Employee.Salary} * 0.04
    If formula < 1000 Then
       formula = 1000
    End If

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

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

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

    Rem Multi-line If example 3
    formula = {Employee.Salary} * 0.04
    If formula < 1000 Then
       formula = 1000
    ElseIf formula > 5000 Then
       formula = 5000
    End If

##### 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 and $20,000 is taxed at 20%, income between $20,000 and $35,000 is taxed at 29%, and income above $35,000 is taxed at 40%.

    Rem Multi-line If example 4
    Dim tax As Currency, income As Currency
    income = {Employee.Salary}

Dim message As String

    If income < 8000 Then
        tax = 0
        message = "no"
    ElseIf income >= 8000 And income < 20000 Then
        message = "lowest"
        tax = (income - 8000)*0.20
    ElseIf 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
    End If
    Dim taxStr As String
    Rem use 2 decimal places
    Rem and use the comma as a thousands separator
    taxStr = CStr (tax, 2, ",")
    formula = "You are in the " & message & _
              " tax bracket. " & _
              "Your estimated tax is " & taxStr & "."

Notice, the use of variables to simplify the logic of the computation. Also, notice that there are two statements 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 statements executed as a result of a condition.

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