Jump to content

VB.NET from beginner to advanced programmer Part 5 - Logical Operators

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
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





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.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Interesting way of presenting these operators (vs the traditional if condition method). Nice work! +rep

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I thought it would be better to talk about logical - and relational operators before continue further on to the if statements.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I like the presentation. It's how math classes teach it. +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog