Introd To 4gl
Introd To 4gl
Lesson I : Programming
Program
Short History
Just as you speak to a friend in a language so you 'speak' to the computer in a language.
The only language that the computer understands is called binary and there are several different
dialects of it - which is why that cool iMac program won't run on your PC and vice versa. Binary is
unfortunately very difficult for humans to read or write so we have to use an intermediate
language and get it translated into binary for us. This is rather like watching the American and
Russian presidents talking at a summit meeting - One speaks in English, then an interpreter
repeats what has been said in Russian. The other replies in Russian and the interpreter again
repeats the sentence, this time in English.
Surprisingly enough the thing that translates our intermediate language into binary is also
called an interpreter. And just as you usually need a different interpreter to translate English into
Russian than you do to translate Arabic into Russian so you need a different computer interpreter
to translate Python into binary from the one that translates VBScript into binary.
The very first programmers actually had to enter the binary codes themselves, this is
known as machine code programming and is incredibly difficult. The next stage was to create a
translator that simply converted English equivalents of the binary codes into binary so that instead
of having to remember that the code 001273 05 04 meant add 5 to 4 programmers could now
write ADD 5 4. This very simple improvement made life much simpler and these systems of
codes were really the first programming languages, one for each type of computer. They were
known as assembler languages and Assembler programming is still used for a few specialized
programming tasks today.
Even this was very primitive and still told the computer what to do at the hardware level -
move bytes from this memory location to that memory location, add this byte to that byte etc. It
was still very difficult and took a lot of programming effort to achieve even simple tasks.
Gradually computer scientists developed higher level computer languages to make the
job easier. This was just as well because at the same time users were inventing ever more
complex jobs for computers to solve! This competition between the computer scientists and the
users is still going on and new languages keep on appearing. This makes programming
interesting but also makes it important that as a programmer you understand the concepts of
programming as well as the pragmatics of doing it in one particular language.
A long time ago a man called Edsger Dijkstra came up with a concept called structured
programming. This said that all programs could be structured in the following four ways:
Sequence of Instructions
Here the program flows from one step to the next in strict sequence.
Branches
Here the program reaches a decision point and if the result of the test is true then
the program performs the instructions in Path 1, and if false it performs the actions in
Path 2. This is also known as a conditional construct because the program flow is
dependent on the result of a test condition.
Loops
In this construct the program steps are repeated continuously until some test
condition is reached, at which point control then flows past the loop into the next piece of
program logic.
Modules
Here the program performs an identical sequence of actions several times. For
convenience these common actions are placed in a module, which is a kind of mini-
program which can be executed from within the main program.
Other Features
Along with these structures programs also need a few more features to make them
useful:
Φ Data (raw materials)
Φ Operations (add, subtract, compare, etc)
Φ Input / Output capability ( display results)
Clearing some Terminologies
We already said that programming was the art of making a computer do what you want,
but what is a program?
In fact there are two distinct concepts of a program. The first is the one perceived by the
user - an executable file that is installed and can be run repeatedly to perform a task. For
example users speak of running their "word processor program". The other concept is the
program as seen by the programmer, this is the text file of instructions to the computer, written in
some programming language, that can be translated into an executable file. So when you talk
about a program always be clear about which concept you mean.
Basically a programmer writes a program in a high level language which is interpreted
into the bytes that the computer understands. In technical speak the programmer generates
source code and the interpreter generates object code. Sometimes object code has other names
like: P-Code, binary code or machine code.
The interpreter has a couple of names, one being the interpreter and the other being the
compiler. These terms actually refer to two different techniques of generating object code from
source code. It used to be the 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, translate and execute it.
Structure of a Program
The exact structure of the 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.
Statements – these are the core of your program. The statements actually manipulate
the data we define and do the calculations, print the output, etc.
Batch Programs
These are typically started from a command line and tend to follow a pattern of:
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.
In this configuration the program again starts off by setting up its internal state,
but then control is handed off to the event loop - which is usually provided by the
operating environment (sometimes referred to as the runtime). The program then waits
for the event loop to detect user actions which it translates to events. These events are
sent to the program to deal with one at a time. Eventually the user will perform an action
that terminates the program, at which point an Exit Event will be created and sent to the
program.
Programming
Computer programming (often shortened to programming or coding) is the process of
writing, testing, and maintaining the source code of computer programs.
Computer Programming is the art of making a computer do what you want it to do. At the
very simplest level it consists of issuing a sequence of commands to a computer to achieve an
objective.
In the early days of computing – back in the 1940s – using a computer required writing
programs, because there wasn’t any software for sale. And writing programs required writing out
the strings of ones (1) and zeros (0) that the computer could understand. There is still a small
amount of programming that’s done this way. It is known as programming in machine language,
because it involves writing codes that the machine understands.
In general, a programming language is any set of words or symbols used to write
instructions for a computer. Code is just a short word for program instructions.
It didn’t take long before programmers realized that they were writing certain strings of
machine language code over and over. They began inventing shorthand symbols to represent
these strings. The result of their efforts was the first symbolic computer languages, known as
assembly language.
Current Uses
1GL is mainly used on now very ancient computers, machine level programming still finds
a use in several areas of modern programming. First and foremost, any native-code compiler
creates machine language.
The primary niche for these languages are in kernels, device drivers, and system
libraries. Compilers usually use these as intermediate languages between a higher level
language and machine code. Besides those, they are almost never used directly.
let b = c + 2 * d
Fortran, ALGOL and COBOL are early examples of this sort of language. Most "modern"
languages (BASIC, C, C++, Delphi, Java, and including COBOL, Fortran, ALGOL) are third
generation. Most 3GLs support structured programming.
Features
There are three primary features of 3GL. First, they are largely machine-independent. In
other words, a program written in a 3GL is not written for a specific type of processor. If you write
a program in BASIC, you can use that program on a Mac, or a PC.
The second feature is that program require either an interpreter or a compiler to translate
the program into machine language. An interpreter is a program that translates the language
while the program is running. A compiler translates the program before it can be run.
Finally the 3GL are known as procedural languages because they force the programmer
a develop a structures series of procedures or steps to accomplish the goal.
In general then, a 3GL is a machine-independent procedural language that requires an
interpreter or a compiler.
All 4GLs are designed to reduce programming effort, the time it takes to develop
software, and the cost of software development. They are not always successful in this task,
sometimes resulting in inelegant and unmaintainable code.
Types