+ Reply to Thread
Results 1 to 4 of 4

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

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

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

    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
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. 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:

    Code:
    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:







    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:


    Code:
    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":




    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 Q
    Code:
    (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.
    Last edited by Vswe; 03-21-2010 at 03:26 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

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

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

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

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

    I thought it would be better to talk about logical - and relational operators before continue further on to the if statements.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

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

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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2009, 05:40 PM
  2. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  4. Replies: 1
    Last Post: 11-02-2009, 05:43 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:38 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts