You are on page 1of 2

Notes on Programming 10.2005.1: What is programming?

For most of these notes I am indebted to: Cadenhead, R (1999): Sams Teach Yourself Java 2 in 24 Hours (Indianapolis: Sams Publishing). Computer programming is insanely difficult. It requires a four-year degree in computer science, thousands of dollars in computer hardware and software, a keen analytical intellect, the patience of Job, and a strong liking for caffeinated drinks. If you are a programming novice, this is probably what you have heard about computer programming. These rumours are greatly exaggerated. In fact, programming is a lot easier than most people think. However, there are several reasons why you might believe otherwise: Computer programmers have been telling people for years that programming is hard so that less people take to it and they can find high-paying jobs. Computer programming manuals are often written in a language that only Scrabble players would appreciate. Strange acronyms like OOP, RAD, COM, and MOD are frequently used along with newly invented jargon like instantiation, bytecode, and makefile. Many programming languages have been available only with software packages costing $200 or more, which is a lot of cabbage. Because of the growth of the Internet and other factors, it is a great time to learn programming. Useful programming tools are being made available at low cost (or no cost), often as downloads from World Wide Web sites. The key to learning how to program is to start with the right language. The programming language you choose often depends on the tasks you want the computer to accomplish. Each language has things it is well-suited for and things that are difficult, or perhaps impossible, to do with the language. A computer program, also called software, is a way to tell a computer what to do. Everything a computer does, from booting up to shutting down, is done by a program. Windows 95/98/NT/ME/XP is a program. Quake is a program. The dir command used in DOS to display file names is a program. Even the LOVELETTER virus is a program. Computer programs are made up of a list of commands the computer handles in a specific order when the program is run. Each of these commands is called a statement. While our minds solve problems in various ways and often think of other things as well, computers know no leeway. They follow instructions literally, one statement at a time. The following example of a computer program System.out.print ("Shall we play a game? "); String answer = Console.readString (); is equivalent to giving the computer the following to-do list (once it has been translated into plain English): Dear PC, Item 1: Display the question Shall we play a game? Item 2: Give the user a chance to answer the question. Love Your programmer Each line of the computer program is a statement. A computer handles each statement in a specific order, in the same way that a cook will follow a recipe. In BASIC, line numbers are used to put the statements in the correct order. Other languages, such as Java, do not use line numbers, favouring different ways to tell the computer how to run a program. Because of the way programs operate, its hard to blame the computer when something goes wrong while your program runs. After all, the computer is just doing exactly what you told it to do. Unless your hardware is not well, a serious virus is attacking your system, or your operating system is having a bad day, the blame for the program error lies with YOU, the programmer. Thats the bad news. The good news is that you cant do any permanent damage to your computer with the programming errors you make while you learn how to program.

This computer is stupid, it doesnt do what I want! (Only what I tell it to do.)
Please cut out or duplicate this now-famous saying (it might have been uttered by you!) and paste it on your computer monitor so that you never ever forget it. ( the centre of the screen may be best place!)

Notes on Programming 10.2005.1

Page 2 of 2

Most computer programs are written in the same way that you write a letter or other text document by typing each statement into a word-processor. Most programming languages allow you to type in any text-editing software. When you have finished writing the program, you save the file just like you would save any other document to disk. Like many document types, computer programs usually have their own filename extension to indicate what type of file they are (and so that the compiler can recognise them!). While MS Word documents require the extension .doc, Pascal programs use the extension .pas and Java programs .java. Note: When using a specialised word-processor, like MS Word, save the file in Text Only mode instead of saving it as a Word document. Notepad, a word-processor that comes with Windows, saves all files as unformatted text. There are, however, many specialised editors for the many different programming languages. Often they apply different colours to different types of words (usually reserved words for that particular programming language), assist you with the spelling of these special words and may even help with the layout of your program. Often these special editors come with the compiler. Java, being a non-proprietary programming language, has many editors by different companies. You might want to download the Java Development Kit (JDK) from Sun Microsystems web page. Or you can use the Ready to program with Java technology by Holt Software provided by your school. Most specialised editors are combined with a compiler and offer some degree of error detection and correction. An editor program (with compiler) that lets you compile and run the program without leaving the editor, is known as an IDE (Integrated Development Environment). Usually these are the easiest to use, especially if they also offer templates for specific program types. However, the type of suggestions for error correction and the general help program of a particular editor are two of its most important features and often determine its usefulness. For a program to run (after you have created it with the help of a word-processor or editor and saved it to disk) you need some help. Some languages need an interpreter to run a program that interprets each line of the program and tells the computer what to do. Most versions of BASIC are interpreted languages. While interpreted languages are often faster to test, they run more slowly than other programs because they have to be interpreted again and again. Other programming languages require a compiler. The compiler takes a computer program and translates it into a form that the computer can understand. It also does what it can to make the program run as efficiently as possible. The compiled program is saved to disk (often as an executable file) and can be run directly, without the need of an interpreter. Compiled programs run more quickly than interpreted programs, but have to be compiled before one can try them out. After any change to the program, the program has to be recompiled. Java is unusual in that it requires both a compiler and an interpreter. The text-based program (also known as source code) is saved with the .java extension. The compiler then creates a class (saved as .class) in bytecode. The bytecode is platform independent, i.e. it can be run on any operating system Windows, Mac, DOS, UNIX, Linux or Solaris. However, the specific operating system has to be equipped with a Java interpreter (the JVM Java Virtual Machine) that then interprets the class and runs it on the specific machine. Remember: Computers can only follow machine language instructions (in binary code). As each type of machine has its own machine language, a program compiled for Windows will not work on a Mac (and vice versa). Many new programmers become discouraged when they start to test their programs. Errors appear everywhere. Some of these are syntax errors (like the capitalisation errors in Java) which are identified by the computer as it looks at the program and becomes confused by what you wrote. Depending on the compiler you will get error messages, often pinpointing the error and suggesting corrections; other compilers are less helpful. Other errors are logic errors, which will give you the wrong results although the program compiles and runs. You must test for these with critical test values (where you can predict the answer), otherwise they might remain undetected and cause unintended results. As you start to write your own programs, expect to encounter errors. They are a natural part of the process! Programming errors are called bugs and the process of fixing errors is called debugging. Whether you want it or not, youll get a lot of debugging experience as you learn how to write computer programs. It is important to develop sound debugging techniques so as to get on with the job of programming quickly.

The art of programming is doing the right things to the right variables in the right order.
Prof B Hahn, UCT

mb

You might also like