# Lesson 13 If..Then..Else

In this Lesson, we shall learn how to write Visual Basic 2015 code that can make decisions. We can write a Visual Basic 2015 program that can ask the computer to perform a certain task until certain conditions are met. In order to control the program flow and to make decisions, we can use the conditional operators and the logical operators together with the If..Then...Else keywords.

### 13.1 Conditional Operators

Conditional operators resemble mathematical operators. These operators allow a Visual Basic 2015 program to compare data values and then decide what actions to be taken. They are also known as numerical comparison operators which are used to compare two values to see whether they are equal or one value is greater or less than the other value. The comparison will return a true or false result. These operators are shown in Table 13.1

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

### 13.2 Logical Operators

In certain cases, we might need to make more than one comparisons to arrive at a decision.In this case, using numerical comparison operators alone might not be sufficient and we need to use the logical operators, as shown in Table 13.2. Logical operators can be used to compare numerical data as well as non-numeric data such as strings.  In making strings comparison, there are certain rules to follows: Upper case letters are less than lowercase letters, “A”<”B”<”C”<”D”…….<”Z” and number are less than letters.
Table 13.2: Logical Operators
Operator Description
And Both sides must be true
Or One side or other must be true
Xor One side or other must be true but not both
Not Negates true

### 13.3 Using the If control structure with the Comparison Operators

To control the Visual Basic 2015 program flow and to make decisions, we shall use the If control structure together with the conditional operators and logical operators. There are three types of If control structures, namely If….Then statement, If….Then… Else statement and If….Then….ElseIf statement.

#### 13.3(a) If….Then Statement

This is the simplest control structure which instructs the computer to perform a certain action specified by the Visual Basic 2015 expression if the condition is true. However, when the condition is false, no action will be performed. The syntax for the if…then.. statement is

```If condition Then

Visual Basic 2015 expressions

End If
```

#### Example 13.1

In this program, we insert a text box and rename it as txtNum and a button and rename it as OK. We write the code so that when the user runs the program and enter a number that is greater than 100, he or she will see the “You win a lucky prize” message. On the other hand, if the number entered is less than or equal to 100, the user will not see any message.

#### The code

```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
```

The output is as shown in Figure 13.1 and Figure 13.2

#### 13.3(b) If….Then…Else Statement

As we have seen in example 13.1, using  If….Then statement does not provide alternative output for the user. In order to provide an alternative output, we need to use the If….Then…Else Statement. This control structure will ask the computer to perform a certain action specified by the Visual Basic 2015 expression if the condition is met. And when the condition is false , we  usean alternative action will be executed. The syntax for the if…then... Else statement is

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

#### Example 13.2

We modified the code in Example 13.1 by adding the Else keyword and an alternative expression MsgBox("Sorry, You did not win any prize”). When you run the program and enter a number that is greater than 100, the message “Congratulation! You win a lucky prize” will be shown.Otherwise, you will see the “Sorry, You did not win any prize” message, as shown in Figure 13.3

#### The code

```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
```