VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
Using logical operators is not something dificult, they simply take one or two boolean values and returns a boolean value depending on their values depending to each other. The logical operators in VB.NET is AND, OR, NOT and XOR.
The Not operator
The Not operator will return the opposite of the boolean value, Not True will return False and Not False will return True. See the example below:
Dim P As Boolean = False MessageBox.Show(Not P)
We have declared a boolean variable with value False. Then we show the value of "Not P" in a messagebox. Since P is False, Not P returns True, and this is what we will see.
The table below shows the output of Not P depending on the value of P:
[TABLE]P|Not P
True|False
False|True[/TABLE]
The And operator
The And operator will check if two boolean values are true. Both needs to True or the And operator won't return true. See the examples below:
Dim P, Q As Boolean P = True Q = False MessageBox.Show(P And Q) Q = True MessageBox.Show(P And Q)
We declares two Boolean variable, P and Q. We set P to True and Q to False.
Then we show "P And Q", since Q is not True this will return False and this is what we sees. Then we change the value of Q to True. Now when we show "P And Q" again we'll see True, since both P and Q are True.
Here's a table showing the value of "P and Q" depending on the values "P" and "Q":
[TABLE]P|Q|P And Q
True|True|True
True|False|False
False|True|False
False|False|False[/TABLE]
The Or operator
For the Or operator to return True, at least one of the values have to be True. If both are True doesn't matter, it will also return True. Look at this example below:
Dim P, Q As Boolean P = True Q = False MessageBox.Show(P Or Q) P = False MessageBox.Show(P Or Q)
After declaring the boolean variables P and Q we set P to True and Q to False. So now one of the values in the Or operation "P Or Q" are True, this means it will return True, this is what will pop up. After that we change the value of P to False. This means none of the variables are True and this will result that "P Or Q" will return False.
In this table you can see the value of "P Or Q" depending of what values "P" and "Q" has:
[TABLE]P|Q|P Or Q
True|True|True
True|False|True
False|True|True
False|False|False[/TABLE]
The Xor operator
The Xor operator will return True if and only if one of the values are True. This means that the different between Or and Xor is that if both values are True, Or will return True but Xor will return False, See this example:
Dim P, Q As Boolean P = False Q = False MessageBox.Show(P Xor Q) P = True MessageBox.Show(P Xor Q) Q = True MessageBox.Show(P Xor Q)
First both the boolean variables are False, this makes "P Xor Q" return False.
Then we change P to True, now the variables have a different value, P is True and Q is False. With different values the Xor operator will now return True. At last we also change Q to True. Now both the variables are True. This makes the Xor operator return False again. So the outputs in this example are False,True,False.
In the table below you can see the output from the Xor operator depending on the two values:
[TABLE]P|Q|P Xor Q
True|True|False
True|False|True
False|True|True
False|False|False[/TABLE]
Using them together:
It's possible to use as many Logical operators together as you want, even different ones. For example, these two code blocks below are doing the same thing, one using Xor and one using a combination of Not, And and Or.
P Xor Q
(P Or Q) And Not (P And Q)
In the second one, the more complex one, we fist check a normal Or operation between P and Q. But since we want this to behave like a Xor operator we also check if P and Q are the same. Then we convert that value with the Not operator since we don't want P And Q to return True(then if it return False everything is correct for us and therefor we want that to be True). So if both "(P Or Q)" is True and "Not (P And Q)" is true the whole thing will return True when we compare them with a last And operator.
This last thing was just an example how you can create more complex things, I hope it didn't confuse you. In the next part we will talk about Relational operators.
Edited by Vswe, 21 March 2010 - 02:26 PM.


Sign In
Create Account


Back to top









