Decision Structures

See Also

Visual Basic procedures can test conditions and then, depending on the results of that test, perform different operations. The decision structures that Visual Basic supports include:

If...Then

Use an If...Then structure to execute one or more statements conditionally. You can use either a single-line syntax or a multiple-line block syntax:

If condition Then statement

If condition Then
statements

End If

The condition is usually a comparison, but it can be any expression that evaluates to a numeric value. Visual Basic interprets this value as True or False; a zero numeric value is False, and any nonzero numeric value is considered True. If condition is True, Visual Basic executes all the statements following the Then keyword. You can use either single-line or multiple-line syntax to execute just one statement conditionally (these two examples are equivalent):

If anyDate < Now Then anyDate = Now

If anyDate < Now Then
   anyDate = Now
End If

Notice that the single-line form of If...Then does not use an End If statement. If you want to execute more than one line of code when condition is True, you must use the multiple-line block If...Then...End If syntax.

If anyDate < Now Then
   anyDate = Now
   Timer1.Enabled = False      ' Disable timer control.
End If

If...Then...Else

Use an If...Then...Else block to define several blocks of statements, one of which will execute:

If condition1 Then
[statementblock-1]
[ElseIf condition2 Then
[statementblock-2]] ...
[Else
[statementblock-n]]

End If

Visual Basic first tests condition1. If it's False, Visual Basic proceeds to test condition2, and so on, until it finds a True condition. When it finds a True condition, Visual Basic executes the corresponding statement block and then executes the code following the End If. As an option, you can include an Else statement block, which Visual Basic executes if none of the conditions are True.

If...Then…ElseIf is really just a special case of If...Then...Else. Notice that you can have any number of ElseIf clauses, or none at all. You can include an Else clause regardless of whether you have ElseIf clauses.

For example, your application could perform different actions depending on which control in a menu control array was clicked:

Private Sub mnuCut_Click (Index As Integer)
   If Index = 0 Then            ' Cut command.
      CopyActiveControl         ' Call general procedures.
      ClearActiveControl
   ElseIf Index = 1 Then      ' Copy command.
      CopyActiveControl
   ElseIf Index = 2 Then      ' Clear command.
      ClearActiveControl
   Else                        ' Paste command.
      PasteActiveControl
   End If
End Sub

Notice that you can always add more ElseIf parts to your If...Then structure. However, this syntax can get tedious to write when each ElseIf compares the same expression to a different value. For this situation, you can use a Select Case decision structure.

For More Information   See "If...Then...Else Statement" in the Language Reference.

Select Case

Visual Basic provides the Select Case structure as an alternative to If...Then...Else for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more readable when there are several choices.

A Select Case structure works with a single test expression that is evaluated once, at the top of the structure. Visual Basic then compares the result of this expression with the values for each Case in the structure. If there is a match, it executes the block of statements associated with that Case:

Select Case testexpression
[Case expressionlist1
[statementblock-1]]
[Case expressionlist2
[statementblock-2]]
.
.
.
[Case Else
[statementblock-n]]

End Select

Each expressionlist is a list of one or more values. If there is more than one value in a single list, the values are separated by commas. Each statementblock contains zero or more statements. If more than one Case matches the test expression, only the statement block associated with the first matching Case will execute. Visual Basic executes statements in the Case Else clause (which is optional) if none of the values in the expression lists matches the test expression.

For example, suppose you added another command to the Edit menu in the If...Then...Else example. You could add another ElseIf clause, or you could write the function with Select Case:

Private Sub mnuCut_Click (Index As Integer)
   Select Case Index
      Case 0                  ' Cut command.
         CopyActiveControl      ' Call general procedures.
         ClearActiveControl
      Case 1                  ' Copy command.
         CopyActiveControl
      Case 2                  ' Clear command.
         ClearActiveControl
      Case 3                  ' Paste command.
         PasteActiveControl
      Case Else
         frmFind.Show         ' Show Find dialog box.
   End Select
End Sub

Notice that the Select Case structure evaluates an expression once at the top of the structure. In contrast, the If...Then...Else structure can evaluate a different expression for each ElseIf statement. You can replace an If...Then...Else structure with a Select Case structure only if the If statement and each ElseIf statement evaluates the same expression.