So you've come back for more I see? I'm glad that you want to continue your path in programming. As before we will be using Visual Basic .Net and IDEONE which we will be using again for out programming platform.
This lesson we will be doing some simple mathematics, exploring the uses of operators and creating variable dimensions. Remember when you are entering code, make sure that you put capital letters exactly where I put them, or your results may vary!
1. Open IDEONE in a new window or tab on your browser and select Visual Basic .NET from the list of programming languages as we did in lesson 1.
2. Clear all of the pre-made code in the window so we can input our own code. Clearing the code and re-writing it will help you to memorize the code for later use. So input the following into the code window:
Imports System
Public Class Test
Public Shared Sub Main()
3. At this stage we are now going to do something different from last time. We are going to write this into the code window:
Dim integer_1 as Integer = 5
Dim integer_2 as Integer = 10
Dim result_add as integer
Dim result_sub as integer
Dim result_mul as integer
Dim result_div as integer
Dim result_combi as integer
'Dim' stands for dimension, meaning you are setting the dimensions for the variable, and a variable is something which holds raw data. In this case our variable is 'integer_x'. You can set variables as something called an array which can hold multiple values by putting:
Dim integer_3(x) as Integer 1 Dimensional Array
Dim integer_4(x, x) as Integer 2 Dimensional Array
However we will be working with non array variables today. There are many different variable types within Visual Basic .NET including:
- Integer - 4 bytes of data
- Single - 4 bytes of data
- Double - 8 bytes of data
- Long - 8 bytes of data
- String - A string of unicode text up to 2 billion characters
- Char - One unicode character
- Date - 8 bytes of data to hold a date
And of course a few more!
The part of the code we entered where we have placed '= x' is where we pre-define data for the variable, but we have not done that for the result as we want the code to generate data for the result automatically. You may have also noticed we have Dim'd a result variable for each operation and one for a combination of operations.
There are several 'operators' in visual basic, but we will be focusing out attention on the four basic ones: Adding, Subtracting, Multiplying and Dividing; represented of course as '+,-,* and /'.
Code time:
4. Now we can begin doing some maths, first we will add:
result_add = integer_1 + integer_2
'result_add' is at the start of the sum as we need to tell the computer that we want the value of 'result_add' to be equal to the sum of 'integer_1 + integer_2" as opposed to on a calculator where we would do 'integer_1 + integer_2 = result_add".
Now we can finish off the other 3 operators using the same formula:
result_sub = integer_1 - integer_2
result_mul = integer_1 * integer_2
result_div = integer_1 / integer_2
Now to be really fancy we will combine some operators using brackets:
result_combi = (integer_1 + integer_2) * integer_2
Your code should now look like this:
5. Time to output your results, you may remember the 'console.writeline' statement from out last program, well we are going to use it again, this time a little more complexly):
Console.Writeline("Adding = {0}", result_add)
Console.Writeline("Subtracting = {0}", result_sub)
Console.Writeline("Multiplying = {0}", result_mul)
Console.Writeline("Dividing = {0}", result_div)
Console.Writeline("Adding then Multiplying = {0}", result_combi)
Lets dissect this part a bit:
'{0}', these curly brackets are holding the reference number to the argument outside of the speech marks. 0 is always the first number, no matter how many times you use this, always start with 0 and work up if there is more than one argument in your statement.
', result_x', Here we place a comma marking the next argument, which is our variable.
This is the easiest way of having text and a variable appear in the same writeline statement. You could of course write multiple writeline statements, but this is untidy and will produce multiple lines when we really only need one.
6. Now we just need to round off our code with our closing statements:
End Sub
End Class
Now click run and see what it's like to be a true programming mathematician!
Your results should be:
Next time we will try our hand at inputting data!
Once again, thanks for reading.
Joseph Wright - SCOTECH
No comments:
Post a Comment