# The Basic Math Symbols in VB .NET

If you're doing any programming, you need to know how to use the basic Math symbols. The basic Math symbols in Visual Basic .NET are these:

+ The Plus sign adds numbers together

- The minus sign takes one number away from another

* The asterisk symbol, above the number 8 on your keyboard, is used to multiply

/ The forward slash on your keyboard is the divide by symbol

= The equals sign

A word or two about how to use the mathematical symbols in Visual Basic. You can use the operators by themselves:

Or you can combine them by using parentheses.

Here, Visual Basic will work out the sums in parentheses first, and then add the two sums together

answer = (8 - 4) + (4 -2)

But you've got to be careful with parentheses, because there is a strict order that VB uses when it's doing maths. Consider this sum:

answer = 8 - 4 + 4 + 2 * 2

Try that code behind a new button. Display the result in a MsgBox. What answer did you get? 12! It's wrong! But why?

You would think it would work out the sum like we do - left to right

8 - 4 = 4
+ 4 = 8
+ 2 = 10
* 2 = 20

But VB doesn't work it out like that. Visual basic will do the multiplying first. So it will calculate like this

2 * 2 = 4
8 - 4 + 4 = 8
8 + 4 = 12

To make sure Visual Basic does your sums correctly you have to be careful of the parentheses. Try changing the code to this:

answer = (8 - 4) + (4 + 2) * 2

Now what happens. That's right - you get 16! It's still wrong! At least it is if you are working from left to right. But Visual Basic isn't. It will do the (4 + 2) * 2 part first, and then add that to 8 - 4. Which gives you 16.

In order to force Visual Basic to get the sum right, you need even more parentheses. Try this code and see what happens:

answer= ((8 - 4) + (4 + 2)) * 2

Finally we get the answer we've been expecting - 20! The parentheses above have grouped our sums into separate sections, thereby forcing VB to do the sums in the right order.

So take care when using parentheses to do your sums: the order that Visual Basic does its sums does matter!

In the next section of the course, we'll have some fun adding menus to a Visual basic .NET form.

Back to the VB NET Contents Page
{"ft_lang_label":"__label__en","ft_lang_prob":0.8966564,"math_prob":0.97531796,"size":2310,"snap":"2021-43-2021-49","text_gpt3_token_len":640,"char_repetition_ratio":0.13573287,"word_repetition_ratio":0.044806518,"special_character_ratio":0.2987013,"punctuation_ratio":0.09259259,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99788284,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-25T15:11:08Z\",\"WARC-Record-ID\":\"<urn:uuid:3a710e76-5633-4e63-9f0a-90587a461513>\",\"Content-Length\":\"9883\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9c7461d3-e488-4005-b60f-79655ea52946>\",\"WARC-Concurrent-To\":\"<urn:uuid:8f4c600c-2d50-44ba-b34a-58c85e81973d>\",\"WARC-IP-Address\":\"62.182.20.50\",\"WARC-Target-URI\":\"https://www.homeandlearn.co.uk/NET/nets3p6.html\",\"WARC-Payload-Digest\":\"sha1:B2L2WMQ6AWHY22IFB2BIOEOXX4YXMAHQ\",\"WARC-Block-Digest\":\"sha1:2BPRXRYWMRLKP5DW67T3LW36MXZXMTUL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587711.69_warc_CC-MAIN-20211025123123-20211025153123-00122.warc.gz\"}"}