The If Statement

Perhaps the most important statement in a program is the If statement and its cousin statements. With logic that If provides, your application can begin to analyze data and make decisions based on that analysis. For example, your program can display a three-button message box and determine, with the If statement, which command button the user clicked to close the message box.

If uses the comparison operators you learned earlier in this lesson to test data values. If performs one of two possible code actions, depending on the result of the comparison. In other words, If uses comparison operator results to test data. If might execute one or more lines of subsequent code, depending on the result of a comparison.

Before If, the code you wrote executed sequentially, one statement after another. If lets your program be more decisive and execute only parts of the program if the data warrants partial execution. For example, suppose you were writing an invoicing system. In such a system, no sales tax should be computed for tax-exempt organizations, so your program would skip over the tax computation code when processing such organizations.

If makes decisions. If a comparison test is true, the body of the If statement executes. (In fact, the previous sentence is almost identical to Visual Basic’s If statement!) Here is one format of If:

If comparisonTest Then

One or more Visual Basic statements

End If

End If tells Visual Basic where the body of the If statement ends. Suppose that the user enters a sales figure into a Text Box control named txtSales. The following If computes a bonus amount based on the sales:

If (txtSales.Text > 5000.00) Then

sngBonus = txtSales.Text * .12

End If

note

Data enters a control such as a text box as a Variant datatype. When you perform arithmetic with a Variant and the Variant datatype holds a numeric value, Visual Basic converts the Variant to a number for the calculation.

Remember that Visual Basic stores 0 in all numeric variables that you don’t first initialize. Therefore sngBonus has a 0 before the If executes. When If executes, the code changes the sngBonus variable only if the value of the txtSales.Text property is more than 5000.00. In a way, the If reads like this:

If the sales are more than $5,000.00, then compute a bonus based on that sales value.

Visual Basic stores a null zero in string variables that you have not yet initialized. If you use an uninitialized Variant datatype variable, the variable holds a null value that becomes zero if you assign the variable to a numeric variable.

The body of an If can have more than one statement. The body is often known as a block. The following If calculates a bonus, the cost of sales, and a reorder amount based on the value of the txtSales text box entry:

If (txtSales.Text > 5000.00) Then

sngBonus = txtSales.Text * .12

curCostOfSales = txtSales.Text * .41

curReorderCost = txtSales.Text * .24

End If

The three statements that make up the body of the If execute only if the condition txtSales.Text > 5000.00 is true. Suppose that this code contains another assignment statement immediately after End If. That assignment statement is outside the body of the If, so the true or false result of the condition affects only the body of the If. Therefore, the tax computation in the following routine executes regardless of whether the sales are more or less than $5,000.00:

If (txtSales.Text > 5000.00) Then

sngBonus = txtSales.Text * .12

curCostOfSales = txtSales.Text * .41

curReorderCost = txtSales.Text * .24

End If

sngTax = .12 * txtSales.Text

tip

The parentheses are not required around the comparison test in an If, but they help separate the test from the rest of the code. In addition, the indentation helps illustrate the code that appears inside the If statement’s body.

Can you see how the program makes decisions using If? The body of the If executes only if the comparison test is true. Otherwise, the rest of the program continues as usual.

There is a shortcut form of If that you might run across. The single-line If statement has a format that looks like this:

If comparisonTest Then VBStatement

The single-line If doesn’t require an End If statement because the comparison test and the body of the If reside on the same line. Single-line If statements don’t provide for easy program maintenance. If you decide that you want to add to the body of the If, you must convert the single-line If to a multiple-line If, and you might forget to then add End If. Therefore, even if the body of an If statement takes only one line, code the If as a multiple-line If...End If statement to make the program more maintainable.

Top Home