Moving on to C# 2: Variables

Using Variables in C#

In this tutorial I'm going to show you how to use variables in C#. It's different to Small Basic but pretty straightforward.

I'm assuming that you've worked through the Small Basic Tutorial and are happy using variables in SB. I'm also assuming that you gone through my first tutorial on how to set up the C# environment to use the SB library.

To run the programs, below, you will need to open a project in Visual Studio Express using use the template that we created in the first tutorial and type (or paste) in the code.

Data Types

OK, so there are two basic types of data: textual data and numerical data. All variables represent data of one or the other type (or some combination of both).

In SB we don't care what type of data a variable contains we just use it. However, in C#, we have to say what type of variable we are going to use.

The most common (probably) text variable is the string. This is a number of textual characters strung together like this sentence. A string is represented as characters enclosed in quotes. You have seen this countless times in SB, no doubt.

Below is the C# equivalent of a program in Variables section the SB Tutorial. As in the first tutorial we are going to ignore the code outside of the block that represents our program. What we are interested in is inside the innermost curly braces.

And you can see that this code looks almost identical to the equivalent SB. Apart from the fact that each line ends with a semicolon - all C# statements do - the only difference is the additional first line:

string name;

This is called a declaration statement and it simply means that we are going to use a variable called name and that it will be a string type of variable.

(Actually, there is one other difference which is nothing to do with the languages involved: the last line contains a read from the TextWindow. This effectively stops the program and waits for the user to input something, or just press the enter key. If we don't do this the TextWindow disappears immediately the program  ends.)
 
        
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.SmallBasic.Library;

 namespace CSSB1
 {
  class Program
  {
   static void Main()
   {
    string name;
    TextWindow.Write("Enter your name: ");
    name = TextWindow.Read();
    TextWindow.WriteLine("Hello " + name);
    TextWindow.Read();
   }
  }
 }
 


That's it! A C# program that uses a string variable to say 'hello'.

So what about numbers?

Numbers

Actually, there are a couple of different types of numbers. There are integer numbers like 1, 2 and 3, the ones we use to count with, and then there are so-called real numbers like 3.1416 - a very rough approximation to Pi. The difference is clear: integers do not have decimal parts, real numbers do.
Here's the C# version of the program that adds two numbers together. The numbers that we are adding here are integers. So we need to declare the variables that we are going to use as integers:
 
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.SmallBasic.Library;

 namespace CSSB1
 {
  class Program
  {
   static void Main()
   {
    int number1;
    int number2;
    int number3;
    TextWindow.WriteLine("Enter two numbers");
    number1 = TextWindow.Read();
    number2 = TextWindow.Read();
    number3 = number1 + number2;
    TextWindow.WriteLine(number3);
    TextWindow.Read();
   }
  }
 }
Again, not much difference to the SB version.
Now, we are going to write the temperature conversion program from the SB tutorial. But for this we want to use real numbers, soo the type of variable we are going to use is float.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.SmallBasic.Library;

 namespace CS_SB_Lib1
 {
     class Program
     {
         static void Main(string[] args)
         {
             float fahr;
             float celsius;

             TextWindow.Write("Enter temperature in Fahrenheit: ");
             fahr = TextWindow.ReadNumber();
             celsius = 5 * (fahr - 32) / 9;
             TextWindow.WriteLine("Temperature in Celsius is " + celsius);

             TextWindow.Read();
        }
     }
 }


What would happen, you might ask, if you use the wrong type, if for example you use int instead of float? Well, "not a lot", is the answer. The program still runs perfectly happily but you'll lose any decimal part of the answer. For example, using integers, 100 degrees F gives the result 37, whereas with real numbers it gives the better answer 37.77778.
And if you try to use string instead of float? Well, as you might expect, the program won't run. You'll get a helpful error message from Visual Studio:

 

  Operator '-' cannot be applied to operands of type 'string' and 'int'
  C:\Documents and Settings\User\Local Settings\Application Data
  \Temporary Projects\CSSB1\Program.cs   17 28 CSSB1


Which is telling you that you can't use the minus operator with a a mixture of the string variable fahr and the integer 32. Fair enough.

Accuracy

You might be asking yourself why it is that a powerful language like C# gives you a less accurate answer to the temperature conversion than good old Small Basic (try it! SB gives you many more decimal places).
OK, so try replacing float with double and running the program again. And then replace double with decimal.

You can see with each change in type that the accuracy gets better. So, what's going on here?
C# has three different types that can represent real numbers float, double and decimal, and each one is more accurate than the previous one.

Why?

The more accurate the representation of a number is, the more mememory it needs to store it and the longer it takes to do a calculation. So C# gives you the choice of (relatively) slow and accurate or faster and less accurate. Whichever, type you use, though, C# calculations will be vastly faster than you can achieve in small Basic.
Do we have the same thing for integer types? The answer is yes. There are actually a whole bunch of integer types but the most common are short, int and long.

The different numeric types that we've talked about and their range of values is given below.

short      -32768 to 32767

int        -2147483648 to 2147483647

long       -9223372036854775808 to 9223372036854775807



float      1.5 x 10^-45 to 3.4 x 10^38 
           accurate to 7 significant digits

double     5.0 x 10^-324 to 1.7 x x 10^308 
           accurate to 15-16 significant digits

decimal    1.0 x 10^-28 to 7.9 x 10^28 
           accurate to 28-29 significant digits 

The last three can represent negative numbers in the same ranges, as well.

Also, note that the real numbers are often approximations to the actual value as they are only accurate to the number of digits specified.

Comments

Post a Comment

Popular posts from this blog

Just Enough Python

Learn to code with C

Introduction to Python by Andrew Ng