Jump to content

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





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:


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:


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:


[TABLE]This:|Does the same as this:
variable += 1|variable = variable + 1
variable -= 1|variable = variable - 1
variable *= 1|variable = variable * 1
variable /= 1|variable = variable / 1[/TABLE]


I'll show you an example of it:


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:


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:

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:

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:


[TABLE]This:|Does the same as this:
variable &= "a"|variable = variable & "a"[/TABLE]


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

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.

Edited by Vswe, 21 March 2010 - 02:30 PM.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You can also use a + to concatenate a string in Visual Basic:
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

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

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

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What about div and mod operators? There should be such things.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog