Difference between revisions of "Visual Basic Comparison and Logic"

From Techotopia
Jump to: navigation, search
(New page: In the previous chapter (Visual Basic Arithmetic) we looked at how ''expressions'' are constructed in Visual Basic and looked in some detail at how to create arithmetic expressions. In...)
 
m (Text replacement - "<table border="0" cellspacing="0"> " to "<table border="0" cellspacing="0" width="100%">")
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
<table border="0" cellspacing="0" width="100%"><tr>
 +
<td width="20%">[[Visual Basic Arithmetic|Previous]]<td align="center">[[Visual Basic Essentials|Table of Contents]]<td width="20%" align="right">[[Visual Basic Flow Control|Next]]</td>
 +
<tr>
 +
<td width="20%">Visual Basic Arithmetic<td align="center"><td width="20%" align="right">Visual Basic Flow Control</td>
 +
</table>
 +
<hr>
 +
 +
 +
<htmlet>vbasic</htmlet>
 +
 +
 
In the previous chapter ([[Visual Basic Arithmetic]]) we looked at how ''expressions'' are constructed in Visual Basic and looked in some detail at how to create arithmetic expressions. In this chapter we will look at constructing comparison and logic expressions in Visual Basic.
 
In the previous chapter ([[Visual Basic Arithmetic]]) we looked at how ''expressions'' are constructed in Visual Basic and looked in some detail at how to create arithmetic expressions. In this chapter we will look at constructing comparison and logic expressions in Visual Basic.
 +
 +
== Visual Comparison Operators ==
 +
 +
Comparison operators provide the ability to compare one value against another and return either a ''True'' or ''False'' result depending on the status of the match. For example, you might use a comparison operator to check if a variable value matches a particular number, or whether one string is identical to another. Visual Basic provides a number of different operators for just about every comparison need.
 +
 +
The comparison operators are used with two operands, one to the left and one to the right of the operator. The following table outlines the Visual Basic comparison operators and provides brief descriptions and examples:
 +
 +
<google>ADSDAQBOX_FLOW</google>
 +
<table border="1" cellspacing="0">
 +
<tr style="background:#efefef;">
 +
<th>Operator<th>Type<th>Description<th>Examples</th>
 +
<tr>
 +
<td>=<td>Equal to<td>Returns ''True'' if first operand equals second<td>Dim intValue = 10<br>If intValue = 10 Then Exit Sub</td>
 +
<tr>
 +
<td><<td>Less than<td>Returns ''True'' if the value of the first operand is less than the second<td>If intValue < 10 Then Exit Sub</td>
 +
<tr>
 +
<td>><td>Greater than<td>Returns ''True'' if the value of the first operand is greater than the second<td>If intValue > 10 Then Exit Sub</td>
 +
<tr>
 +
<td><=<td>Less than or equal to<td>Returns ''True'' if the value of the first operand is less than, or equal to, the second<td>If intValue <= 10 Then Exit Sub</td>
 +
<tr>
 +
<td>>=<td>Greater than or equal to<td>Returns ''True'' if the value of the first operand is greater than, or equal to, the second<td>If intValue >= 10 Then Exit Sub</td>
 +
 +
</table>
 +
 +
== Visual Basic Logical Operators ==
 +
 +
''Logical Operators'' are also known as ''Boolean Operators'' because they evaluate parts of an expression and return a ''True'' or ''False'' value, allowing decisions to be made about how a script should proceed.
 +
 +
The first step to understanding how logical operators work is to construct a sentence rather than to look at a script example right away. Let's assume we need to check some aspect of two variables named intVal1 and intVal2. Our sentence might read:
 +
 +
''If intVal1 is less than 25 AND intVal2 is greater than 45 display a message.''
 +
 +
Here the logical operator is the "AND" part of the sentence. If we were to express this in Visual Basic we would use the AND logical operator:
 +
 +
<pre>
 +
        Dim intVal1, intVal2 As Integer
 +
 +
        intVal1 = 10
 +
        intVal2 = 45
 +
 +
        If intVal1 < 25 And intVal2 > 45 Then
 +
            MessageBox.Show("OK")
 +
        End If
 +
</pre>
 +
 +
Similarly, if our sentence was to read:
 +
 +
''If intVal1 is less than 25 OR intVal2 is greater than 45 display a message.''
 +
 +
Then we would use the Visual Basic ''Or'' operator in our If statement:
 +
 +
<pre>
 +
        Dim intVal1, intVal2 As Integer
 +
 +
        intVal1 = 10
 +
        intVal2 = 45
 +
 +
        If intVal1 < 25 Or intVal2 > 45 Then
 +
            MessageBox.Show("OK")
 +
        End If
 +
</pre>
 +
 +
Another useful logical operator is the ''Exclusive Or'' (XOR) operator. The XOR operator returns true if only one of the expressions evaluates to be true. For example:
 +
 +
''If intVal1 is EITHER less than 25 OR greater than 45 display a message''
 +
 +
We represent XOR with the keyword ''Xor'':
 +
 +
 +
<pre>
 +
        Dim intVal1 As Integer
 +
 +
        intVal1 = 10
 +
 +
        If intVal1 < 25 Xor intVal1 > 45 Then
 +
            MessageBox.Show("OK")
 +
        End If
 +
</pre>
 +
 +
The final Logical Operator is the ''Not'' operator which simply inverts the result of an expression:
 +
 +
<pre>
 +
 +
(10 > 1)        ' returns ''True''
 +
 +
Not(10 > 1)      ' returns ''False'' because we have inverted the result with the logical NOT
 +
 +
</pre>
 +
 +
 +
<htmlet>vbasic</htmlet>

Latest revision as of 20:16, 27 October 2016

PreviousTable of ContentsNext
Visual Basic ArithmeticVisual Basic Flow Control


Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99


In the previous chapter (Visual Basic Arithmetic) we looked at how expressions are constructed in Visual Basic and looked in some detail at how to create arithmetic expressions. In this chapter we will look at constructing comparison and logic expressions in Visual Basic.

Visual Comparison Operators

Comparison operators provide the ability to compare one value against another and return either a True or False result depending on the status of the match. For example, you might use a comparison operator to check if a variable value matches a particular number, or whether one string is identical to another. Visual Basic provides a number of different operators for just about every comparison need.

The comparison operators are used with two operands, one to the left and one to the right of the operator. The following table outlines the Visual Basic comparison operators and provides brief descriptions and examples:

<google>ADSDAQBOX_FLOW</google>

OperatorTypeDescriptionExamples
=Equal toReturns True if first operand equals secondDim intValue = 10
If intValue = 10 Then Exit Sub
<Less thanReturns True if the value of the first operand is less than the secondIf intValue < 10 Then Exit Sub
>Greater thanReturns True if the value of the first operand is greater than the secondIf intValue > 10 Then Exit Sub
<=Less than or equal toReturns True if the value of the first operand is less than, or equal to, the secondIf intValue <= 10 Then Exit Sub
>=Greater than or equal toReturns True if the value of the first operand is greater than, or equal to, the secondIf intValue >= 10 Then Exit Sub

Visual Basic Logical Operators

Logical Operators are also known as Boolean Operators because they evaluate parts of an expression and return a True or False value, allowing decisions to be made about how a script should proceed.

The first step to understanding how logical operators work is to construct a sentence rather than to look at a script example right away. Let's assume we need to check some aspect of two variables named intVal1 and intVal2. Our sentence might read:

If intVal1 is less than 25 AND intVal2 is greater than 45 display a message.

Here the logical operator is the "AND" part of the sentence. If we were to express this in Visual Basic we would use the AND logical operator:

        Dim intVal1, intVal2 As Integer

        intVal1 = 10
        intVal2 = 45

        If intVal1 < 25 And intVal2 > 45 Then
            MessageBox.Show("OK")
        End If

Similarly, if our sentence was to read:

If intVal1 is less than 25 OR intVal2 is greater than 45 display a message.

Then we would use the Visual Basic Or operator in our If statement:

        Dim intVal1, intVal2 As Integer

        intVal1 = 10
        intVal2 = 45

        If intVal1 < 25 Or intVal2 > 45 Then
            MessageBox.Show("OK")
        End If

Another useful logical operator is the Exclusive Or (XOR) operator. The XOR operator returns true if only one of the expressions evaluates to be true. For example:

If intVal1 is EITHER less than 25 OR greater than 45 display a message

We represent XOR with the keyword Xor:


        Dim intVal1 As Integer

        intVal1 = 10

        If intVal1 < 25 Xor intVal1 > 45 Then
            MessageBox.Show("OK")
        End If

The final Logical Operator is the Not operator which simply inverts the result of an expression:


(10 > 1)        ' returns ''True''

Not(10 > 1)       ' returns ''False'' because we have inverted the result with the logical NOT


Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99