Chapter 1-Introduction To Computer Programming
Chapter 1-Introduction To Computer Programming
Programming
Tadele M.
1
Computer programming
Computers are used for many different purposes in many different
situations.
For versatility they can be programmed.
The ability for a computer to be programmed allows it to do
whatever their programs tell them what to do.
A program is a set of instructions that tell a computer what to do.
A computer cannot do anything unless it has a program to tell it what
to do.
Computer programming is the act of writing computer
programs, which are a sequence of instructions written using a
Computer Programming Language, to perform a specified task by
the computer.
2
Programming Languages
Programming languages are the vocabulary and set of
grammatical rules for instructing a computer to perform
specific tasks.
There are many different types of programming languages
each having a unique set of keywords (words that it
understands) and a special syntax (grammar)for organising
program instructions.
The following are a few of them:
Java
PHP
C
Perl
C++
Ruby
Python
4
Continued…
A low level language is machine dependent; that is, it is
written to run on one particular computer.
A high level language is machine independent, which means
the high level language code can run on many different types
of computer.
There are two types of low-level programming languages:
Machine Language and Assembly Language.
5
Machine language – First Generation
Language (1GL), 1940’s
The machine language writes programs using the machine
code of 1s and 0s, which is directly understood by the
computer.
The main problems with using machine code directly are that
It is:
Very easy to make a mistake, and
Very hard to find it once you realise the mistake has been made.
6
Characteristics of 1GL
Fastest to execute because it is already in the language that the
computer can understand
Difficult to interpret (requires the aid of a reference manual to
interpret the meaning of each code)
Easy to make mistakes in the sequence of 1s and 0s; replacing a 1 for
a 0 can result in the wrong command/instruction being executed
It is difficult to identify mistakes made
Time-consuming and tedious to write
Machine dependent
Programing becomes more difficult as the complexity of the
program increases
7
Assembly language – Second Generation
Language (2GL), early 1950’s
Assembly language is written using mnemonic codes
(abbreviated English words) or short codes that suggest their
meaning and are therefore easier to remember.
These codes represent operations, addresses that relate to
main memory, and storage registers of the computer. Typical
codes might be: LDA, STO, ADD, NOP, etc.
8
Continued…
An example of a program code to add and store two numbers
would be:
LDA A, 20 : load accumulator A with the value 20
ADD A, 10 : add the value 10 to accumulator A
STO B, A : store contents of accumulator A into
storage register B
NOP: no operation (stop here)
9
Characteristics of 2GL
As with machine language, assembly language is machine
dependent.
Assembly language, being machine dependent, is faster and
more efficient in the use of hardware than high-level
programming languages.
Assembly languages have to be translated into machine
language by language translators known as assemblers for
the processor to understand.
Easier to write than machine language
The code is not very easy to understand, hence the
introduction of high level programming languages.
10
High-level programming languages
High level programming language is defined as one that is machine
independent and uses variables and objects, Boolean expressions,
functions, loops, arrays, threads, locks which are similar to their
meaning (abstraction).
High-level languages have evolved over the years and can be
grouped into five categories: Third Generation Languages
(3GL), Fourth Generation Languages (4GL), Object Oriented
Programming Languages (OOP), Fifth Generation Languages
(5GL) and Scripting Languages
11
Third Generation Language:1950’s
to 1970’s
Uses English like instructions and mathematicians were able
to define variables with statements such as Z = A + B
Such languages are much easier to use than assembly
language.
Programs written in high level languages need to be
translated into machine language in order to be executed.
All third generation programming languages are procedural
languages.
In procedural languages, the programmer is expected to
specify what is required and how to perform it, i.e. People-
Oriented Programming Languages
Examples: COBOL, FORTRAN, Basic (old version not
12
new), Pascal, C
Fourth Generation (since late 1970’s)
Aimed at simplifying the programmers task of imparting
instructions to a computer .
Forth generation languages are non-procedural languages.
In non-procedural languages the programmer concentrate on
defining the input and output rather than the program steps
required.
Some examples of 4GL are structured query languages (SQL),
report generators, application generators and graphics
languages.
For example, the Microsoft Office suite supports macros and
ways to generate reports.
13 Some popular examples: C++, Java
Fifth Generation (1990’s):
These are used in artificial intelligence (AI) and expert
systems; also used for accessing databases.
5GLs are “natural” languages whose instruction closely
resembles human speech. E.g. “get me Jone Brown’s sales
figure for the 1997 financial year”.
5GLs require very powerful hardware and software because
of the complexity involved in interpreting commands in
human language.
14
Characteristics of high-level languages
They are machine independent hence portable
They are user friendly and easy to learn
High-level language programs are easy to debug
They are more flexible hence they enhance the creativity of the
programmer, increasing productivity
They are executed much slower than low-level programming
languages
They have to be translated into machine code before execution, this
is done by Compilers and Assemblers.
One instruction translates into several machine code instructions
Examples of high-level programming languages in active use today include
15
Python, Visual Basic, Delphi, Perl, PHP, Ruby, C++,C#, Java,…
Language Translators
Any program written in a language other than machine
language needs to be translated to machine language because:
The computer does not understand assembly languages or high-
level languages.
Computers work in machine code or machine language.
Language translators are system programs that convert
assembly language and high-level language into the machine
language.
There are different kinds of translator software, among which
compiler, interpreters , and assemblers are interest for most
programmers.
16
Source Machine
Compiler
Program Language
Assembly Machine
Assembler
Language Language
Source code
Source code is a program code of either an assembly language
or high-level language that the programmers write in a
program editor.
Source code cannot be understood by the computer until it
has been translated into machine code.
17
Compilers and Interpreters
Compilers: a compiler is a computer program that translates a series
of statements written in source code (a collection of statements in a
specific programming language) into a resulting object code
(translated instructions of the statements in a programming
language).
A compiler changes or translates the whole source code into executable
machine code (also called object code) which is output to a file for latter
execution. E.g. C++, Pascal, FORTRAN, etc.
The object code (machine code) stored on disk has an EXE file name
extension. It is then loaded or linked (stored in memory) and executed
Interpreters: is a computer program that translate a single high level
statement and executes it and then goes to the next high level
language line etc. E.g. QBASIC, Lisp etc.
Interpreters are slower than compilers because interpreters convert a
statement at a time and runs it before moving to the next line.
18
The machine codes are not saved after execution.
How a Compiler and Interpreters works
19
Assemblers
The assembly-language program must be translated into
machine code by a separate program called an assembler.
The assembler program recognises the character strings that
make up the symbolic names of the various machine
operations, and substitutes the required machine code for
each instruction.
20
Assemblers
The assembly-language program must be translated into
machine code by a separate program called an assembler.
The assembler program recognises the character strings that
make up the symbolic names of the various machine
operations, and substitutes the required machine code for
each instruction.
21
Major Programming Paradigms
The major land marks in the programming world are the
different kinds of features or properties observed in the
development of programming languages.
Among these the following are worth mentioning:
Procedural,
Structured and
Object Oriented Programming Paradigms.
22
I. Procedural Programming
Procedural programming is a programming paradigm based
upon the concept of procedure call.
Procedural programming is often a better choice than simple
sequential programming in many situations which involve
moderate complexity or which require significant ease of
maintainability.
Possible benefits: the ability to re-use the same code (function or
procedure) at different places, an easier way to keep track of
program flow
23
II. Structured Programming
Process of writing a program in small, independent parts.
This makes it easier to control a program's development and to
design and test its individual component parts.
Structured programs are built up from units called modules,
which normally correspond to single procedures or functions.
Can be seen as a subset or sub discipline of procedural programming
24
III. Object-Oriented Programming
The idea behind OOP is that, a computer program is composed of
a collection of individual units, or objects.
Object-oriented programming is claimed to give more flexibility,
easing changes to programs.
This approach is often simpler to develop and maintain.
25
What skills do we need to be a programmer?
For someone to be a programmer, in addition to basic skills in
computer, needs to have the following major skills:
Programming Language Skill: knowing one or more
programming language to talk to the computer and instruct the
machine to perform a task.
Problem Solving Skill: skills on how to solve real world problem
and represent the solution in understandable format.
Algorithm Development: skill of coming up with sequence of
simple and human understandable set of instructions showing the
step of solving the problem. Those set of steps should not be
dependent on any programming language or machine.
26