Difference between revisions of "Visual Basic Flow Control"

From Techotopia
Jump to: navigation, search
(Using If ... Then .. ElseIf to Make Decisions)
(Evaluating Multiple Possibilities using Select Case)
Line 90: Line 90:
  
 
== Evaluating Multiple Possibilities using Select Case ==
 
== Evaluating Multiple Possibilities using Select Case ==
 +
 +
The Visual Basic ''Select Case'' construct is extremely useful if you need to evaluate a value or expression against multiple possible outcomes. For example you might want to compare a string against a variety of other strings in order to decide what action to take. The syntax for the ''Select Case'' construct is as follows:
 +
 +
'''Select Case''' ''expression''<br>
 +
&nbsp;&nbsp;'''Case''' ''value1, value2, ...''<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;...<br>
 +
&nbsp;&nbsp;'''Case''' ''value3, value3, ...''<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;...<br>
 +
&nbsp;&nbsp;'''Case''' ''value1, value2, ...''<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;...<br>
 +
'''End Select'''

Revision as of 14:59, 8 August 2007

In Visual Basic Comparison and Logic we looked at how to use Visual Basic logical expressions to decide is something is True or False. Much of the art of programming involves writing code that makes decisions based on one or more criteria. Such decisions define which code gets executed and which code gets by-passed when the application is running. This is often referred to as flow control since it controls the flow of execution.

In previous chapters the If statement has been widely used in examples. In this chapter of Visual Basic Essentials we are going to cover the various forms of If statements such as If .. Then .. Else, together with other decision making code structures such as Select Case and GoTo


Contents


Using If ... Then to Make Decisions

The If ... Then construct is probably the most common decision making tool used by Visual Basic programmers. The basic syntax of this decision structure is as follows:

If expression Then

.. Code to be executed is expression is True ..

Endif

Essentially if the expression evaluates to True (see Visual Basic Comparison and Logic for more details of this type of logic) then the code in the body of the statement is executed. If, on the other hand, the expression evaluates to False the code is skipped.

For example, if a decision needs to be made depending on whether one string matches another the following code might be used:

        Dim myName As String = "John Doe"

        If myName = "John Doe" Then
            MessageBox.Show("Strings match")
        End If

Clearly, the strings match, so the above code will cause the MessageBox to appear.

Using If ... Then .. Else to Make Decisions

The next variation of If statement allows us to also specify some code to perform if the expression in the If statement evaluates to '"False. The syntax for this construct is as follows:

If expression Then

.. Code to be Executed is expression is True ..

Else

.. Code to be executed is expression is False

Endif

We can, therefore, extend our previous example to display a different message if the comparison expression evaluates to be False:

        Dim myName As String = "John Doe"

        If myName = "John Smith" Then
            MessageBox.Show("Strings match")
        Else
            MessageBox.Show("Strings do not match")
        End If

In this case, the strings to do not match, so the second MessageBox will display.


Using If ... Then .. ElseIf to Make Decisions

The final variation of the If .. Then structure is the If .. Then .. ElseIf construct. This allows multiple expressions to be evaluated in a single structure. The syntax of If .. Then .. ElseIf is as follows:

If expression Then

.. Code to be Executed is expression is True ..

ElseIf expression Then

.. Code to be executed is expression is True

ElseIf expression Then

.. Code to be executed is expression is True

Endif

For example, the following code construct uses the ElseIf approach to find a match to a string:

        Dim myName As String = "John"

        If myName = "Fred" Then
            MessageBox.Show("Hello Fred")
        ElseIf myName = "John" Then
            MessageBox.Show("Hello John")
        ElseIf myName = "Paul" Then
            MessageBox.Show("Hello Paul")
        End If

This approach works well for a moderate number of comparisons, but can become cumbersome for a larger volume of values or expression evaluations. For such situations, the Select Case construct provides a more flexible and efficient solution.

Evaluating Multiple Possibilities using Select Case

The Visual Basic Select Case construct is extremely useful if you need to evaluate a value or expression against multiple possible outcomes. For example you might want to compare a string against a variety of other strings in order to decide what action to take. The syntax for the Select Case construct is as follows:

Select Case expression
  Case value1, value2, ...
    ...
  Case value3, value3, ...
    ...
  Case value1, value2, ...
    ...
End Select