The Selection Control Structure
The selection control structure allows one set of statements to be executed if a condition is true and another set of actions to be executed if a condition is false. A selection structure, also called an "If-Then-Else" structure, is flowcharted as follows:
After either the true set of actions or the false set of actions are taken, program control resumes with the next statement (the statement that would be placed below the connector in the flowchart above).
In VB, the following form is preferred for implementing the If-Then-Else structure (this is the "block", or "multi-line" form of the If statement):
If <conditional expression> Then
<one or more statements to be executed if condition is true>
Else
<one or more statements to be executed if condition is false>
End If
If the conditional expression is true, the statements between the keywords Then and Else will be executed (and the statements between the keywords Else and End If will be bypassed). If the conditional expression is false, the statements between the keywords Else and End If will be executed (and the statements between the keywords Then and Else will be bypassed). In any case, program control will resume with the statement following End If.
Example:
If sngNumberOfCredits < 12 Then
lblStatus.Text = "PART-TIME STUDENT"
dblTuitionAmount = sngNumberOfCredits * 175
Else
lblStatus.Text = "FULL-TIME STUDENT"
dblTuitionAmount = 2000
End If
STYLE TIPS FOR THE BLOCK IF STATEMENT:
(Note: If the "pretty listing" option of the IDE editor is turned on, as it is by default, then you need not be too concerned with these tips, as your code will automatically be indented and reformatted to conform to the "proper" style.)
(1) Indent the "true" actions 4 spaces from the keyword "If"
(2) Code the keyword "Else" on a separate line, aligned with the keyword "If"
(3) Code the "false" actions 4 spaces from the keyword "Else"
(4) Align the keywords "End If" with the keywords "If" and "Else"
The If-Then-Else statement is a "two-alternative" decision - actions are taken on both the "If" side and the "Else" side. Sometimes, however, you may only want to perform an action or set of actions if a condition is true, but do nothing special if the condition is false. This could be flowcharted as follows:
To implement this in VB, omit the "Else" portion of the block If structure. For example:
If dblTotalSales > 100000 Then
dblBonusAmount = 500
End If
If statements can also be written in a "single-line" format. The syntax is:
If <conditional expression> Then <true statement> [Else <false statement>]
Examples:
If sngTemperature <= 32 Then Console.WriteLine("It's freezing!")
If sngAvgGrade >= 60 Then Console.WriteLine("You passed!") Else Print Console.WriteLine("You failed!")
Note that in the single-line format of the If statement, the keywords "End If" are not used.
Style-wise, the block form of the If statement is preferred over the single-line form in that the block form enhances readability.
Although not used frequently, in the block form of the If statement, "null" or "empty" sets of actions are permitted (i.e., "if a condition is true, do nothing - else, do something"). For example:
If sngTemperature > 32 Then
Else
Console.WriteLine("It's freezing!")
End If
When using an empty set of actions, it improves readability to place a comment (remark) where the true statements would normally go:
If sngTemperature > 32 Then
' Do nothing
Else
Console.WriteLine("It's freezing!")
End If
Similarly, an empty set of actions could follow an Else:
If sngTemperature <= 32 Then
Console.WriteLine("It's freezing!")
Else
' Do nothing
End If
A Boolean variable, which can hold the value True or False, can be used anywhere a relational expression can be used.
Example:
If blnFoundIt Then . . .
can be coded instead of:
If blnFoundIt = True Then . . .
in other words, the "= True" part is implied.
Also, to test if a Boolean variable is false, you can use:
If Not blnFoundIt Then . . .
instead of:
If blnFoundIt = False Then . . .
For cases where you want to assign a particular variable one value if a condition true and another value if a condition is false, you can use the IIf function. The syntax is:
IIf(<conditional expression>, true part, false part)
The statement
strMessage = IIf(sngAvgGrade >= 60, "You passed!", "You failed!")
is equivalent to
If sngAvgGrade >= 60 Then
strMessage = "You passed!"
Else
strMessage = "You failed!"
End If
Note: The IIf function, while very convenient at times, has a reputation for being inefficient because it evaluates both the True part and the False part even if it does not need to (i.e. if the condition is true, there is no need to examine the false part). VB 2008 addressed this inefficiency with the introduction of a short-circuited If function.
The Case Structure
When the situation arises where you need to choose between more than two alternatives, an extended form of the selection structure, called the case structure, must be used. A flowcharted example follows:
In VB, the case structure can be implemented in one of two ways: with an extended block If structure, or with the Select Case statement structure.
Extended Block If Statement (If/Then/ElseIf)
Format:
If <conditional expression 1> Then
<one or more statements to be executed if condition 1 is true>
ElseIf <conditional expression 2> Then
<one or more statements to be executed if condition 2 is true>
. . .
ElseIf <conditional expression n> Then
<one or more statements to be executed if condition n is true>
Else
<one or more statements to be executed if none of the above conditions is true>
End If
Note that one or more ElseIf clauses are "sandwiched" between the first "If" clause and the last "Else" clause. Note also the keyword ElseIf is one word. Using the format above, this extended If structure is to be understood as follows: if "conditional expression 1" is true, perform the statements associated with that condition, then exit to the statement following the End If; if "conditional expression 1" is false, then check "conditional expression 2" - if "conditional expression 2" is true, perform the statements associated with that condition, then exit to the statement following the End If, and so on. VB will execute the statements associated with the first true conditional expression it finds and then exit to the statement following the End If. The final Else statement is often useful to trap errors that may occur when unexpected conditions arise, none of which matches the conditions in the previous If or ElseIf clauses.
Example:
If strShiftCode = "1" Then
sngShiftRate = sngHourlyRate
ElseIf strShiftCode = "2" Then
sngShiftRate = sngHourlyRate * 1.1
ElseIf strShiftCode = "3" Then
sngShiftRate = sngHourlyRate * 1.15
Else
Console.WriteLine("Shift code error")
End If
The example above also shows the recommended style for indenting. (If the "pretty listing" option of the IDE editor is turned on, as it is by default, then indenting will occur automatically.)
Note that the use of "ElseIf" saves the coding of multiple "End If" statements in a nested If structure. As a comparison, the above example could have been written as follows:
If strShiftCode = "1" Then
sngShiftRate = sngHourlyRate
Else
If strShiftCode = "2" Then
sngShiftRate = sngHourlyRate * 1.1
Else
If strShiftCode = "3" Then
sngShiftRate = sngHourlyRate * 1.15
Else
Console.WriteLine("Shift code error")
End If
End If
End If
The Select Case Statement
VB's Select Case is a powerful statement with several options. The format is:
Select Case <test expression>
Case <expression list 1>
<statement list 1>
Case <expression list 2>
<statement list 2>
…
Case Else
<statement list n>
End Select
The format above is to be understood as follows: The Select Case statement specifies an expression to be tested. Each subsequent Case clause specifies an expression(s) that the test expression will be compared to. The first Case clause that contains an expression that matches the test expression will have its associated actions executed, then program control will branch to the statement following End Select. The final Case Else clause is often useful to trap errors that may occur when an unexpected value of the test expression is present, none of which matches the expression list specified in any of the above Case clauses.
Example:
Select Case strShiftCode
Case "1"
sngShiftRate = sngHourlyRate
Case "2"
sngShiftRate = sngHourlyRate * 1.1
Case "3"
sngShiftRate = sngHourlyRate * 1.5
Case Else
Console.WriteLine("Shift Code Error")
End Select
The example above also shows the recommended style for indenting. (If the "pretty listing" option of the IDE editor is turned on, as it is by default, then indenting will occur automatically.)
The test expression following "Select Case" can be a more complex expression, such as:
Select Case intNumber + 1
Select Case Mid(strTest, 4, 2)
The expression list in a "Case" clause can have any of the following formats:
Format |
Examples |
<expression> [, expression, . . . ] |
Case 1, 10, 100 Case "Y", "y" |
<expression> To <expression> |
Case 1 To 9 Case "A" To "C" |
Is <relational operator expression> |
Case Is >= 21 |
(combination of any of the above) |
Case Is <= 5, 20 To 29, 43 |
The Select Case statement does not need to be "tied" to one particular variable or expression; it can be used to evaluate any number of conditions, using the following format:
Select Case True
Case <condition 1>
<statement list 1>
Case < condition 2>
<statement list 2>
…
Case Else
<statement list n>
End Select