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

lesson 13 Using If...Then...Else


In the previous lessons, we have learned how to write code that accepts input from the user and displays the output. However, we cannot control the program flow. In this lesson, we shall learn how to write VB2013 code that can make decisions and control the program flow.

Decision-making process is an integral part of programming in Visual Basic 2013. The ability to make decision helps solve problems intelligently and provide useful output to the user. For example, we can write a Visual Basic 2013 program that can ask the computer to perform a certain task until a certain condition is met or a program that will reject non-numeric data. In order to control the program flow and to make decisions, we need to use the conditional operators and the logical operators together with the If...Then...Else control structure.

13.1 Conditional Operators

The conditional operators are powerful tools that resemble mathematical operators. These operators allow a Visual Basic 2013 program to compare data values and then decides what actions to take, whether to execute a program or terminate the program. They are also known as numerical comparison operators. They are used to compare two values to see whether they are equal, one value is greater or less than the other value. The comparison will return a true or false result. These operators are shown in the 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

Sometimes we might need to make more than one comparisons before a decision can be made and an action taken. In this case, using numerical comparison operators alone is not sufficient, we need to use additional operators, and they are the logical operators.

The logical operators are shown in the Table 13.2.

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

The above operators can be used to compare numerical data as well as non-numeric data such as text(string).  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.

13.3 Using the If control structure with the Comparison Operators

To effectively control the Visual Basic 2013 program flow, we shall use the If control structure together with the conditional operators and logical operators. There are basically 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 2013 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 2013 expressions

End If
Example 13.1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumber As Integer
 myNumber = TextBox1.Text
 If myNumber > 100 Then
 Label2.Text = ” You win a lucky prize”
 End If
End Sub

* When you run the program and enter a number that is greater than 100, you will see the “You win a lucky prize” statement. On the other hand, if the number entered is less than or equal to 100, you don’t see any display.

13.3(b) If…Then…Else Statement

Using just If….Then statement is not very useful in programming and it does not provide choices for the users. In order to provide a choice, we can use the If….Then…Else Statement. This control structure will ask the computer to perform a certain action specified by the Visual Basic 2013 expression if the condition is met. And when the condition is false, an alternative action will be executed. The syntax for the if…then... Else statement is

Example 13.2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumber As Integer
 myNumber = TextBox1.Text
 If myNumber > 100 Then
  Label2.Text = ” Congratulation! You win a lucky prize”
 Else
  Label2.Text = ” Sorry, You did not win any prize”
 End If
End Sub

* When you run the program and enter a number that is greater than 100, the statement “Congratulation! You win a lucky prize” will be shown. On the other hand, if the number entered is less than or equal to 100, you will see the “Sorry, You did not win any prize” statement

Example 13.3

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumber, MyAge As Integer
 myNumber = TextBox1.Text
 MyAge = TextBox2.Text
 If myNumber > 100 And myAge > 60 Then
  Label2.Text = ” Congratulation! You win a lucky prize”
 Else
  Label2.Text = ” Sorry, You did not win any prize”
End If
End Sub

* This program uses the logical operator And besides the conditional operators. This means that both the conditions must be fulfilled in order for the conditions to be true, 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.

13.3(c) If…Then…ElseIf Statement

If there are more than two alternative choices, using just If…Then…Else statement will not be enough. In order to provide more choices, we can use the If….Then…ElseIf Statement.The general structure for the if…then... Elseif statement is

If condition Then
Visual Basic 2013 expression
ElseIf condition Then
Visual Basic 2013 expression
ElseIf condition Then
Visual Basic 2013 expression

.
.

Else
Visual Basic 2013 expression
End If

Example 13.4

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mark As Integer
Dim Grade as String

Mark = TextBox1.Text
If myNumber >=80 Then
 Grade=”A”
ElseIf Mark>=60 And Mark <80
 Grade="B"
ElseIf Mark>=40 And Mark <60
 Grade=”C”
Else
Grade=”D”
End If
End Sub


❮ Previous lesson Next lesson ❯


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