1. Home
  2. Computing & Technology
  3. Visual Basic

Chapter 6 - Using Decision Structures - Part 2

By Dan Mabbutt, About.com

2 of 3

The Select Case Structure

If-Then-Else syntax

If-Then-Else syntax

Whenever you have a series of decisions like this, a Select Case structure ought to be considered. Here's the same code using a Select Case Structure:

~~~~~~~~~~~~~~~~~~~~~~~~~
Select Case AdjustedIncome
  Case Is <= 27050 ' 15% tax bracket
    TaxDue = AdjustedIncome * 0.15
  Case Is <= 65550 ' 28% tax bracket
    TaxDue = 4057.5 + ((AdjustedIncome - 27050) * 0.28)
  Case Is <= 136750 ' 31% tax bracket
    TaxDue = 4057.5 + ((AdjustedIncome - 65550) * 0.31)
  Case Is <= 297350 ' 36% tax bracket
    TaxDue = 4057.5 + ((AdjustedIncome - 136750) * 0.36)
  Case Else ' 39.6% tax bracket
    TaxDue = 4057.5 + ((AdjustedIncome - 297350) * 0.396)
End Select
~~~~~~~~~~~~~~~~~~~~~~~~~

If you initialize AdjustedIncome to say, $1,000,000 (that is, a small, insignificant fraction of what Bill Gates makes in a year), you will find that you get exactly the same answer with either code. Use MsgBox or Debug.Write to see the answer. (If you use Debug.Write, make sure the Output window is displayed.

So ...

Assignment: Which One Should You Use?

I'll give you MY answer in the next lesson! But remember, your answer might be just as good as mine. Leave a message in the About Visual Basic Forum and tell us what YOUR answer is.

Microsoft has a complete explanation of how to read their syntax diagrams at their MSDN page.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Using Decision Structures - Part 2

©2009 About.com, a part of The New York Times Company.

All rights reserved.