Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
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:
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.Code:Dim P As Boolean = False MessageBox.Show(Not P)
The table below shows the output of Not P depending on the value of P:
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:
We declares two Boolean variable, P and Q. We set P to True and Q to False.Code:Dim P, Q As Boolean P = True Q = False MessageBox.Show(P And Q) Q = True MessageBox.Show(P And Q)
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":
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:
Code: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:
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:
Code: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:
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.
Code:P Xor QIn 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.Code:(P Or Q) And Not (P And Q)
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.
Last edited by Vswe; 03-21-2010 at 03:26 PM.
Interesting way of presenting these operators (vs the traditional if condition method). Nice work! +rep
I thought it would be better to talk about logical - and relational operators before continue further on to the if statements.
I like the presentation. It's how math classes teach it. +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks