The Basics of Programming


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) Variables

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, BASIC you can use the DIM statement or in C/C++ you can use int, float, char, long 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".


int number; /* Define an integer number (16-bit) */
long longnumber; /* Define a long-integer number (32-bit) */
short number; /* Define a short length number (8-bit) or boolean values */ char code; /* Define a character variable called code */ float avg; /* Define a floating point variable called avg */
double longvalue; /* Define a double length floating point variable */

Note: The text between the '/*' and '*/' is called a comment or remark, which is used to document the code. 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. Signed means that the value can be negative or postive values. Unsigned means that it can be positive only.
A special type is called void which means that no value is returned and can be discarded.
A variable, declared ina 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.

int numbers[100];
or sometimes like this:
int[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.

int count, numbers[101];
(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:
int mynumber = numbers[56];

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

int 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 contain. For example, a name could have upto 50 characters.

char name[50] = "Andrew Smith";

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.

char countries[4][20] = { "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 #declare pre-processor instruction to store values. E.g.

#declare PI = 3.14
#declare VAT = 20

In ANSI C, you can use the const statement instead, and you can also declare the type.

const float PI = 3.14;

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

Special variable types can be defined using the #typedef instruction, AmigaOS uses a number of these. The special types are found in the header file exec\types.h.
ULONG unsigned long;    /* define a new type called ULONG using 'unsigned long' type */
typedef uint16 usigned short;     /* define an unsigned short type for 16-bit values */

Next Page