+ Reply to Thread
Results 1 to 4 of 4

Thread: VB.NET from beginner to advanced programmer Part 8 - Arithmetical 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 8 - Arithmetical 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





    In this part I'll tell you about Arithmetic operators. The Arithmetic operators in VB.NET are addition(+), subtractions(-), multiplying(*) and division(/) and one more which we'll come to later on in this part. When using them in programing there's a few things you need to think of and a few more ways you can use them in but it's pretty much straight forward In this part we will also cover an extra operator for strings(&) which also can be very useful.


    Here comes an example on how to use the first three of them:


    Code:
    Dim X As Integer
    
    'Addition
    X = 4 + 1
    MessageBox.Show(X)
    
    'Substraction
    X = 7 - 5
    MessageBox.Show(X)
    
    'Multiplying
    X = 3 * 8
    MessageBox.Show(X)
    
    'All three
    X = 2 * 6 + 4 - 1
    MessageBox.Show(X)
    
    'All three with parentheses
    X = 2 * (6 + 4 - 1)
    MessageBox.Show(X)


    In thee first examples below we use one of addition, subtraction and multiplying to give X a value which we then shows. The two last is an example of the precedence rules. Programming uses the same rules as mathematics when it comes to precedence.

    The output for the above code would be: 5, 2 , 24, 15 and 18.





    When we using division we need to be a little more careful though. We can not divide something with 0, an error will occur if we do. A second thing is that even though we divide one integer with an other integer the answer mustn't be an integer, just as in math.

    If we keep those two things in mind we can create the code like this:


    Code:
    Dim nValue1, nValue2 As Integer, fAnswer As Single
    nValue1 = 5
    nValue2 = 2
    If nValue2 <> 0 Then
        fAnswer = nValue1 / nValue2
    End If
    MessageBox.Show(fAnswer)

    We have declared the variable which will store our answer as the type Single instead. So now we can get the answer with a few decimals too. Before we do the actual calculation we also use an If statement to make sure the second value isn't 0. In the example above the answer that will be showed is 2.5.





    When programming we can use these Arithmetic operators together with an equals to sign when setting values of variables to create some new functionalities.
    By adding the Arithmetic operators before the equals to sign we do not set the variable's value to the value to the right but just change it depending on what it already was and what the value to the right was, see this list below to see how it works:




    I'll show you an example of it:


    Code:
    Dim X As Integer = 3
    X *= 5
    X += 3
    X -= 12
    X /= 3
    MessageBox.Show(X)

    First X has the value 3. The second row will then take 3(the value of X) and multiply this with 5. This is 15 and that is now the value of X. On the third row we'll increase X's value with 3. X is now equals to 18. At the next row we use "-=" to decrease the value of X with 12. Now we only have 6 left and we divide that with 3 and stores it as X. Now X will have the value of 2. This is what we will see.





    Strings


    As I mentioned before you can use the & sign for manipulation of strings. By using it you can simply combine two strings together, one after the other:


    Code:
    Dim sMessage1, sMessage2 As String
    sMessage1 = "Hello"
    sMessage2 = "CodeCall"
    MessageBox.Show(sMessage1 & " " & sMessage2)
    In the example above we have one string with the value "Hello" and one with the value "CodeCall". Then we merge them together with a space in between by using two &s. The message we will see will then be "Hello CodeCall".



    With this we could easily improve our code from Part 2. The main part of the code was:

    Code:
    MessageBox.Show(txtName.Text)
    which showed the name the user had written to a textbox. But now by using some & signs we can make this better:

    Code:
    MessageBox.Show("Welcome " & txtName.Text & "!")
    Now if the user's have entered, for example, Bob, the message which will be show will be: "Welcome bob!", instead of just "Bob".




    We can use this in exactly the same way you used the Arithmetic operators together with an equals to sign:




    The code below will simply show "Hello" in a messagebox using "&=":

    Code:
    Dim sMessage As String
    sMessage = "H"
    sMessage &= "e"
    sMessage &= "l"
    sMessage &= "l"
    sMessage &= "o"
    MessageBox.Show(sMessage)





    This was everything for now, we'll continue in next Part.
    Last edited by Vswe; 03-21-2010 at 03:30 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 8 - Arithmetical Operators

    You can also use a + to concatenate a string in Visual Basic:
    Code:
    Dim a As String = "abc"
    Dim d As String = "def"
    Dim z As String = a & d
    Dim w As String = a + d
    ' The preceding statements set both z and w to "abcdef".
    Or string.concat: String.Concat Method

    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 8 - Arithmetical Operators

    yes I know that but what if you want to do 1 & 2? (Answer: double quotes ) I probably should have mentioned that though. Thanks for all your feedback.

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

    Re: VB.NET from beginner to advanced programmer Part 8 - Arithmetical Operators

    What about div and mod operators? There should be such things.
    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:20 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