TechnologyUK - Programming (VB6) Logo

Conditional Statements

The If ... Then statement

The If ... Then statement evaluates whether or not an expression is true. The construct is used as follows:

If ConditionToCheck Then Statement

The program examines a condition (ConditionToCheck), which can be a simple expression or a combination of expressions. If ConditionToCheck evaluates to true, the program will execute Statement. Any number of statements can be executed following the Then keyword. If only one statement is to be executed, the If ... Then statement can be written on one line, as shown above. If a number of statements are to be executed, each statement should appear on a separate line, as shown below.

If ConditionToCheck Then
    Statement 1
    Statement 2
    ...........
    Statement n

End If

Note the use of the End If keywords to terminate this construct.


Exercise

  1. Start Visual Basic and select Standard EXE by pressing Enter
  2. Right-click the form and click View Code
  3. In the Code Editor, click the arrow of the Object combo box and select Form
  4. Click the arrow of the Procedure combo box and select Click
  5. Implement the events as follows:

Private Sub Form_Click()
    If BackColor = vbRed Then BackColor = vbBlue
End Sub

Private Sub Form_Load()
    BackColor = vbRed
End Sub

Run the application and test it by clicking anywhere in the form to change its colour.



The If ... Then ... Else statement

This constructs offers two alternative courses of action. The first alternative is chosen if the specified condition is true, otherwise the second alternative is chosen. The construct is used as follows:

If ConditionToCheck Then
    Statement1
Else
    Statement 2
End If


Exercise

  1. Change the form's Click event as follows:

Private Sub Form_Click()
    If BackColor = vbRed Then
        BackColor = vbBlue
    Else
        BackColor = vbRed
    End If
End Sub

Run the application and test it by clicking anywhere in the form to toggle its colour.



The If ... Then ... ElseIf statement

This construct is like If ... Then ... Else, except that it offers a number of choices. The construct is used as follows:

If Condition1 Then
    Statement1
ElseIf Condition2 Then
    Statement2
    .
    .

ElseIf Conditionk Then
    Statementk
End If

The program will examine each condition in turn until it finds a condition that is true. Once a true condition has been found, and its statement executed, the program terminates the conditional search at EndIf. If none of the stated conditions are true, a default condition can be provided by adding a last Else section. This must be last in the list of conditions, and its associated statement would be executed only if none of the other conditions were true.


Exercise

  1. Change the form's Click event as follows:

Private Sub Form_Click()
    If BackColor = vbRed Then
        BackColor = vbBlue
    ElseIf BackColor = vbBlue Then
        BackColor = VBGreen
    ElseIf BackColor = vbGreen Then
        BackColor = VBBlack
    Else
        BackColor = vbRed
    End If
End Sub

Run the application and test it by clicking anywhere in the form to cycle its colour.



The Select Case Statement

If there are a large number of conditions to be tested, an alternative approach is to use the Select Case statement. The construct used is as follows:

Select Case Expression
    Case Expression1
        Statement1
    Case Expression2
        Statement2
    Case Expressionk
        Statementk
End Select

The condition described by Expression is evaluated, and compared with each of the expressions (Expression1, Expression2 etc.) that follow. Once a match is found, the corresponding statement is executed. If none of the stated conditions are true, a default condition can be provided by adding a Case Else statement at the end of the list.


Exercise

  1. Change the form's Click event as follows:

Private Sub Form_Click()
    Select Case BackColor
        Case vbRed
            BackColor = vbBlue
        Case vbBlue
            BackColor = VBGreen
        Case vbGreen
            BackColor = VBBlack
        Case Else
            BackColor = vbRed
    End Select
End Sub

Run the application and test it by clicking anywhere in the form to cycle its colour.