Visual Basic Decision Making

Decision making statements come in two forms in Visual Basic, ‘If’ statements and ‘Case’ statements. Both allow a decision to be made between two or more options.

‘If’ Statements

In its most basic form an ‘If’ statement executes a group of statements if an expression evaluates to true. Its basic syntax is as follows.

If expression Then
    ' Statement(s) will execute if the expression is true.
End If

The following example checks whether the values of two variables are the same and displays a message to the console if they are.

Dim a As Integer = 10
Dim b As Integer = 10

If a = b Then
    Console.WriteLine("a is equal to b")
End If

This can be extended to execute a statement or statements if the expression is false.

If expression Then
   ' Statement(s) will execute if the expression is true.
Else
   ' Statement(s) will execute if the expression is false.
End If

This example adds an ‘Else’ statement to the one above to output a message if ‘a’ and ‘b’ are not equal.

Dim a As Integer = 10
Dim b As Integer = 10

If a = b Then
   Console.WriteLine("a is equal to b")
Else
   Console.WriteLine("a is not equal to b")
End If

The ‘If’ statement can be further extended with the use of ‘ElseIf’. Any number of ‘ElseIf’ statements can be used to extend the decision making process.

If expression Then
   ' Statement(s) will execute if the expression is true.
ElseIf expression Then
   ' Statement(s) will execute if the first expression is false
   ' and the second expression is true.
Else
   ' Statement(s) will execute if the expression is false.
End If

Again, the above example, that compares the variables ‘a’ and ‘b’ can be extended to check if they are equal, then check if ‘a’ is greater than ‘b’ and if neither of the conditions are true, output a third message.

Dim a As Integer = 10
Dim b As Integer = 10

If a = b Then
   Console.WriteLine("a is equal to b")
ElseIf a > b Then
   Console.WriteLine("a is greater than b")
Else
   Console.WriteLine("a is less than b")
End If

‘If’ statements can also be nested one inside another.

If expression Then
   ' Statement(s) will execute if the expression is true.
   If expression Then
      ' Statement(s) will execute if the expression is true.
   End If
End If

Below are some examples of ‘If’ statements using the logical operators discussed in the previous section on operators.

Dim a As Boolean = True
Dim b As Boolean = True
Dim c As Boolean = False

If a And b Then

    ' Display a message if 'a' and 'b' are true.
    Console.WriteLine("Condition 1 is true")

End If
If c Or b Then

    ' Display a message if 'c' or 'b' are true.
    Console.WriteLine("Condition 2 is true")

End If
If c And b Then

    ' Display a message if 'c' and 'b' are true.
    Console.WriteLine("Condition 3 is true")

Else

    ' Display a message if 'c' and/or 'b' are not true.
    Console.WriteLine("Condition 3 is not true")

End If
If Not (c And b) Then

    ' Display a message if 'c' and/or 'b' are not true.
    Console.WriteLine("Condition 4 is true")

End If

The results of running the above will be as follows.

Condition 1 is true
Condition 2 is true
Condition 3 is not true
Condition 4 is true

The Ternary Operator

The ternary operator provides a means to write an ‘if’ statement in shorthand form.

If(expression, true, false)

The expression can be the same as in a normal ‘if’ statement. If it evaluates to true then the code after the first comma gets executed, otherwise the code after the second comma gets executed.

The following example produces the same results as the ‘If-Else’ statement above. Here the ternary operator is used as part of a ‘Console.WriteLine’ statement, to incorporate ‘equal to’ or ‘not equal to’, into a message, depending on whether the variables are equal or not.

Dim a As Integer = 10
Dim b As Integer = 10

Console.WriteLine("a is " & If(a = b, "equal to", "not equal to") & " b")

‘Case’ Statements

Where there are more than two options in a decision, a ‘Case’ statement can be more efficient than using multiple ‘ElseIf’ statements.

Select Case expression

   Case expression-result-option1
      ' Statement(s) to execute.

   Case expression-result-option2
      ' Statement(s) to execute.
 
   Case expression-result-option3
      ' Statement(s) to execute.

   Case Else
      ' Statement(s) to execute.

End Select

A simple example of a ‘Case’ statement would be to display a message in the console depending on the value of a variable.

Dim a As Integer = 3

Select Case a

    Case 1
        Console.WriteLine("a equals 1")

    Case 2
        Console.WriteLine("a equals 2")

    Case 3
        Console.WriteLine("a equals 3")

    Case Else
        Console.WriteLine("a is not equal to 1, 2 or 3")

End Select

The message displayed in the console would be “a equals 3”. There is no limit as to the number of options that a ‘Case’ statement can have. The ‘Case Else’ at the end is a catch all if the value is something other than one, two or three.