The If Statement’s Else Branch

Whereas If executes code based on the comparison test’s true condition, the Else statement executes code based on the comparison test’s false condition. Else is an optional part of the If statement. Else specifies the code that executes if the comparison test is false. The complete format of the If statement with Else is as follows:

If comparisonTest Then

One or more Visual Basic statements

Else

One or more Visual Basic statements

End If

Typically, programmers call this full-blown If statement the If...Else statement. The If...Else statement is sometimes called a mutually exclusive statement. The term mutually exclusive simply means that one set of code or the other executes, but not both. The If...Else statement contains two sets of code[md]that is, two bodies of one or more Visual Basic statements - and only one set executes, depending on the result of the If. An If statement is either true or false because the If’s comparison produces either a true or false result. Therefore, either the first or the second body of code in an If...Else executes.

Suppose that a salesperson receives a bonus if sales are high (more than $5,000.00) or suffers a pay cut if sales are low (less than $5,000.00). The If...Else shown next contains the code necessary to reward or punish the salesperson. The If code body computes the bonus, as you saw in the previous section. The code body of the Else subtracts $25 from the salesperson’s pay, which is stored in the curPayAmt variable, if the sales quota isn’t met. The following code computes such a payment amount based on the quota:

1: If (txtSales.Text > 5000.00) Then

2: sngBonus = .05 * txtSales.Text

3: Else

4: curPayAmt = curPayAmt - 25.00

5: End If

6: curTaxes = curPayAmt * .42

The fourth line of code might surprise you at first. The assignment appears to make the statement that the pay is equal to the pay minus 25. You know that nothing can be equal to itself minus 25. In math, the equal sign acts as a balance for the two sides of the equation. In Visual Basic, when the equal sign isn’t used inside an If’s comparison test, it is an assignment that takes everything to the right of the equal sign and stores that value in the variable to the left of the equal sign. Therefore, the fourth line subtracts the 25 from the value stored in curPayAmt and assigns that result back to curPayAmt. In effect, it lowers the value of curPayAmt by 25.

note

When a variable appears on both sides of an assignment’s equal sign, the variable is being updated in some way.

To further your understanding of the If...Else statement and to demonstrate testing for an input box’s return value, study how Listing 7.1 uses If...Else to respond to an input box. The code asks the user for a company name and then accepts the name or recognizes that the user clicked Cancel to close the input box without answering it. (When a user clicks Cancel in response to an input box, the input box returns a null string, “”.)

Listing 7.1. Checking an input box’s return value.

1: Dim strCompName As String

2: Dim intPress As Integer ‘ MsgBox return value

3: ‘ Ask the user for a name

4: ‘ Use XYZ, Inc. for the default name

5: strCompName = InputBox(“What is the company name?”, _

6: “Company Request”, “XYZ, Inc.”)

7: ‘ Check the return value

8: If (strCompName = ““) Then

9: ‘ The user clicked Cancel

10: intPress = MsgBox(“Thanks anyway”)

11: Else

12: ‘ The user entered a company name

13: intPress = MsgBox(“You entered “ & strCompName)

14: End If

Top Home