Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gauld A.Learning to program (Python)_1.pdf
Скачиваний:
23
Добавлен:
23.08.2013
Размер:
1.34 Mб
Скачать

case that compilers produced object code that could be run on its own (an executable file - another term) whereas an interpreter had to be present to run its program as it went along. The difference between these terms is now blurring however since some compilers now require interpreters to be present to do a final conversion and some interpreters simply compile their source code into temporary object code and then execute it.

From our perspective it makes no real difference, we write source code and use a tool to allow the computer to read and execute it.

The structure of a program

The exact structure of a program depends on the programming language and the environment that you run it on. However there are some general principles:

A loader - every program needs to be loaded into memory by the operating system. The loader does this and is usually created by the interpreter for you.

Data definitions - most programs operate on data and somewhere in the source code we need to define exactly what type of data we will be working with. Different languages do this very differently. All of the languages we will use allow us to create a new data definition just by using the data, we'll see what I mean in the next section!

Statements - these are the core of your program. The statements actually manipulate the data we define and do the calculations, print the output etc.

Most programs follow one of two structures:

Batch programs

These are typically started from a command line (or automatically via a scheduler utility) and tend to follow a pattern of:

Initialize internal data

Read input data

Process that data

Print or store results

Event driven programs

Most GUI systems (and embedded control systems - like your Microwave, camera etc) are event driven. That is the operating system sends events to the program and the program responds to these as they arrive. Events can include things a user does - like clicking the mouse or pressing a key - or things that the system itself does like updating the clock or refreshing the screen.

Event driven programs generally look like:

Initialize the internal data

Wait for events to arrive

Identify an incoming event and react accordingl

13

Points to remember

Programs control the computer

Programming languages allow us to 'speak' to the computer at a level that is closer to how humans think than how computers 'think'

Programs operate on data

Programs can be either Batch oriented or Event driven

14