Changes

Jump to: navigation, search

Visual Basic Comparison and Logic

4,333 bytes added, 14:46, 7 August 2007
no edit summary
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:
 
<table border="1" cellspacing="0" width="100%">
<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</td>
<tr>
<td>!=<td>Not equal to<td>Returns ''true'' if first operand is not equal to second<td>$myVar = 10; <br>if ($myVar != 20)<br>echo 'myVar does not equal 10';</td>
<tr>
<td><><td>Not equal to<td>Returns ''true'' if first operand is not equal to second<td>$myVar = 10; <br>if ($myVar <> 20)<br>echo 'myVar does not equal 10';</td>
<tr>
<td>===<td>Identical to<td>Returns ''true'' if first operand equals second in both value and type<td>$myVar = 10;<br>$myString="10"; <br>if ($myVar === $myString)<br>echo 'myVar and myString are same type and value';</td>
<tr>
<td>!==<td>Not identical to<td>Returns ''true'' if first operand is not identical to second in both value and type<td>$myVar = 10;<br>$myString="10"; <br>if ($myVar !== $myString)<br>echo 'myVar and myString are not same type and value';</td>
<tr>
<td><<td>Less than<td>Returns ''true'' if the value of the first operand is less than the second<td>$myVar = 10; <br>if ($myVar < 20)<br>echo 'myVar if less than 20';</td>
<tr>
<td>><td>Greater than<td>Returns ''true'' if the value of the first operand is greater than the second<td>$myVar = 10; <br>if ($myVar > 5)<br>echo 'myVar if greater than 5';</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>$myVar = 10; <br>if ($myVar <= 5)<br>echo 'myVar is less than or equal to 5';</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>$myVar = 10; <br>if ($myVar >= 5)<br>echo 'myVar is greater than or equal to 5';</td>
 
</table>
 
== PHP 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 desicions 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 $var1 and $var2. Our sentence might read:
 
''If $var is less than 25 AND $var2 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 PHP we would use the comparision operators we covered earlier together with the && logical operator:
 
<pre>
 
if (($var1 < 25) && ($var2 > 45)
echo 'Our expression is true';
 
</pre>
 
Similarly, if our sentence was to read:
 
''If $var1 is less than 25 OR $var2 is greater than 45 display a message.''
 
Then we would replace the "OR" with the PHP equavalent ||:
 
<pre>
 
if (($var1 < 25) || ($var2 > 45)
echo 'Our expression is true';
 
</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 $var is EITHER less than 25 OR greater than 45 display a message''
 
We represent XOR with the keyword ''xor'':
 
<pre>
 
if (($var1 < 25) xor ($var2 > 45)
echo 'Our expression is true';
 
</pre>
 
The final Logical Operator is the NOT operator which simply inverts the result of an expression. The ! character represents the NOT operater and can be used as follows:
 
<pre>
 
(10 > 1) // returns ''true''
 
!(10 > 1) // returns ''false'' because we have inverted the result with the logical NOT
 
</pre>

Navigation menu