Comparison Operators
Here are some advanced operators in Visual Basic that are used a lot for making decisions in codes.
In comparison we have either "True" or "False" results. You will be using some mathematical operators, which you learned (=, <>, etc...).
Examples
4 = 4 Result = True
3 = 5 Result = False
69 < 68 Result = False
2 <> 4 Result = True
Text1.Text = 2 Result = ? Depends on what Text1.Text is
The If statement is a very important programming statement. If uses comparison operators to test values. It performs one of two comparison actions, depending on the test of the comparison. If the comparison test is true then the code executes;
If Text1.Text = "40" Then
Label1.Caption = "OK"
End If
As you can see, after comparing the two (Text1.Text to 40) there is the word "Then" after it. You always need to write "Then" after the comparison test. The second line is the code that executes if the test is true (you can have more then one line of code). After your done with the If statement you have to end it, that's where you put the "End If" code.
When the code executes in the If statement, it does so because the test's condition is true. Now what if it wasn't true, maybe you would want to inform the user? Now you can by using the "Else" statement which goes in the body of the If statement.
If Text1.Text = "40" Then
Label1.Caption = "OK"
Else
Label1.Caption = "You need to put in a number between 39 and 41"
End If
That's pretty simple, but there's more. There are a few more operators you haven't heard about - "And," "Or," and "Not."
If Text1.Text = "40" And Text2.Text = "30" Then
Codes...
End If
If (A > B) Or (C < D) Then
Codes...
End If
If Not(A = "OK") Then
Codes...
End If
If Text1.Text = "0" Or Text1.Text = "1" Or Text1.Text = "2" Or Text1.Text = "3" Then
Codes...
End If
Select Case is another statement that handles multiple choice conditions better then the Else statement in the If statement...
Select Case Expression
Case Case Value
VB Statements
Case Case Value
VB Statements
Case Case Value
VB Statements
End Select
Select Case is good for several choice conditions. Heres some code for you to follow on; (by the way you'll need a command button, a label and a textbox)
Private Sub Command1_Click()
Dim Text As String
Text = Text1.Text
Select Case Text
Case 1: Label1.Caption = "The user entered 1 in the text box"
Case 2: Label1.Caption = "The user entered 2 in the text box"
Case 3: Label1.Caption = "The user entered 3 in the text box"
Case 4: Label1.Caption = "The user entered 4 in the text box"
Case Else: Label1.Caption = "The user entered something else"
End Select
End Sub
Note: As you can see you can also use the Else code w/ Select Case, as shown in the codes above.
Just when you thought it was over... There are 2 more Select Case formats.
Select Case Expression
Case Is Whatever expression you want to perform against the expression above
VB Statements
Case Is Whatever expression you want to perform against the expression above
VB Statements
Case IsWhatever expression you want to perform against the expression above
VB Statements
End Select
In the case you would write something like Case Is <5: VB codes, or Case is >30: VB codes.
Select Case Expression
Case Expression1 to Expression2
VB Statements
Case Expression1 to Expression2
VB Statements
Case Expression1 to Expression2
VB Statements
End Select
In the case you would write something like "1 To 5:", or in full ex. Case 5 To 10: VB Codes.
Well there you have it. You now know how to make decisions in VB, and know the comparison statements that go w/ it.