The Basics of Programming

Variables

For computers to process information and to store information, it needs to allocate and access this information in memory. So, in programming, we need to use variables which are just named areas of memory which the program can store information such as numbers or text. We use the word variable because the contents of them can change.

a) Variable types

A variable name can be from one character long to many characters and may be alpha numeric and can only start with a letter. For example, count, name, total, age, address, balance, TotalUsers, Number501 etc. Variables, in some languages have to be defined first before that can be used. This helps with determining what the variable can contain and to allocate a certain amount to memory to store the information. For example, in C# you can use integer, string, single, double, boolea and so on to define variables.

Variable types: Integer is a whole number e.g. 1, 2, 45, 193, a float is a decimal number e.g. 3.1, 4.1234, 100.9, 0.6666, a long is a very long number e.g. 12372905, a char is a single character e.g. "M" and text is a string of characters e.g. "LONDON".

e.g.

Int16 number; /* Define an integer number (16-bit)  */
Int32
number; /* Define a 32-bit number, aka Integer */
Int64 longnumber; /* Define a 64-bit long-integer number */
short number; /* Define a short length number (16-bit) */ char code; /* Define a UTF-16 character variable called code */ Single avg; /* Define a floating point variable called avg */
Double longvalue; /* Define a double length floating point variable */ string str-value; /* Define a string value */
Boolean bool; /* Define a boolean (true/false) value */
Object obj; /* Define an object - any type */

Note: The text between the '/*' and '*/' is called a comment or remark, which is used to document the code, you cal also use double slashes (//) or triple slashes for documenting start of functions. Also, in C, all statements end with a semi-colon (;) character to denote the end of that instruction.
Number variables can be signed and unsigned. By default numbers are signed, which means that the value can be negative or postive values. Unsigned means that it can be positive only, for unsigned variables, prefix it with a 'u' e.g. UInt16, ulong etc.
A special type is called void which means that no value is returned and can be discarded.
A variable, declared in a function, as static means that its value is retained between function calls. A static variable, declared at top or global level, means that the variables are local to the compile unit (file), and do not show in the compile binary object, thus that they do not pollute the namespace.
A variable, declared as volatile would be used in special cases, such as compiler optimizations, interrupts are enabled, hardware drivers, or real-time tasks.
To set values for variables, you would use the assignment statement which is basically in the format: variable = expression where variable is the destination and expression is a value or a a formula or some other thing that can return a value. e.g.

number = 100;
name = "Simon"; /* Note: characters and string values are always surrounded by quotes */
count = 1;
code = "A";

To make programs more useful, you can also do arithmetic such as adding, subtracting, multiply, divide and so on. A list of arithmetic symbols is below:

Symbol Description
+ Add
- Subtract
* Multiply
/ Divide
^ Power to

For example to add two numbers together and divide it by another number:

number = (100 + 58) / 2;

Note that the 100 + 58 is enclosed in brackets. In programming, multiple and divide are calculated before any adds or subtracts. If no brackets were provided, then the result would be 100 + 29 = 129 instead of 158 / 2 = 79 (with the brackets).
You can also use variables in your expressions, as follows:

number = (150 + number2) / number3;

So if the number2 variable contains 60 and the variable number3 contains 10 then the result would be (150 + 60) / 10 = 21

b) Arrays

Variables are okay to store single values, but if you wanted to store 10s or 100s of values, then having 10s or 100s of variables e.g. number1, number2, number3 to number100 would be too clumbersum and wasteful. Therefore, you can use an array or a collection of variables together which share the same name. An array has a index, which allows you to access any of the individual values using for format: arrayname [index]. The index can be a number or another variable, this makes arrays ideal for use with loops and specifically counted loops. You will still need to declare arrays first, like variables and specify the maximum number of values in the square brackets. E.g.

Int16 numbers[100];
or sometimes like this:
Int16[100] numbers;

For example, if you want to store all the numbers from 1 to 100 into the array called numbers, you can use a for loop. In this example we have to allocate 101 in the definition as count(0) is usually the first indexed value of an array (0 to 99 = 100 values), so for 1-100 , we have to have 101 indexed values.

Int16 count[101];
for
(count=1; count<=100; count++) {    numbers[count] = count; }

The for statement reads like this: set a variable called count to value 1, check that the condition 'count<=100' is true before running the instructions between the curly brackets { } and then increment the value of count by 1 (++ = add 1). At the end curly bracket, repeat the loop until the count<=100 becomes false.

So to access the 56th item in array numbers, you can do this:
Int16 mynumber = numbers[56];

You can also initialize an array with constants using the curly brackets and seperating the values with commas.

Int16 figures[10] = { 45, 67, 123, 59, 12, 4, 23, 89, 66, 90 };

For single strings, you need to use an array of chars, and set the maximum size the string is likely to container. For example a name could have upto 50 characters.
To store strings, we have to use an array of arrays, as a string is basically an array of characters. So we specify two values in square brackets, the first is a number of strings, and the second is max length of the strings. So, in this example we have 4 country strings, with each a maximum of 20 characters long.

string name = "Andrew Smith";
string countries[4] = { "France", "Germany", "Poland", "Italy" };

c) Constants and type declarations

Another useful type of memory storage is the constant. If you wanted to store a value that does not change then you need to use a Constant. This type of variable can only be set once and then referred to later in expressions in assignments. Constant names are usually specificied in Upper Case to differ them from variables.

The typical instruction in to set a constant in C is to use the const instruction to store values. E.g.

const float PI = 3.12F;
const Int16 VAT = 20;

Then you can use the constant in your expressions in assignments. e.g. VAT_Value = Price * VAT / 100

 

Go to C# Contructs