DECISION STATEMENTS
A decision or conditional statement represents a branch in the program. It marks a place
where the program can execute one set of statements or another, or possibly no statements at all, depending on some condition. These include several kinds of If statements, Choose
statements, and Select Case statements.
Single-Line If Then
The single-line If Then statement has two basic forms. The f rst allows the program to execute a
single statement if some condition is True. The syntax is as follows:
If condition Then statement
If the condition is True, the program executes the statement. In the most common form of singleline If Then statements, the statement is a single simple command (such as assigning a value to a
variable or calling a subroutine).
The single-line If Then statement can also include Else If clauses. For example, the following code
examines the variable X. If X is 1, the program sets variable txt to “One.” If X has the value 2, the
program sets txt to “Two.” If X is not 1 or 2, the program sets txt to a question mark.
Dim txt As String
If X = 1 Then txt = “One” Else If X = 2 Then txt = “Two” Else txt = “?”
The code can include as many Else If clauses as we like. However, confusing code such as the
preceding example can lead to puzzling bugs that are easy to avoid if we use multiline If Then
statements instead.
Multiline If Then
A multiline If Then statement can execute more than one line of code when a condition is True.
The syntax for the simplest form of the multiline If Then statement is as follows:
If condition Then
statements ...
End If
If the condition is True, the program executes all the commands that come before the End If
statement.
Like the single-line If Then statement, the multiline version can include Else If and Else clauses.
For possibly historical reasons, ElseIf is spelled as a single word in the multiline If Then statement.
The syntax is as follows:
If condition1 Then
statements1 ...
ElseIf condition2
statements2 ...
Else
statements3 ...
End If
If the f rst condition is True, the program executes the f rst set of statements. If the f rst condition is
False, the code examines the second condition and, if that one is True, the code executes the second
set of statements. The program continues checking conditions until it f nds one that is True and it
executes the corresponding code.
If the program reaches an Else statement, it executes the corresponding code. If the program reaches the End If statement without f nding a True condition or an Else clause, it doesn’t execute any of the statement blocks.
Select Case
The Select Case statement lets a program execute one of several pieces of code depending on a single
value. The basic syntax is as follows:
Select Case test_value
Case comparison_expression1
statements1
Case comparison_expression2
statements2
Case comparison_expression3
statements3
...
Case Else
else_statements
End Select
If test_value matches comparison_expression1, the program executes the statements in
the block statements1. If test_value matches comparison_expression2, the program executes the statements in the block statements2. The program continues checking the expressions in the
Case statements in order until it matches one, or it runs out of Case statements.
If test_value doesn’t match any of the expressions in the Case statements, the program executes
the code in the else_statements block. Note that you can omit the Case Else section. In that
case, the program executes no code if test_value doesn’t match any of the expressions.
Select Case is functionally equivalent to an If Then Else statement. The following code does the
same thing as the previous Select Case code:
If test_value = comparison_expression1 Then
statements1
ElseIf test_value = comparison_expression2 Then
statements2
ElseIf test_value = comparison_expression3 Then
statements3
...
Else
else_statements
End If
Select Case is sometimes easier to understand than a long If Then Else statement. It is often faster as well, largely because Select Case doesn’t need to reevaluate test_value for every Case statement. If test_value is a simple variable, the difference is insignif cant, but if test_value represents a slow function call, the difference can be important. For example, suppose test_value represents a function that opens a database and looks up a value. The Select Case version will f nd the value once and use it in each comparison, whereas the If Then version would reopen the database for each comparison.