+ Reply to Thread
Results 1 to 4 of 4

Thread: VB.NET from beginner to advanced programmer Part 7 - If statements

  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 7 - If statements

    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





    We have now come to Part 7, If statements. The If Then statement(or only If Statement) is a block of code that will only be run if the specific condition is met and therefor will return True, if the condition returns False the code will jump past the If block and continue at "End If" which is the end of an If block. The syntax for and If statement are:

    Code:
    If <condition> Then
        'code to be executed if the condition are met
    End If

    A very simply example here below:

    Code:
    If True Then
         MessageBox.Show("Inside the If block")
    End If

    The If statement check if the condition is True, and here it is. This will make the line(s) inside the If block to be executed. In this example a messagebox will be shown. But if we change "True" to "False" in the above code, the messagebox will not be showed since the code will jump past the If block because the conditions where False.



    Now you're maybe a little bit confused, What condition? Why test if we have wrote True or False?

    Yes the above code has no real function at all, and the If statements there is completely useless. But now, we're going to use what we learned about in the two last lessons; Logical operators and Relational operators to make the If Statement not only "not useless" but also completely necessary.


    Firstly we use a relational operator:

    Code:
    Dim x As Integer
    x = 3
    
    If x > 0 Then
       MessageBox.Show("x is greater then 0")
    End If

    Now we can check if x is greater then 0 with a relational operator. If x is greater then 0 the "x > 0" operation will return True as you learned about in the last Part, the If statement will then check for a True and a False. If It gets a True from the relational operator the If block will be executed.

    Now just of a sudden we can test values and get our code to do different things depending on it. We can also include Logical operators to make it a little bit more complex and also more useful:

    Code:
    Dim x, y As Integer
    x = 7
    y = 5
    
    If x > y And y <> 0 Then
        MessageBox.Show("x is greater then y and y is not 0")
    End If






    If Then Else Statement:


    The opertunitives doesn't end there. We can also create something called an If Then Else statement. This works like an If Then Statement but it has two blocks of codes. If the condition is met(True) the first block of code will be run. But instead of just jumping to the End If straight away if the conditions aren't met(False) the second block of code is executed.


    The syntax for an If Then Else Statement looks like this:


    Code:
    If <condition> Then
        'code to be executed if the condition are met
    Else
        'code to be executed if the condition are NOT met
    End If


    Here comes and example on how to use it:

    Code:
    Dim x, y, z As Integer
    x = 1
    y = 9
    z = 14
    
    If x < y And y < z Then
        MessageBox.Show("x is smallest and z is largest")
    Else
        MessageBox.Show("The variables are not in size order")
    End If
    The code checks if x is less the y and if y is less then z. If this is True one message will be showed and if It's False another message will be showed.








    If Then ElseIf Else Statement:

    We can also add ElseIf blocks to an If Statement, we're able to add as many ElseIf blocks as we want in an If Statement, Not only one as it is with the If block and the Else Block. The ElseIf blocks is working exactly the same as an If block, the If block is the first one and then it's followed by ElseIf blocks and at the end you can add a else block if you want. If the If block's statement are True that block will be run and then the code will jump to the End If, however if it's False it will test if the condition of the first ElseIf block is True, if this also is False it will go to the second one etc. If all conditions are False and there's an Else block this will be executed.


    Here's the syntax for the If Then ElseIf Else Statement:

    Code:
    If <condition> Then
        'code to be executed if the condition are met
    ElseIf <condition> Then
        'code to be executed if the condition are met but no conditions above are met
    ElseIf <condition> Then
        'code to be executed if the condition are met but no conditions above are met
    ElseIf <condition> Then
        'code to be executed if the condition are met but no conditions above are met
    Else
        'code to be executed if none of the conditions are met
    End If





    Here comes an example on an If Then ElseIf Else Statement:


    Code:
    Dim x, y, z As Integer
    x = 2
    y = 5
    z = 8
    
    If x = 0 Then
        MessageBox.Show("x is equals to 0")
    ElseIf x < y And y < z Then
        MessageBox.Show("x is smallest and z is largest")
    ElseIf x = y And y = z Then
        MessageBox.Show("all variables has the same value")
    Else
        MessageBox.Show("None of the conditions above are met")
    End If
    In this example above we will see the message "x is smallest and z is largest" since this is the first of the conditions that is met. However, If we change x's value to 0, both the two conditions will be True. Then the first block which has a met condition will be the block who is executed, in this case we will see: "x is equals to 0".



    That was everything for this part, we will later learn about an option to If statements, see you later.
    Last edited by Vswe; 03-21-2010 at 03:29 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 7 - If statements

    I always hated the fact that VB didn't use brackets around conditions and I disliked Then...End If. Anyway, nicely done! +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 7 - If statements

    It's much easier to follow if you're not experienced. Then you can easily see what is ending, which is harder with only a bracket. But I understands what you means.

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

    Re: VB.NET from beginner to advanced programmer Part 7 - If statements

    What I hate is that if you break the elseif into else if, then you need extra end if's. +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: 5
    Last Post: 11-02-2009, 05:46 PM
  2. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:53 AM
  4. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 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