Programming - Part 1
Computer programmers, especially web programmers are very much in demand.
It's quite well paid, and it can be fun too!

These two programming modules explain the general concepts of programming. The aim is that you should understand what's involved in a commercial software project. It's useful for background information, and you need to know it if you're hoping for a job.
From the level of creating a simple piece of software such as a screen-saver, to the most complex level of space research programming. The principles of programing remain the same.
Programming websites is an excellent introduction to programming in general. Once you've mastered HTML, PHP and SQL, you can investigate further and try things like Java or compiled languages. Most operating systems come with at least one built in programming language - all Unixes come with 'C'. You can also find many superb freeware language compilers / interpreters if you look around.
Learning how to program computers isn't as difficult as you might think. All computers are built on the same principles, so the building blocks of any programming language are the same. Computers can only do a few of the things that people can do, so they're much easier to deal with than people!
Programming is very simple - as long as you can think logically! Professional programming involves a lot of thinking... Think carefully about what you're doing and how you're doing it. Don't rush. A well designed program is smaller and faster, plus it's easier to write, maintain, and use. It will save you time in the long run if you get into the habit of thinking things through.
Computer Basics
Computers are all the same... However big or small, whatever they've got connected, or not.
In the 1930's, Alan Turing, a 22 year old mathematician invented a general purpose computing machine that could theoretically solve any mathematical problem. He boldly stated his machine was as powerful (ignoring raw speed) as any that could ever be designed. He said this a decade before any computing industry existed, and time has proved him right. Turing is now considered to be the father of modern computers.
Once Turing showed that all computers are profoundly similar, his insights were soon converted into practical reality by John Von Neumann. The basics of the Von Neumann architecture were first published in 1944. (For a longer discussion read this.).
Von Neumann defined the basic elements of a computer as:
- Storage - This is memory and disk space in a modern computer. It stores BOTH programs and data.
- Processing Unit - This manipulates data according to a program.
- Control Unit - This synchronises all the other bits of the computer.
- Input/Output - The user-interface - how it interacts with the world. i.e. keyboard, screen, mouse, printer etc. Computers connect to peripherals via ports.
|
INFO: Interestingly, a new architecture and method of computing is now on the horizon - Quantum Computing which uses the bizarre properties of the quantum world to provide an incredibly powerful computer with far less hardware. Only time will tell if it can be made to work. |
Computers are much simpler than people(!). They can only really count in base 2 (or 'binary') i.e. they only have the numbers 0 and 1. This is because they all operate on the the principles of Boolean Logic which is a system for computing using just 'True' and 'False'.
All programs and data are stored in your computers memory, or hard-disk, in binary.
To get a better understanding of how binary works to give us 'proper' numbers and letters, read this excellent article on bits and bytes from HowStuffWorks.com
A program is a list of instructions.
You give instructions to your computer in a language (i.e. HTML), which follows certain rules of syntax (grammar). Just like giving instructions in a human language like English.
There is a choice of languages available for computers - just like there are different human languages.
Human languages have evolved, computer languages are designed - they are much simpler than human language.
Computers take everything absolutely literally. They need to be told instructions in exact detail. You have to think of all the implications of what you're doing, because the computer will never infer from your code what you really want!
Programming is, in essence: Analysis, Design, Code then Test. The actual writing of code is the simplest and shortest task, and the language you use is largely irrelevant! It's the understanding of a problem, and the development of a solution that's the clever bit!
You can achieve the same results with many different languages. The choice of language to use for a program is often taken quite late during the design process. Most computer languages all do pretty much the same things, just in different ways. There are some specialised languages though, such as SQL, which are designed for a specific task like accessing a database.
To write a program, you write your list of instructions or commands in a text file. This is your source code i.e. it's the basic source of your program.
INFO:
From this human-readable form, your source code might get changed into a machine-readable form of code ('machine-code'). In this case you have the source (your code) and the target (an 'executable', or 'binary' file...), which you can run. This process of converting source-code to machine-code is called 'compiling' the program, and it's done by a program called a 'compiler'.
Once compiled, the executable file is distributed, not the source code. Compiled programs cannot be read by humans. You will not be writing any code that'll be compiled during this course - it'll all be interpreted.
Interpreted languages are converted into machine-code at runtime - i.e. when you use them, not before.
HTML is an interpreted language - your browser (Internet Explorer / Netscape etc) creates the formatted web page from the source code it receives. In other words the browser 'renders' or 'paints' the web page from the instructions that are the HTML...
That's why browsers can show the same page differently... That's why you can see the source code when you click on 'View Source'.
|
Your source code is processed by the language interpreter software, which checks, then translates your source code into machine code.
All computer programs do a clearly defined job. To program a computer you have to know how to translate your requirements into a form the computer understands. The key to this is knowing what commands are generally available to you in all computer languages...
Computer Languages
Just as with human language, computer languages are describing a set of things that computers can do. Most computer languages are very generalised - like Perl, PHP, Java or C, but a few are used for specific functions: SQL is for dealing with databases, HTML is for formatting text and images.
As all computers are the same, and all languages are (roughly) the same, then once you have a grasp of what you can do with one language, then you can understand how any other language, on any other platform (type of hardware) works...
All general-purpose computer languages allow you to use the following features in your code:
- Literals: you can put real values such as numbers or strings (text) in your source code, e.g.:
- Numbers: 1, -20, 100, 99.9, 78645971.45987439
- Strings: 'hello', "GoodBye!"...
In most languages literals can be also be stored in 'constants' - a special type of variable whose value never changes.
For example. In the language 'C':
#define VERSION 2.1;
Defines the constant VERSION as '2.1'.
puts ("VERSION");
Prints the value of VERSION to the screen (i.e. '2.1').
- Variables: these are symbols that can contain a value.
For example, in Perl:
$hello = 'hello';
This assigns the value 'hello' to the variable $hello.
$hello = 99.2;
This assigns the value '99.2' to the variable $hello
In any execution environment, there are often a number of environment variables available to your program that contain useful information, such as the name of the program, info on who's running the program, and the program arguments (or parameters) that were passed to it...
The program arguments or command line options are values that are included when you run the program. e.g. a calendar program might have two parameters: year and month. You'd access it by typing in: 'cal 1987 6'.
In Perl, for example, the command line arguments are contained in a special variable called @ARGV
- Functions: these will do something.
print 'hello'; prints the text 'hello' on the screen.
open (FILE, ">orders.log"); opens the file 'orders.log' in 'append' mode.
Functions often give you a return value. The open function returns TRUE if the file was opened successfully, FALSE if it wasn't...
$a = cos($b); this statement puts the cosine ( a trigonometric function)of $b into $a
Here's a list of the kinds of functions you'll find built-in to most programming languages:
- Display Functions: These would be allow you to print out text, draw images, graphics, windows, tables, documents...
- Input Functions: Get input from the keyboard / mouse / ports...
- Arithmetic Functions: maths, trigonometry, square root, random numbers...
- String Functions: join, split, search, replace...
- File Manipulation Functions: open, close, read and write to files.
- Networking Functions: connect, send, receive network connections and data.
- Database Functions: open, close, query databases.
- Operators: These allow you to compare, assign and manipulate values. e.g.:
The '=' operator is the assignment operator.
The '+' (addition) operator adds two numbers (and sometimes two strings - depending on the language).
The '<' (less than) operator tests to see if one value is less than another:
1 < 2; (...is a complete statement which returns TRUE.)
Although some operators are language specific, there are a few basic classes of operators common to most languages:
- Numerical: + - / * %
- Assignment: = += -+ *=
- Comparison: < > <= >=
- Logical: AND OR NOT XOR && ||
- Increment/Decrement: ++ --
- Statements: these are combinations of variables, functions and operators that form a logically complete task. These are analogous to a sentence in a human language. Statements often have a special terminator character - like the semi-colon ';'.e.g.
print 'hello'; ...is a complete statement, whereas...
print ...on it's own isn't!
$var1 = $var2 * 25; is a complete statement (which assigns $var1 the value of $var2 times 25).
- Flow Control: Generally, your program executes line by line from the top of your source to the bottom. Sometimes though, you need loops, jumps or conditional statements. These allow you to define how your program flows. i.e.:
goto label; jumps to the place in the code where you put 'label:'
if (condition) then ... if the condition is true then do ...
else ... otherwise do ...
for $variable = 1 to 20 { do_something } $variable is set to each number from 1 to 20 in turn and do_something is done.
while (condition) do_something; While condition is true, do_something
- Libraries: These are functions that don't come ready made with the language, but either you, or other people have written. By writing your own functions, they effectively become part of the language itself and you can use them in any program!
Pseudo-Code
As the actual language used may not be decided until quite late on in a project, people sometimes use pseudo-code - a loosely specified language, which is only used for descriptive purposes. It's never actually run on a computer.
The reason you'd use pseudocode is because it allows you to start thinking like a computer, but without disrupting your creative flow - you just make up functions out of thin air, and generally take liberties that you couldn't if it was real code.
Here's a short example of a program written in pseudo code which performs password verification:
| Task (English) |
Command (PseudoCode) |
| Get username |
start:
print "Enter your username:"; get ($username);
Note: The 'get' pseudo-function gets a line of input from the keyboard. |
| Get password |
print "Enter your password:"; get ($password1); |
| Get password from database |
$password2 = do_sql("select password from database where username = $username");
Note: The 'do_sql' pseudo-function runs a query written in SQL on the database. |
| Compare passwords |
if ($password1 = $password2) goto next_screen; else print "Oops - wrong password"; |
| Try again |
goto start; |
Procedural Vs. Object-Oriented Languages
There are two main approaches to programming: procedural and object-oriented languages...
All programs have two parts: Data and Code. The data can change between successive runs of the program, the code doesn't.
With procedural languages, the relationship between code and data is only loosely defined. You can define your variables how you like and use them wherever you like. With object orientation, the code and data is held together in a stand-alone object. This concept is called encapsulation.
Procedural languages generally start at the top of the code, and run line by line until they reach the end. In fact, procedural programs are often coded as lots of separate functions or subroutines with a single 'main' function which calls the subordinate functions as and when it needs them.
Object-oriented languages, are more modular and event-driven. They still have all the same capabilities - you're just arranging your program differently.
Objects are arguably a safer and easier way to program complex systems than procedural languages. They encourage the programmer to think small and program individual objects, which fit together to produce the program as a whole. They are easier to test and once written, an object can be used in many different programs (the concept of code re-use).
Here's four good reasons why we won't be using object-oriented languages in this course:
- There's little advantage if your programs are simple. A simple task would be coded exactly the same in either a procedural or object language.
- The concept of object orientation takes a leap of understanding, whereas procedural languages are (for most people) quite intuitive.
- While both Perl and PHP have object capabilities, they are not object-oriented languages at heart.
- You never need to use object orientation, so it's an additional concept. Any task that can be done with O-O can be done in a procedural language.
Now you've got an idea of what computer languages are, and can do. The next stage is to look at how you actually create your programs - i.e. translate your requirements into things the computer can do...
|
|