Control structures

Introduction to Control Structures                

                                                                                                             << Back to Index
Control structures allow you to control the flow of your program's execution. If left unchecked by control-flow statements, a program's logic will flow through statements from left to right, and top to bottom. While some very simple programs can be written with only this unidirectional flow, and while some flow can be controlled by using operators to regulate precedence of operations, most of the power and utility of any programming language comes from its ability to change statement order with structures and loops.

Decision Structures


Visual Basic procedures can test conditions and then, depending on the results of that test, perform different operations. The decision structures that Visual Basic supports include:
  • If...Then
  • If...Then...Else
  • Select Case

If...Then

Use an If...Then structure to execute one or more statements conditionally. You can use either a single-line syntax or a multiple-line block syntax:
If condition Then statement
If condition Then
statements

End If
The condition is usually a comparison, but it can be any expression that evaluates to a numeric value. Visual Basic interprets this value as True or False; a zero numeric value is False, and any nonzero numeric value is considered True. If condition is True, Visual Basic executes all the statements following the Then keyword. You can use either single-line or multiple-line syntax to execute just one statement conditionally (these two examples are equivalent):
If anyDate < Now Then anyDate = Now

If anyDate < Now Then
   anyDate = Now
End If
Notice that the single-line form of If...Then does not use an End If statement. If you want to execute more than one line of code when condition is True, you must use the multiple-line block If...Then...End If syntax.
If anyDate < Now Then
   anyDate = Now
   Timer1.Enabled = False      ' Disable timer control.
End If

If...Then...Else

Use an If...Then...Else block to define several blocks of statements, one of which will execute:
If condition1 Then
[statementblock-1]
[ElseIf condition2 Then
[statementblock-2]] ...
[Else
[statementblock-n]]

End If
Visual Basic first tests condition1. If it's False, Visual Basic proceeds to test condition2, and so on, until it finds a True condition. When it finds a True condition, Visual Basic executes the corresponding statement block and then executes the code following the End If. As an option, you can include an Else statement block, which Visual Basic executes if none of the conditions are True.
If...Then…ElseIf is really just a special case of If...Then...Else. Notice that you can have any number of ElseIf clauses, or none at all. You can include an Else clause regardless of whether you have ElseIf clauses.
For example,

Private Sub cmdLargest_Click ( )
   Dim a As Integer, b As Integer, C As Integer 
   a = InputBox ("First number")
   b = InputBox ("Secondt number")
   c = InputBox ("Third number")  
 
 If a > b and a > c Then            
      Print " Largest number is " & a
 Else
    If b > c Then      
       Print " Largest number is " & b
    Else
        Print " Largest number is " & c 
   End If
End Sub
Notice that you can always add more ElseIf parts to your If...Then structure. However, this syntax can get tedious to write when each ElseIf compares the same expression to a different value. For this situation, you can use a Select Case decision structure.

Select Case

Visual Basic provides the Select Case structure as an alternative to If...Then...Else for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more readable when there are several choices.
A Select Case structure works with a single test expression that is evaluated once, at the top of the structure. Visual Basic then compares the result of this expression with the values for each Case in the structure. If there is a match, it executes the block of statements associated with that Case:
Select Case testexpression
[Case expressionlist1
[statementblock-1]]
[Case expressionlist2
[statementblock-2]]
.
.
.
[Case Else
[statementblock-n]]

End Select
Each expressionlist is a list of one or more values. If there is more than one value in a single list, the values are separated by commas. Each statementblock contains zero or more statements. If more than one Case matches the test expression, only the statement block associated with the first matching Case will execute. Visual Basic executes statements in the Case Else clause (which is optional) if none of the values in the expression lists matches the test expression.
For example, suppose you added another command to the Edit menu in the If...Then...Else example. You could add another ElseIf clause, or you could write the function with Select Case:
 
Private Sub cmdDay_Click ( )
    Dim Index As Integer       
    Index = MsgBox ("Enter your choice")
 Select Case Index
      Case 0                  
         Print "SUNDAY"
      Case 1                  
         Print "MONDAY"
      Case 2                  
          Print "TUESDAY"
      Case 3                  
          Print "WEDNESDAY"
      Case 4                  
         Print "THURSDAY"
      Case 5                  
         Print "FRIDAY"
      Case 6                  
          Print "SATURDAY" 
      Case Else
          MsgBox ("Enter right choice")     
   End Select
End Sub
Notice that the Select Case structure evaluates an expression once at the top of the structure. In contrast, the If...Then...Else structure can evaluate a different expression for each ElseIf statement. You can replace an If...Then...Else structure with a Select Case structure only if the If statement and each ElseIf statement evaluates the same expression.