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 symbol above the number 8 on your keyboard tells Visual Basic to multiply two numbers |
/ | 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:answer = 8 + 4 answer = 8 - 4 answer = 8 * 4 answer = 8 / 4 Or you can combine them by using round brackets (parentheses). Here, Visual Basic will work out the sums in parentheses first, and then add the two sums together answer = (8 - 4) + (4 -2) answer = 4 + 2 answer = 6 But you've got to be careful with parentheses, because there is a strict order that VB uses when doing its Math. 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? Was it the answer you were expecting?If you didn't pick up much Math in school, you might do the sum from left to right, thereby giving you an answer of 20. 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 But try changing the code to this: answer = (8 - 4) + (4 + 2) * 2 Here, we've added some parentheses.Run the code and see what happens. That's right - you get 16! This time, Visual Basic will do the (4 + 2) * 2 part first, and then add that to 8 - 4. Which gives you 16. Try this code and see what happens: answer= ((8 - 4) + (4 + 2)) * 2 Now we get an entirely different answer - 20! The parentheses above have grouped our sums into separate sections, and we now get a third solution to our seemingly simple calculation.The lesson to learn here is to take care when mixing different Math symbols. Use parentheses to spell out to VB exactly what you mean. |
.
The Basic Math Symbols in VB .NET
Subscribe to:
Post Comments (Atom)
VB Tutorials
- Getting started
- VB Forms
- Controls Toolbox
- Adding Textbox Form
- VB and Properties
- The Text Property
- Adding splash colour
- Saving your work
- Create a New Project
- What is a Variable?
- Add cod button Form
- Writing first .NET code
- String Variables
- Get Text in a textbox
- More VB .NET variables
- Using variables
- Calculator Project
- code for calculator
- The Message Box
- If Statements
- Select Statements
- Add Combo Box
- Conditional Operators
- Three Exercises
- Introduction Loops
- For Loops
- Do Loops
- Times Table Program
- Code,Time Table Prog
- Basic Math Symbols
- Add menu to Form
- add code to Menu
- add Sub Menu to Form
- Shortcuts Add Menu
- VB menu Project
- Open File Dialogue Box
- Filter,OpenDialogueBo
- Open File Dialogue Box
- Save File Dialogue Box
- Cut,Copy,Paste,Undo
- Show,Hide Controls
- InsertImagesInPicture
- Add Checkbox to form
- code for Checkboxes
- Option ButtonsTo form
- Error Handling,Debug
- Design Time Errors
- RunTime Errors
- Try ... Catch
- Logic Errors
- Breakpoints,Debug
- What is an Array?
- Arrays ,Index Number
- Assigning Values Array
- ArraysBoundariesNotKnown
- String Variable Type
- use the Trim Method
- difference Char,Chars()
- use InStr Method
- use Substring Method
- Equals,Replace,Insert
- use Split and Join
- What is a Text File?
- Open Text File
- ReadText File Line
- Write Text File
- Appending Text File
- How to Copy a File
- How to Move a File
- How to Delete a File
- to Functions and Subs
- Create your own Subs
- Parameters in Subs
- ByVal and ByRef
- Create a Function
- ParametersInFunctions
- Modules - Part One
- Modules Part Two
- The Click Event
- The MouseDown Event
- The KeyDown Event
- The Form Load Event
- Classes and Objects
- Create own Classes
- Methods in Classes
- Creating Methods
- Create Pro-Classes
- Use your New Property
- Express, Databases
- Database Wizard
- database code
- DataSets,Data Adaptors
- DisplayData in DataSet
- Navigate a Database
- Move Database
- Add,Update,Delete Records
- Add a New Record
- Delete a Record
0 comments:
Post a Comment