VB2010 Lesson 9 - Using If.... Then.... Else
VB2010 Lesson 9 - Using If.... Then.... Else
and display the output without controlling the program flow. In this chapter, you will learn how
to write VB2010 code that can make decision when it process input from the users, and control
the program flow in the process. Decision making process is an important part of programming
because it will help solve practical problems intelligently so that it can provide useful output or
feedback to the user. For example, we can write a VB2010 program that can ask the computer to
perform 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 control structure.
9.1 Conditional Operators
The conditional operators are powerful tools that resemble mathematical operators . These
operators allow a VB2010 programto compare data values and then decide what actions to take,
whether to execute a program or terminate the program andmore. They are also known as
numerical comparison operators. Normally they 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 9.1.
Table 9.1: Conditional Operators
Operator
Meaning
Equal to
>
More than
<
Less Than
>=
<=
<>
Not Equal to
Operator
Meaning
And
or
Xor
Not
Negates truth
s
* Normally the above operators are use to compare numerical data. However, you can also
compare strings with the above operators. 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.
9.3 Using the If control structure with the Comparison Operators
To effectively control the VB2010 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... Elsestatement
and If....Then....ElseIf statement.
9.3(a) If....Then Statement
This is the simplest control structure which ask the computer to perform a certain action
specified by the VB expression if the condition is true. However, when the condition is false, no
action will be performed. The general format for the if...then.. statement is
If condition Then
VB expression
End If
Example 9.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.
9.3(b) If....Then...Else Statement
Using jus 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 VB
expression if the condition is true. And when the condition is false ,an alternative action will be
executed. The general format for the if...then.. Else statement is
If condition Then
VB expression
Else
VB expression
End If
Example 9.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 dif 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 dif not win any prize" statement
Example 9.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
Example 9.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 then
Grade="B"
ElseIf Mark>=40 and Mark<60 then
Grade="C"
Else
Grade="D"
End If
End Sub