Selection        

 

 

Versions

Selection is implemented in one of three ways:

· If/Then structure (single selection)

· If/Then/Else structure (double selection)

· Select Case structure (multiple selection)

 

  • The If/Then selection structure either performs (selects) an action if a condition is True or skips the action if the condition is False.

  • The If/Then/Else selection structure performs an action if a condition is True and performs a different action if the condition is False.

  • The Select Case selection structure is a multiple-selection structure.

The If/Then structure is called a single-selection structure because it selects or ignores a single action.

The If/Then/Else structure is called a double-selection structure because it selects between two different actions.


If/Then Selection Structure

A selection structure is used to choose among alternative courses of action.

Example: The pseudocode statement

If student's grade is greater than or equal to 60
      Display "Passed"

determines if the condition "student's grade is greater than or equal to 60" is True or False. If the condition is True, then "Passed" is displayed, and the subsequent statement is performed. If the condition is False, the display statement is ignored, and the next pseudocode statement in order is performed.

The preceding pseudocode If/Then statement may be written in Visual Basic as

                    If grade >= 60 Then lblStatus.Text = "Passed"

            OR

If grade >= 60 Then
      lblStatus.Text = "Passed"
End If

The If/Then selection structure, when written on a single line, normally expects only one statement in its body. To include several statements in the body of an If/Then, write the statements between the If/Then and End If keywords. A control structure's multiple statement body is often called a blockMultiple-line If/Then or If/Then/Else statements must terminate with End If.

Example:

If grade >= 60 Then
      lblStatus.Text = "Passed"
      numPassed = numPassed + 1
End If

The figure below illustrates the single-selection If/Then structure.


If/Then/Else Selection Structure

The If/Then/Else selection structure allows the programmer to specify that a different action is to be performed when the condition is True than when the condition is False.

Example: The pseudocode statement

If student's grade is greater than or equal to 60
      Display "Passed"
Else
      Display "Failed"

displays "Passed" if the student's grade is greater than or equal to 60 and displays "Failed" if the student's grade is less than 60. In either case, after the message is displayed, control passes to the statement following the If/Then/Else.

Note that the body statements of selection structures are indented.

 

The preceding pseudocode If/Then/Else structure may be written in VB as

If grade >= 60 Then
      lblStatus.Text = "Passed"
Else
      lblStatus.Text = "Failed"
End If

Note that each If/Then or If/Then/Else spanning multiple lines must end in End If unless the line-continuation character _ is used. The line continuation character must be preceded by a blank.

The last End If always matches up with the previous If/Then or If/Then/Else.

The following figure shows the If/Then/Else selection structure.


Nested Ifs

Nested If/Then/Else structures test for multiple cases by placing If/Then/Else structures inside If/Then/Else structures.

Example:

If student's grade is greater than or equal to 90
      Display "A"
Else
      If student's grade is greater than or equal to 80
            Display "B"
     Else
            If student's grade is greater than or equal to 70
                  Display "C"
           Else
                  If student's grade is greater than or equal to 60
                        Display "D"
                  Else
                        Display "F"

 

If grade >= 90 Then
      lblStudentGrade.Text = "A"
Else
      If grade >= 80 Then
            lblStudentGrade.Text = "B"
      Else
            If grade >= 70 Then
                  lblStudentGrade.Text = "C"
            Else
                  If grade >= 60 Then
                        lblStudentGrade.Text = "D"
                  Else
                        lblStudentGrade.Text = "F"
                  End If
            End If
      End If
End If

 

If grade is greater than or equal to 90, the first four conditions will be True, but only the statement after the first test will be executed. After the assignment is executed, the Else-part of the "outer" If/Then/Else statement is skipped, in fact skipping the entire remainder of the preceding code segment.

The preceding If/Then/Else structure can also be written as follows:

If grade >= 90 Then
      lblStudentGrade.Text = "A"
ElseIf grade >= 80 Then
      lblStudentGrade.Text = "B"
ElseIf grade >= 70 Then
      lblStudentGrade.Text = "C"
ElseIf grade >= 60 Then
      lblStudentGrade.Text = "D"
Else
      lblStudentGrade.Text = "F"
End If

Both forms are equivalent.


For more applications of the Selection structure...

...see the Error Checking notes. validation

 


Select Case Multiple-Selection Structure

 

The syntax of the Select Case is as follows:

Select Case variable
     Case expression
          [statements]
     [Case Else]
          [statements]
End Select 

  • The variable specified after Select Case will be compared sequentially with each Case until either a match occurs or the End Select statement is executed.

  • Execution of a particular Case statement causes program control to proceed with the first statement after the Select Case structure

  • The Case Else statement is the optional default Case which is executed when a match has not occurred for any previous Case. If used, the Case Else is always specified as the last Case.

  • The required End Select terminates the Select Case structure.

 

Select Case Types

The Select Case statement can be used with all variable types:

Dim someVariable As String
someVariable = "A String"
Select Case someVariable
     Case "A"
          ' This block will not be executed
     Case "B"
          ' This block will not be executed
     Case "A String"
          ' This block will be executed
     Case Else
          ' This block will not be executed
End Select

 

Select Case Ranges

You can use the keyword To to specify ranges in the Case expressions.

Dim someVariable As Integer
' set someVariable to something
Select Case someVariable
     Case 1 To 5
          ' works for all values of someVariable in the range 1 through 5
     Case Else
          ' all other values of someVariable
End Select


Multiple Conditions with Case Ranges

If you want to execute the same block of code for different values of the test expressions, you can do so by specifying the non-consecutive values, separated by commas.

Select Case someVariable
     Case 1, 5, 9
          ' this block will be executed when someVariable is equal to 1, 5, or 9
     Case 10 To 15
          ' this block will be executed when someVariable is equal to any number in the range 10 through 15
     Case Else
          ' this block will be executed when someVariable is equal to any value other than 1, 5, 9, or 10 through 15
End Select 

 

The Is Keyword

Keyword Is along with the comparison operators to specify a range of values to test.

Select Case someVariable
     Case Is > 15
         ' this block will be executed when someVariable is greater than 15
     Case Is < 10
         ' this block will be executed when someVariable is less than 10
     Case Else
         ' this block will be executed when someVariable is between 10 and 15
End Select 

 

Combining Ranges

To combine more than one range of values in a single Case statement, you can use the logical operators And and Or.

Select Case NumVar
     Case Is > 15 Or NumVar <= 10
          ' this block will be executed if NumVar is greater than 15 or less than or equal to 10
     Case Is >= 11 And NumVar <= 13
          ' this block executes when NumVar is 11, 12, or 13
     Case Else
          ' this block will be executed for any other value of NumVar
End Select 

 

Nested Select Case Statements

You can nest Select statements, but each Case has a corresponding End Select.

Select Case someVariable
     Case 1 To 3
          Select Case
someVariable
               Case 1
                    ' this block will be executed when
someVariable equals 1
               Case 2
                    ' this block will be executed when
someVariable equals 2
               Case 3
                    ' this block will be executed when
someVariable equals 3
          End Select
     Case Else
          ' this block is executed when
someVariable is not equal to 1, 2, or 3
End Select

Grade example using a Select Case

Select Case grade
      Case Is 
>= 90 
           lblStudentGrade.Text = "A"
      Case Is >= 80 
           lblStudentGrade.Text = "B"
      Case Is >= 70 
           lblStudentGrade.Text = "C"
      Case Is >= 60 
           lblStudentGrade.Text = "D"
      Case Else
           lblStudentGrade.Text = "F"
End Select


More

See also Selection Functions.