Controlling Program Flow in Visual Basic 2010

One of the early programming concepts taught in most every
computer science program is flow control. Making decisions in your program
based on some condition is essential to doing useful work and producing an
application with that ability. Visual Basic 2010 contains all of the standard
programming constructs for flow control you would expect to find in a modern
language. In this article we’ll look at the familiar ones and maybe some
not-so-familiar ones as well.

Early versions of the BASIC language had conditional IF /
THEN statements, coupled with GOTO, to branch around a section of code and
resume execution at a specific line number. Looping was the other flow control
concept and used the FOR and NEXT keywords to iterate over some code a fixed
number of times. Those same concepts are still present in Visual Basic 2010
including "GoTo". Additional keywords have been added to bring more
functionality, but the underlying logic remains the same. The "While"
keyword repeats a block of code down to an "End While" statement until
the condition is met. A common programming construct is to use the statement
"While True" to loop forever and break out of the loop another way.
Here’s what a simple test block using these concepts might look like:

Module Module1

Sub Main()

Dim Done As Boolean

Done = False

While True

If Not Done Then

Console.WriteLine("We're not done yet")

Done = True

Else

Console.WriteLine("All done!")

GoTo alldone

End If

End While

alldone:

Console.WriteLine("Hit return to continue")

Console.ReadLine()

End Sub

End Module

You can also use "Exit While" or "Exit
For" to immediately jump out of either the "While" or
"For" loops. Many in the mainstream computer science fields frown
upon the use of "GoTo" as it can cause errors if not used carefully.
VB 2010 offers another form of the "For" loop with the addition of
"For Each x In Y". Here’s an example of how you might use it:

Dim Names() As String = {"Mom", "Dad", "Son", "Daughter"}


For Each name In Names

Console.WriteLine("Name = " & name)

Next

Console.WriteLine("Hit return to continue")

Console.ReadLine()

One of the things programmers often need to do is test
something that may or may not even be a valid test. In early versions of Visual
Basic you would have to create a section of code to handle errors using
something like the following:

On Error GoTo Handler
  Throw New DivideByZeroException()
Handler:
  If (TypeOf Err.GetException() Is DivideByZeroException) Then
  ' Code for handling the error is entered here.
  End If

A better way to deal with this is to use the
"Try", "Catch" and "Finally" keywords. This
allows you to catch an error condition immediately and deal with it in your
code. Here’s an example from Microsoft’s MSDN
site
:

Public Sub TryExample()
  Dim x As Integer = 5       ' Declare variables.
  Dim y As Integer = 0
  Try              ' Set up structured error handling.
    x = x  y       ' Cause a "Divide by Zero" error.
  Catch ex As Exception When y = 0 ' Catch the error.
    Beep()
    MsgBox("You tried to divide by 0.") ' Show an explanatory message.
  Finally
    Beep()         ' This line is executed no matter what.
  End Try
End Sub

The last construct we’ll discuss here is the
"Select" and "Case" keywords. These keywords allow you to
take different actions based on the value of some expression. Here’s the MSDN
example
:

Dim number As Integer = 8
Select Case number
  Case 1 To 5
    Debug.WriteLine("Between 1 and 5, inclusive")
    ' The following is the only Case clause that evaluates to True.
  Case 6, 7, 8
    Debug.WriteLine("Between 6 and 8, inclusive")
  Case 9 To 10
    Debug.WriteLine("Equal to 9 or 10")
  Case Else
    Debug.WriteLine("Not between 1 and 10, inclusive")
End Select

In this article we’ve taken a quick look at a number of flow
control concepts to help you write applications that need to make decisions or
to iterate some number of times. In future articles we’ll use these concepts as
we build some sample applications.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read