Lesson 13

If..Then..Else

In earlier lessons, you learned how to write VB2015 code that performs calculations and manipulates text. In this lesson, you will learn how to make the program decide what to do based on conditions. This is called decision making or program flow control.

Lesson focus:

Programs become much more useful when they can check conditions and respond differently based on the result. This lesson introduces conditional operators, logical operators, and the three main forms of the If control structure.

Lesson Overview

Lesson13
TopicDecision Making
Main FocusIf Control Structures
Key ToolsConditional and Logical Operators
Next StepSelect Case
13.1 Conditional Operators
13.2 Logical Operators
13.3 If Structures
13.4 Practical Examples

Controlling Program Flow

So far, most of the programs in this tutorial have executed their statements from top to bottom without making decisions. In real applications, however, the program often needs to choose between different actions depending on input from the user.

For example, a program may need to:

  • Display a success message if a number is above a certain value.
  • Reject input if a user does not meet age requirements.
  • Assign a grade based on a mark entered by the user.
  • Choose one outcome if a condition is true and another if it is false.

To do this, Visual Basic 2015 uses conditional operators, logical operators, and the If...Then...Else structure.

Comparing Values

Conditional operators compare two values and return either True or False. These operators are also called comparison operators.

Table 13.1: Conditional Operators
Operator Description
=Equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
<>Not equal to

These operators are used whenever the program needs to decide between alternatives.

Combining Conditions

In some cases, comparing one condition is not enough. You may need two or more conditions to be checked together. For this, Visual Basic 2015 provides logical operators.

These operators can be used with both numbers and strings. When strings are compared, the system follows certain rules: uppercase letters are smaller than lowercase letters, and numbers are considered smaller than letters.

Table 13.2: Logical Operators
Operator Description
AndBoth sides must be true
OrEither side can be true
XorOnly one side can be true, but not both
NotNegates a condition

Types of If Control Structures

There are three main types of If structures in Visual Basic 2015:

  • If...Then
  • If...Then...Else
  • If...Then...ElseIf

Each one is suitable for a different kind of decision-making situation.

If...Then Statement

This is the simplest decision structure. If the condition is true, Visual Basic performs the code inside the block. If the condition is false, nothing happens.

If condition Then
    Visual Basic 2015 expressions
End If

Example 13.1 shows a program where the user enters a number. If the number is greater than 100, the program displays a lucky prize message.

Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
    Dim myNumber As Integer
    myNumber = TxtNum.Text
    If myNumber > 100 Then
        MsgBox("You win a lucky prize")
    End If
End Sub

When the entered number is greater than 100, the message appears. Otherwise, no message is shown.

VB2015 Figure 13.1 Input for If Then example

Figure 13.1: Entering a number for the If...Then example

VB2015 Figure 13.2 Lucky prize output

Figure 13.2: Output when the condition is true

If...Then...Else Statement

Sometimes you want the program to do one thing when the condition is true and another thing when the condition is false. In that case, use the If...Then...Else structure.

If condition Then
    Visual Basic 2015 expression 1
Else
    Visual Basic 2015 expression 2
End If

Example 13.2 modifies the earlier lucky prize example so that the user always receives feedback.

Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
    Dim myNumber As Integer
    myNumber = TxtNum.Text
    If myNumber > 100 Then
        MsgBox("Congratulation! You win a lucky prize")
    Else
        MsgBox("Sorry, You did not win any prize")
    End If
End Sub

If the number is greater than 100, the congratulatory message appears. Otherwise, the program displays the alternative message.

VB2015 Figure 13.3 Output of If Then Else example

Figure 13.3: Output of the If...Then...Else example

Using the And Operator with If...Then...Else

In this example, there are two conditions. The number must be more than 100 and the age must be more than 60. Both conditions must be true for the user to win the prize.

Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
    Dim myNumber, MyAge As Integer
    myNumber = TxtNum.Text
    MyAge = TxtAge.Text

    If myNumber > 100 And MyAge > 60 Then
        MsgBox("Congratulation! You win a lucky prize")
    Else
        MsgBox("Sorry, You did not win any prize")
    End If
End Sub

Since the operator is And, both conditions must be satisfied at the same time. If either one fails, the prize is not awarded.

VB2015 Figure 13.4 Output of And operator example

Figure 13.4: Output of the two-condition prize example

If...Then...ElseIf Statement

When there are more than two possible outcomes, using only If...Then...Else is not enough. In that situation, use the If...Then...ElseIf structure.

If condition Then
    Visual Basic 2015 expression 1
ElseIf condition Then
    Visual Basic 2015 expression 2
ElseIf condition Then
    Visual Basic 2015 expression 3
Else
    Visual Basic 2015 expression 4
End If

Example 13.4 uses this structure to assign a grade according to the mark entered by the user.

Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
    Dim Mark As Integer
    Dim Grade As String

    Mark = TxtMark.Text

    If Mark >= 80 And Mark <= 100 Then
        Grade = "A"

    ElseIf Mark >= 60 And Mark < 80 Then
        Grade = "B"

    ElseIf Mark >= 40 And Mark < 60 Then
        Grade = "C"

    ElseIf Mark >= 0 And Mark < 40 Then
        Grade = "D"

    Else
        Grade = "Out of Range"
    End If

    MsgBox("Your Grade is " & Grade)
End Sub

This type of structure is very useful for grading systems, classification tools, approval systems, and many other practical applications.

VB2015 Figure 13.5 Grade input example

Figure 13.5: Entering a mark for the grading example

VB2015 Figure 13.6 Grade result output

Figure 13.6: Output of the grading example

Why Decision Making Is Important

Core takeaway:

Decision structures allow your program to behave intelligently. Without them, every user would get the same output no matter what input they enter. With If...Then...Else, your program can react differently to different conditions.

Build on This Foundation

Continue to VB2026

After learning the basics of decision making in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What is the difference between If...Then and If...Then...Else?
  2. When should you use ElseIf instead of just Else?
  3. Write a short VB2015 program that displays “Pass” for marks of 50 or above and “Fail” otherwise.

Go to Lesson 14

In the next lesson, you will learn how to use the Select Case statement for another style of decision making in Visual Basic 2015.