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.
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
|>=||Equal to or Greater than|
|<=||Less than or Equal to|
|<>||Not equal to|
|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|
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.
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
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.
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
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
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
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