Often when we are writing a program, we need to make a choice between two or more actions. We can illustrate this idea using lightbulbs and switches. A typical light switch has two states (on or off) which control the state of the lightbulb (also on or off). We can describe the logic of the light switch in the following way:

If the switch is on, then the lightbulb is on; else the switch is off and the light bulb is also off.

Notice the keywords If, Then, and Else in the sentence above. We often use these words to express a selection statement with two options. The general form of the statement is as follows:

If (<Boolean expression>) Then
    
<action 1>
Else
    <
action 2>
EndIf

Remember that a Boolean variable is one that is either true or false. Similarly, a Boolean expression is one that is either true or false. We can fill in our selection form using the lightbulb example:

If (switch is on) Then
    
lightbulb is on
Else
    
lightbulb is off
EndIf

Whenever we have more than two options, we can add branches to our selection statement using the keywords Else If. The box below shows an example form for three alternatives:

If (<Boolean expression>) Then
    <action 1>
Else If (<Boolean expression>) Then
    <action 2>
Else
    <action 3>
EndIf

The Selection Tutorial will give you some practice at writing selection statements on your own. Click on the link below to start the Selection Tutorial in a new window. Once you finish the tutorial, continue reading the remainder of this lesson.

Now let's consider how we can make our simple tax program more sophisticated using the selection control structure. Suppose that we added two variables to our program: state and taxRate. The state variable represents one of two states: Virginia or Tennessee. The taxRate variable represents a state's sales tax rate. In this case, the tax rate is either 4.5% for Virginia or 6.0% for Tennessee. This variable taxRate replaces our previous constant TAXRATE. Using these variables and our selection control structure, we can have our program chose the appropriate tax rate and calculate the correct sales tax for either state.

Modified Tax Program

Integer VIRGINIA := 1
Integer TENNESSE := 2

If (state = VIRGINIA) Then
   taxRate
:= 0.045
Else If (state = TENNESSE) Then
   taxRate := 0.06
Else
   <ERROR!>
EndIf

SalesTax 
:= Price taxRate
Total := Price SalesTax

Our variable state is testing to see what is the current state, and then the appropriate sales tax is selected. Of course, we could extend our program to include the tax rates from other states by adding more constants and Else If branches to the program. For example, to add the state of Maine, we would make the following changes:

Integer VIRGINIA := 1
Integer TENNESSE := 2
Integer MAINE := 3

If (state = VIRGINIA) Then
   taxRate
:= 0.045
Else If (state = TENNESSE) Then
   taxRate := 0.06
Else If (state = MAINE) Then
   taxRate := 0.07
Else
   <ERROR!>
EndIf

SalesTax 
:= Price taxRate
Total := Price SalesTax