Chapter 6 Using Decision Structures 163
Dim AdjustedIncome, TaxDue As Double
AdjustedIncome = 50000
If AdjustedIncome <= 8375 Then '10% tax bracket
TaxDue = AdjustedIncome * 0.1
ElseIf AdjustedIncome <= 34000 Then '15% tax bracket
TaxDue = 837.5 + ((AdjustedIncome - 8375) * 0.15)
ElseIf AdjustedIncome <= 82400 Then '25% tax bracket
TaxDue = 4681.25 + ((AdjustedIncome - 34000) * 0.25)
ElseIf AdjustedIncome <= 171850 Then '28% tax bracket
TaxDue = 16781.25 + ((AdjustedIncome - 82400) * 0.28)
ElseIf AdjustedIncome <= 373650 Then '33% tax bracket
TaxDue = 41827.25 + ((AdjustedIncome - 171850) * 0.33)
Else '35% tax bracket
TaxDue = 108421.25 + ((AdjustedIncome - 373650) * 0.35)
End If
Important The order of the conditional expressions in your If... Then and ElseIf statements
is critical. What happens if you reverse the order of the conditional expressions in the tax
computation example and list the rates in the structure from highest to lowest? Taxpayers in the
10 percent, 15 percent, 25 percent, 28 percent, and 33 percent tax brackets are all placed in the
35 percent tax bracket because they all have an income that’s less than or equal to $373,650.
(This occurs because Visual Basic stops at the first conditional expression that is True, even if
others are also True .) All the conditional expressions in this example test the same variable, so
they need to be listed in ascending order to get the taxpayers to be placed in the right groups.
Moral: When you use more than one conditional expression, consider the order carefully.
This useful decision structure tests the double-precision variable AdjustedIncome at the first
income level and subsequent income levels until one of the conditional expressions evaluates
to True, and then determines the taxpayer’s income tax accordingly. With some simple
modifications, it could be used to compute the tax owed by any taxpayer in a progressive tax
system, such as the one in the United States. Provided that the tax rates are complete and up
to date and that the value in the AdjustedIncome variable is correct, the program as written
will give the correct tax owed for single U .S. taxpayers for 2010. If the tax rates change, it’s
a simple matter to update the conditional expressions. With an additional decision structure
to determine taxpayers’ filing status, the program readily extends itself to include all
U .S. taxpayers.
Tip Expressions that can be evaluated as True or False are also known as Boolean expressions,
and the True or False result can be assigned to a Boolean variable or property. You can assign
Boolean values to certain object properties or Boolean variables that have been created by using
the Dim statement and the As Boolean keywords.
In the next exercise, you’ll use an If... Then decision structure that recognizes users as they
enter a program—a simple way to get started with writing your own decision structures.
You’ll also learn how to use the MaskedTextBox control to receive input from the user in
a specific format.