Subedit - Introduction to Programming A582383

0 Conversations

Put simply, programming is just telling a computer what you want it to do. Unfortunately, this isn't as easy as it sounds.

Programming is like trying to tell a very logical alien, who knows no human languages, how to make a cup of tea. Imagine - the alien has no concept of tea, cups, sugar, milk, water, and kettles, and doesn't recognise the words you use for their arms, hands, and eyes or any of the relations between them.

Now, through a complex series of cross-cultural mime you have to design a language that is analogous to your own but can be easily translated into the alien's language. After that, as long as you explain clearly and methodically what things are, how things work, and what the alien needs to do, you should eventually get a cup of tea. On the other hand, if your explanations are unclear, ambiguous, incomplete, or untranslatable you could end up with a kitchen awash with tepid water... and possibly a dead alien.

What is a computer program?

A computer program is a very detailed and precise set of instructions that tell a computer how to perform a task. Now the design and creation of the set of instructions for a task (a program), and the removal of errors1 involves four main stages:

  1. First, you define the task the program is being designed to perform.
  2. Then, you express the instructions in a suitable programming language.
  3. Next, you translate the program into a code that the computer understands.
  4. Finally, you do a test run, find the errors in the code, and remove the errors2.

Errors can be failures in interpretation of instructions, or simply badly written instructions. These errors can lead to the program not working at all or carrying out a task different from that intended. However, unlike in the alien example above, bugs do not usually result in damage to the computer itself, although they have been known to damage users3.

Tasks for computers

There is a wide range of tasks that a computer can perform, within reason. In effect if a need is there that can be filled by a programmed device, someone will be asked to write a program for it.

Programs tend to come in 2 types:

System Software - This type of software is concerned with telling the system how to use parts of itself or to organise and coordinate the system as a whole. For example, drivers for PC graphics or sound cards tell the computer how to use the cards, and the Windows operating system tells the computer how to coordinate and organise data and instructions.

Application Software - This type of software is concerned with applying a device to a task. For example a word processing program is an application as it applies your PC to the task of word processing.

So what is the best way to define the tasks we ask computers to perform? In all probability the best way is to look at the mind-boggling array of uses we have for computers and programming in the world around us.

What is Programming used for?

Well it depends on what people want to use it for. It can be used for the following examples, but really the uses are only limited by developers imagination. Programming is used to create:

  • systems to deal with repetitive tasks
  • systems to help organise data
  • expert systems(i.e., diagnosis programs, computerised researchers, etc.)
  • amusement. In fact there is a mind-boggling amount of computing used for amusement, from web sites, the internet, arcade games, virtual reality consoles, web radio, television, video game consoles, and simulators of all varieties.
  • applications - tools and resources, such as word processors or the h2g2 website.
  • invisibles - a rather a blanket term, this covers everything that doesn't say 'Hello! I'm a Computer', including things you might expect (i.e., video recorders, robots) and some things you wouldn't (i.e., washing machines, refridgerators). These all include programming, although it is mainly compiled onto chips and wired into an electronic circuit. These programs stretch from very simple (i.e., maintain a cold temperature range in the refridgerator) to mind-bogglingly complex (i.e., work out the 3D location of an object, based on a pattern identifier working from images from two differently positioned cameras, and then move a robotic arm to meet the object).

Programming Languages

Why are there programming languages? Why can't it be written in natural language?

There is a good reason for this, computers can only deal with data in a very basic way and the tasks computers perform must be broken down into a series of very simple actions. These actions are based upon the operations a computer can perform, which tend to break down into the following categories:

  • Move data around in memory.
  • Take in data from an external device.
  • Give out data to an external device.
  • Store and retrieve data.
  • Compare two data values and say whether they are equal, or how they are not equal.
  • Arithmetic operations: add, subtract, multiply, divide, increase by one (increment), decrease by one (decrement)


Each task the computer has to perform is based on simple actions using these operations. These actions must be described very precisely and natural language is not suited to this task.

The problems in using natural language are very clear if you try to think about a seemingly precise instruction in natural language from the computers perspective. For example 'Take the Davies file to Personnel!' is a perfectly clear and precise natural language instruction to a person. But that instruction would leave the computer with the questions, 'Where is the file?', 'Where is personnel?', 'How do I move the File there?', 'Where am I?', 'What am I?', more questions on the detailed nature of the file and finally pleading for an explanation of the concept of time. Additionally, if traditional computer programs were written in natural language they would be very long and complex and full of details that a human would find unnecessary.

Programming languages are designed to bridge this gap between human thought and computers. It does this by being clear in its meaning to the human mind but also, after simple translation, to have meaning to the computer.

There are four generations of programming language and many different languages within each generation.

4th Generation LanguagesMATLAB, MathCAD, closer to human language than computer language
3rd Generation LanguagesFORTRAN, the C programming language, C++
2nd Generation Languageassembly languagecloser to computer language than human language
1st Generation Languagemachine code (binary)computer language

However many of these languages have concepts in common and one of the purposes of this project is to explain some of these concepts.

Common Programming Concepts

Programming is used to explain to a computer in terms it understands, exactly what you want it to do, to what variable and the nature of the variable. Now this means that there are a lot of overlaps between different programming languages. In this section we will work out exactly where those overlaps are, and lead to entries on general definitions of how these aspects of a program work.

Now the best way to define these overlaps is to look at what programming is used to define, in terms of the tea making alien, it is used to define the following:

  1. Variables - what data is involved and what the data is (i.e., cup, kettle, water, tea, sugar, milk, visual input, arm, hand).
  2. Process - what series of events happens (i.e., what series of instructions to execute), in what case, and in what order. These parts of the program are known as conditional statements, programming loops, and variable manipulation, with examples such as 'put water in kettle', 'turn on kettle', 'put tea in cup', 'put sugar in cup', 'when water boiled put water in cup', 'add milk', 'remove tea'.

Now these aspects are the basis for most program code. They allow you to convert things from a natural language idea into a programming language structure.

How do these common programming concepts work to create a program?

The best way to answer this question is with an example. The following is a program in natural language, in other words the way that we would speak:

STEP 1STEP 2STEP 3STEP 4
The postman goes to the gate of each house. If there is a letter for the house he goes to the door and posts the letter through the letterbox. If there is no letter for the house, he goes to the next house. This continues until there are no more letters for the street.

This in fact displays obviously two of the main concepts in programming, conditional statements4 and loops5. These are also fundamental aspects of human thought, conditional statements and actions we repeat. These and other common aspects allow programmers to outline concepts in human thought within a framework the computer can use.

The next stage is the breaking down of the natural language program into a series of simple operations based upon the code of a programming language. This is basically the construction of a detailed algorithm for the computer to define the task in the form of the programming language. An algorithm is understandable to the programmer, but it takes on more computer-like qualities. However it is worth noting that the code still unusable by the computer in that form.

To explain some of the conversion, below is a generic programming form to display the general points6.

>
STEP 1 Define Variables (integer): current_address, current_letter_address, number_letter_remain: set start value to 0;
STEP 2 Define Variable (Boolean): letter_delivered: set start value to 'FALSE';
STEP 3 Get values for current_letter_address and number_letter_remain from external device (Postbag);
STEP 4 Get value for current_address from external device (Eyes);
STEP 5WHILE(number_letter_remain NOT EQUAL TO 0)Start of Loop 1
STEP 6WHILE (letter_delivered EQUALS 'FALSE')Start of 2
STEP 7IF(current_address LESS THAN current_letter_address)Increase current_address by 1
STEP 8ELSE IF(current_address GREATER THAN current_letter_address)Decrease current_address by 1
STEP 9ELSE IF(current_address EQUAL TO current_letter_address)Deliver Letter and set letter_delivered to 'TRUE'
STEP 10END OF LOOP 2
STEP 11number_letter_remain = number_letters_remain - 1
STEP 12 Get value for current_letter_addressfrom external device (Postbag);
STEP 13END OF LOOP 1

The above fake programming language example shows how we can construct a program to work on a task based upon these building blocks which are common to most programming languages. However, we have to remember there are variations in how to use these common concepts between languages, for example what they are called and how they are used7.

Now this is all well and good, but we have to remember that there are many different things to keep in mind when writing a program aside from the task itself. It is very rare to find a person who can write successful code instinctively, so over time methods of best practice have developed. Some of the aspects of best programming practice are covered in the seven secrets of a successful programmer.

After we create a programming language program we have to move on to translating the programming language program into computer code8. Binary language is understandable to the computer and is as small as possible so as to help the program to run9 effectively.

Translation

This is the most important part of creating a program, this is the stage where you translate it into a form that the computer can run.

This requires another program to change your program code into the 1's and 0's that the computer uses. In order to do this programming languages set down a syntax that makes conversion easy for the program. This is also where errors can occur if you have used the programming languages set syntax incorrectly.

The programming language program is converted into the machine code by a program that is associated with the language. There are 3 main types of this kind of program; assemblers, compilers, and interpreters. Now which is used depends on the level of the language, 2nd generation languages use assemblers, and 3rd generation and above use either a compiler, or a interpreter. We will now look at how these programs operate.

What is an assembler?

Well, this is the program that converts Assembly code into the binary used by the computer. This differs from the other converters as with assembly language you have a very basic set of mnemonics10 and all that is required is replacing a mnemonic with its corresponding binary code. There is however some basic syntax that tells the assembler how to covert some of the mnemonics and also tells the assembler if a word is a mnemonics or a label11.

What is a compiler?

A compiler is vital to many programming languages. The compiler turns the instructions, which the programmer wrote into instructions that the computer's Central Processing Unit or CPU12 can understand.

The only language a CPU can understand is machine code, which is just binary numbers. For example, when using assembly language with a Motorola's 6802 microcontroller, the instruction INCA13
would be compiled into this machine code: 01001100

This is how the compiler does its work.

The compiler sweeps through the codes from one to as many number of times it requires to make it into machine code. In the old days, the compiler would just turn everything you wrote into machine code, even if your code would attempt doing some irrational things, as long as it was constructed of instructions and not undefined symbols. If you don't know, an instruction is a set of characters agreed upon beforehand, which the compiler can make into machine code (for example, the INCA instruction mentioned above).

Today, the compilers will not accept just anything and sometimes will even tell you where in your code the problem lies.

If you wondered why it would take the compiler several sweeps through the code before it's finished, well, you could imagine someone writing a riddle, and when you solve the riddle, the answer you get is a riddle too, and so on, until you get to the final answer, which is in plain and easily understood English. So, the compiler takes the code and simplifies it (separating it into different instructions, checking each instruction for validity, etc.) until it makes it into machine code. Also, when a part of the code relates to another part of the code, the compiler checks them against each other and makes sure that they 'fit', so to speak.

What is an interpreter?

Well it is a system some program languages use to translate a program into the code the CPU understands. The main difference between this method and a compiler is that it translates the code while the program is being run. In other words: Translate line 1, execute line 1, translate line 2, execute line 2, and so on until the end of the program is reached or a program halting error encountered.

Debugging

When you write a program it is an accepted fact that it is impossible to write something correctly first time. And so it will be teaming with little errors, known as bugs. Now the task of debugging is finding these pieces of code that aren't working the way you want them to. This is made increasingly difficult by another accepted fact, which is that to try every single possibility of even a relatively simple program can take longer than a person's life. So in effect in debugging you are testing the limits and general operations to see whether they work, and if they don't in what way they go wrong so you can correct the program. And in doing this your ultimate aim is to try and prove the correctness of your program.

1In the computer world, such errors are also known as 'bugs'.2This is also known as 'debugging' the program.3A well known and a tragic example is when a company created a radiation therapy control program that gave an incomprehensible error message when a particular error happened, so the nurse clicked through it. It wasn't until later that they realised the error had resulted in the patients receiving severe radiation burns.4Conditional statements use the word 'if'.5Loops use the word 'until', instructing the computer to repeat an action until a given point.6This is not an actual programming language, it is nearly a useful guide to the application of the common programming aspects to a program.7This is illustrated by loops; there are many different types of loop depending on where the condition is tested, and how the condition of the loop is dealt with.8Binary, a collection of 1's and 0's relating to the power being on or off in a wire within the computer, and in groups this relates to memory chip addresses, or functions imbedded in the processor.9It is when using the binary language that the program actually performs its function.10Pronounced ni-MON-ic, a mnemonic is a word or phrase used to aid memory.11In assembly language, you can label lines of code to aid movement within the program.12The CPU is also known as the MPU or Main Processing Unit.13INCA stands for 'INCrement register A by 1.'

Bookmark on your Personal Space


Conversations About This Entry

There are no Conversations for this Entry

Entry

A662708

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more