Visual Basic Flow Control

From Techotopia
Revision as of 18:59, 7 August 2007 by Neil (Talk | contribs) (Using If ... Then to Make Decisions)

Jump to: navigation, search

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

Using If ... Then to Make Decisions

The If ... Then construct is probably the most common decision making tool used by Visual Basic programmers. teh 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 be come cumbersome for a larger volume of values.