1. Computing

How a Computer Reads a Program

From , former About.com Guide

1. Clueless Computers

Before starting to program, it is very important to have an appreciation for how the computer is going to look at your program. The fact of the matter is that, when your program is executed, the computer will do nothing unless your program tells it to do so. This includes reading input from the user or a file, doing something with that input, and giving the results in a predictable manner (usually either to screen or to print). None of this happens unless you, the programmer, tell the computer to do it. You do not need to tell it the fine details of how to do it, but you must tell it to do it.

At their core, computers are machines. Machines operate on the basis of command: you tell them to do something and they do it. This is an essential aspect of computers to be recognised by anyone who wants to program: the computer does what you tell it to do and does it faster than you could. But if it receives bad input, it will give bad output. In the early days of computing, programmers used an acronym for this: GIGO (Garbage In, Garbage Out). Like speaking Swahili to a Parisian, if you do not give the computer the right commands in the expected way, you will not get the right results in the way you expect.

2. Reserved Words Are Key

There are two parts to every executing program: data and code. The first is fairly obvious; it is the information to be processed. The second is comprised of three kinds of statements: assignment, iteration, and operation. The kind of statement determines what the computer will do with it. Unlike in human speech, computers do not understand polite tones. There is no such thing as "I would like you to print now" in computer programming. Instead, one simply tells the computer "print", and it does it.

Programming happens when one tells the computer to do something in a way that the computer understands. So, the secret of programming is how to tell a computer to do something. This differs by programming language. No one programs in the language that the computer itself uses (binary). Instead every language is translated for the computer. Exactly which words are translated and how depends on the vocabulary of the programming language. This vocabulary is a set of terms called "reserved words".

Reserved words are trigger words. When the Python interpreter or computer reads them, it triggers a response, an action. Depending on what the term is, the computer will do something immediately (e.g., 'break') or will look for more information. If two reserved words begin with some of the same letters, the computer reads the program until it is clear which word is meant; only then does the computer do something.

3. Picking an Argument - Part 1

The concept of looking for more information, however, needs further unpacking. Programming languages have a lot of the same characteristics as human languages. If we want to tell someone to do something, we need to communicate a couple of things:

  • who is going to do it (a subject like "Jack")
  • what is going to be done (a verb like "threw")

In addition, sometimes we need to communicate two more pieces of information:

  • to what they are going to do something (a direct object like "ball")
  • who will receive the effect of that action (an indirect object like "Jane")

When we put these items together, we get a sentence. Consider the following:

Jack threw the ball to Jane. We know that the person doing the task is the subject, Jack. What is being done is 'threw'. What is being thrown (or acted upon) is 'ball'. And the one receiving the effect, or output, of the action is 'Jane'. Computer languages are no different.

4. Picking an Argument - Part 2

Now, let us move from a simple statement to a command for Jack:

Throw the ball to Jane! Here, of course, one is telling Jack to do something, 'throw', to an object, 'the ball', with a desired recipient, 'Jane'. In programming, the computer is always the one that does things. What is to be done is, obviously, what you tell Jack, or the computer, to do. In most instances, however, if you do not tell the computer what you want it to act upon, it will not know what you mean. In this example, it will go looking for more information and find 'the ball'. However, the computer, being a bit clueless without your help, will still not know what to do. 'Throwing' carries a sense of direction with it, and the computer will want to know where you want the ball thrown. If the command was 'hold', Jack (and the computer) would know to simply sit still and hold the ball. But throwing requires direction and so one must give Jack (and the computer) an endpoint: Jane. Just like in human language, if we leave out any part of the sentence, confusion fills the gap.

These pieces of more information are what computer programmers call "arguments". Commands can require no arguments or several. Sometimes a command knows to assume information, a default setting, if other information is not given. Other times, however, the Python interpreter will look for more information, not find any, and quit without finishing the program (aka, "throw an error").

5. Picking an Argument - Part 3

In Python, the command to print is simply "print". Like the example of "throw" above, "print" needs an argument, something to print (e.g., "ball"). In Python, this is offset by quotations:

 print "ball" 
The output from this statement would be simply (note: no print command and no quotation marks):
ball By default, the output from print goes to the screen. There are ways to redirect the output, but we shall save that for a later tutorial.

6. Three Statements - Part 1

There are three kinds of statements in programming: assignment, control, and operation. I shall discuss each of them in greater depth in the respective tutorials on Python. In the next couple of pages, however, I will discuss their basic characteristics.

Assignment statements are perhaps the simplest of the three. In assignment, the value on the right side of the equal sign ('=') is assigned to a variable name on the left. The more common way of describing the statement "x = 25" is to say that 'x equals 25'. As long as x is equivalent to '25', one may refer to the variable 'x' instead of '25' wherever needed. However, one must be careful. If a program is dependent upon x being '25' and x is later assigned a new value, perhaps '26', then every calculation that relies upon x being equivalent to '25' will change, sometimes with unpredictable results.

7. Three Statements - Part 2

Control statements have the sole purpose of regulating the flow of the program. Like a traffic signal, a control statement, or loop, tells the computer whether to go or stop. It does this by expressing the conditions under which an action should occur. If the conditions are not met, the entire loop is ignored by the computer.

Operation statements are the lifeblood of a program. These statements tell the computer what to do. Like the example about Jack and Jane above, operation statements are commands to do an action on a certain input and to produce a certain output. Some commands expect to be told explicitly upon which input to act or where it should to direct the output. If this information is expected and not given, the command will be unclear to the computer, and the program will quit unexpectedly.

8. Summary

In summary, you should keep the following points in mind when you write your programs:
  • A computer does only what you tell it to do.
  • A computer program consists of data and code.
  • There are three kinds of statements: assignment, control, and operation.
  • The computer is the subject of every command in a program.
  • It reads your program looking for coherent statements made up of reserved words and arguments.
  • It then does what you tell it to do with the data.

©2013 About.com. All rights reserved.