VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

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

VB2015_fig13.1
Figure 13.1

VB2015_fig13.2

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
VB2015_fig13.3
Figure 13.3

Example 13.3

In this program, we use the logical operator And besides using the conditional operators. This means that both conditions must be met otherwise the second block of code will be executed. In this example, the number entered must be more than 100 and the age must be more than 60 in order to win a lucky prize, any one of the above conditions not fulfilled will disqualify the user from winning a prize. You need to add another text box for the user to enter his or her age. The output is as shown in Figure 13.4

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
VB2015_fig13.4
Figure 13.4

13.3(c) If….Then…ElseIf Statement

In circumstances where there are more than two alternative conditions, using just If….Then….Else statement will not be enough. In this case, we can use the If….Then…ElseIf Statement.The general structure for the if..then.. Else statement is

If condition Then
Visual Basic 2015 expression1
ElseIf condition Then
Visual Basic 2015 expression2
ElseIf condition Then
Visual Basic 2015 expression3

..

Else

Visual Basic 2015 expression4
End If

Example 13.4

This program can compute grade for the mark entered by the user. Is uses several ElseIf statements and the logical operator And to achieve the purpose. The outputs are as shown in Figure 13.5 and Figure 13.6

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
Grade = "C"

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

Else
Grade = "Out of Range"

End If

MsgBox("You Grade is " & Grade)

End Sub
VB2015_fig13.5
Figure 13.5

VB2015_fig13.6

Figure 13.6


❮ Previous Lesson Next Lesson ❯


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy