|
Contents Home Page Climbing Programming Biking Running Web Links Programming Style Recommended books |
This page is a basic (no pun intended) introduction to programming section. Since leaving college this has been my source of employment. Programming is the means of writing, in a structured manner, instructions for a computer to follow. Natural languages such as English contain too many ambiguities to be used directly so a limited subset is usually used. In a similar way to legal language, each word or symbol has a specific meaning. In addition to the restricted language being used most programming languages have a syntax. This is a series of rules that allow the program that translates what the programmer has written into in a form that the computer can understand. This program is known as either a compiler or an interpreter depending on its mode of operation. In order for a program to be accepted by the translator the syntax must be correct, however the translator is not capable of detecting logical errors in the written code. These are errors in translating the problem into the code, more commonly known as bugs. At its simplest a programming language consists of four underlying constructs.
SequenceThis is the simplest concept, in effect the reading of the program begins at the left hand end of the first line and proceeds moving from left to right. When the end of the line is reached, the next line is begun. This is the same mechanism as reading this paragraph. When there are no more instructions to read the program stops or terminates. Obviously, a program consisting solely of sequence constructs is not exactly interesting, nor is it particularly useful. There are some forms of programs where this is exactly what you want. These are usually scripts that are intended to do one thing, once then terminate. SelectionSelection allows a program to make choices as to which part of the program to execute. At its simplest it consists of a check on a condition. If the condition is true then the next section of code is executed, otherwise it is skipped. IF it is raining THEN take umbrella The above line will only execute the command "take umbrella" if the test "if it is raining" is true. If the test is false then the "take umbrella" command is never undertaken. One point to note is that by convention code that is contained within a control statement such as the "if" line above is indented. This is not for the computers' sake but allows human readers to see at a glance how the control flow of the program is laid out. If every line of the code began in the first column then with more complicated control it would take some effort to work out which bit of code is associated with which control. In fact it takes very little more than the above to become so complicated that indentation is vital to reading. In fact it is often the case that spaces, newlines and tab characters are not important in the source code. Collectively known as whitespace these characters allow the code to be laid out in a logical manner. Note that some languages are sensitive to newline characters and some are sensitive to tabs so it is not an absolute rule. To see the effect compare the following two lines. Thisshouldbeaneasysentencetoreadifthewriterhadbotheredtousewhitespace. This should be an easy sentence to read since the writer has used whitespace. So what happens if we wish something to happen if the condition is false? Most programming languages extend the "IF" construct to have an "ELSE" part if required so...
IF temperature is less than 25C THEN take coat ELSE take suntan lotion In this case, we will take our coat if the temperature is cooler than 25 degrees Celsius otherwise we will take our suntan lotion. As you can see the general format is: IF test THEN statements when true [ELSE statements when false ]. Note that the test may be more two or more tests combined by Boolean logic as well as a single test as used above. IF temperature is less than 25C AND it is sunny THEN take coat ELSE take suntan lotion So what happens when we have a large set of tests to make? Many languages provide a mechanism for selection of a single item from multiple choices. This is usually known as a select or switch statement and takes the form. SELECT value CASE 1 statements for case when "value" is equal to "1" CASE 2 statements for case when "value" is equal to "2" CASE 3 statements for case when "value" is equal to "3" ... CASE n statements for case when "value" is equal to "n" END SELECT The above block works by checking value against each case value until a match is found. The statements connected with that case are then executed and the select block is then exited and the remainder of the code following the END SELECT statement is executed. It is important to note that only one set of statements will be executed on a single pass through this section of code. If more than one block is required then either the program must loop back (for which see the next section) and pass through this section again or the extra code is duplicated so that it appears in two of the cases. IterationIteration allows a program to loop over a section of code several times. It involves a test at either end of the loop to determine whether or not the program should go around the loop again. A loop may run for a fixed number of times, known as a bounded loop, or for an indeterminate number of times, known as an unbounded loop. The first is used where you know the number of items that you need to run the loop for, calculating the scores of a cricket team for example. The second is used where you do not know the number in advance, reading lines from a file on the computer's hard disk. In this case the condition or test for ending the loop would be along the lines of "have we reached the end of the file?" FOR count from 0 to ten STEP 1 print count The above block states that we are printing the numbers from 0 to 10 with increments or steps of 1. So the output would be: 0 1 2 3 4 5 6 7 8 9 10
The following is an example of an unbounded loop. WHILE we have not reached the end of the file get line from file print line print newline END WHILE This block reads each line in turn from a file on some storage device and prints each line out in turn until the end of the file is reached. The first form of the loop cannot be used in this case since we do not know in advance how many lines the file contains. We could read the file counting the lines then use the first loop format with the number thus obtained but this would mean reading the file twice: once to find out how many lines there were, and again to retrieve the information. not very efficient. All loops have the general form of: Initialise loop control test condition on loop control modify loop control All the above examples are in a form of structured English that is not a programming language but a format generally called "Pseudo code". This gives an idea of what the layout of the program will be. Converting this into actual code that may be run on a computer is relatively easy: all that is required is a knowledge of the syntax of the particular language to be used. The pseudo-code that I have used here is very similar to BASIC so converting to that is a simple matter of ensuring that the syntax is correct. For languages like C more work is required as that language uses various symbols to separate the program. Below is an example of a "for" loop in BASIC then C. FOR count FROM 0 TO 10 STEP 1 PRINT count
for(count = 0; count < 11; count++)
{
printf("%d ", count);
}
The first line is read as follows: Set the value of count to zero. Check if the value of count is less than eleven. Take the current value of count and add one to it. The body of the loop uses the C function printf, the use of which could take a whole page to explain but for this example code it states: Print a string which consists of a format specifier (indicated by the % symbol) which says "this is a place holder for what follows" (the 'd' in this case specifies an integer) then a space. The printf function takes this string and knows that there should also be an integer given to it. In this case it is "count" so the current value of "count" is substituted for the %d. Finally the string is printed. StructureIn the above discussion of the C style for loop the printf function was mentioned. So what is a function? Well as a program becomes larger having everything in one place becomes difficult to manage. Also if you have two identical sections of code in different locations then any changes to one section also needs to be done to the other. This could introduce bugs since in a rush the programmer may forget to update both sections. To enable the subdivision of larger programs, most programming languages have two means at their disposal. Functions and subroutines. The difference is that a function passes a value back to the part of the program that called or invoked it whereas a subroutine does not. For the purposes of this discussion I shall treat functions and subroutines as the same. To repeat the question asked above, "What is a function?" A function is block of code that is only executed if the program asks the function to run. To see this work, check out the snippet below. This is in C as all the ensuing snippets will be. Before we go any further some terminology: a constant is a value in the program that will never change whereas a variable is a named area of memory that may change through the lifetime of the program.
/* This is our function: it is called square, it must be called or run
with an integer and it passes back an integer. The word "int" is
reserved by the language and is used to define user variables.*/
int square(int x)
{
return x * x;
}
/* our main section of code Oh this is a C style comment by the way */
int a;
int b;
a = 2;
/* call the function. */
b = square(a);
The function is called with the variable 'a' which has the value 2 so the function will now square this giving 4 and pass this back to the main section of code. This assigns or sets the value returned from the function to the variable named 'b' so if that was printed out (using the printf function) we would see the number 4 on the screen. Although this is a simple example, it means that wherever we need to square a number then we do not need to write the full code out every time, but simply call the function square, giving it the number to operate on. If the operation being performed was very complicated it is easy to see that calling a function is much easier than writing out the operation every time and is much less prone to errors. A further advantage to functions is that if they are well named they allow the programmer to structure the program in such a way that it is easier for another programmer (or indeed the original author some time later when he has forgotten the workings of the code) to easily see what is going on in the program. Here is some simple code to pay a cricket team's wages. If all this code were to be written out long hand then it would be much more difficult to see what the code did but the use of well named functions has made the code clear.
/* Assume that 'player' is a string and that wages is an integer for
convenience. DEBIT is an integer that indicates to the 'adjustBankBalance'
function that the amount is to be deducted rather than added. */
for(playerCount = 0; playerCount < 11; playerCount++)
{
getName(player);
wages = calcWages(player);
payPlayer(player, wages);
adjustBankBalance(wages, DEBIT);
}
|