Computer Programming
Tadele M.
1
Course Contents
Chapter 1: Introduction to computer programming
Programming Languages: Introduction
Levels of programming languages
Language Translation and Interpretation
Major Programming Paradigms
Chapter 2: Algorithm Representation and Data Structure
Program design
Pseudo code
Flow chart
Pseudocode to Flowchart Conversion
2
Course Contents…
Chapter 3: Programming constructs
Process of compiling and running programs
Basic syntax and semantics of a high-level languages
Basic Elements of Programming Chapter 5: Functions
Structure (Anatomy) of C++ program • Function Basics
• Declaring functions
Input/output Statements
• Defining Functions
• Calling Functions
Chapter 4: Program Control
Sequential Statements
Selection Statements: if, if…else
Repetition or Loops: for, while, do...while
3
Chapter 1: Introduction to Computer
Programming
4
Computer programming
Computers are used for many different purposes in many
different situations.
5
Computer programming
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.
6
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
So the programmer will have to decide which one to use for
7
solving a particular problem.
Levels of programming languages
Programming languages are classified into five major
categories:
first generation languages (machine languages),
second generation languages (assembly languages),
third generation languages,
fourth generation languages, and
Fifth generation languages (natural languages).
Machine and assembly languages are referred to as low level
languages; third generation, fourth generation and natural
languages are categorised as high level languages.
8
Continued…
A low level language is machine dependent; that is, it is
written to run on one particular computer.
These are closer to machine code (binary) and are often
hardware-specific.
A high level language is machine independent, which means
the high level language code can run on many different types
of computer.
These are closer to human languages and are more abstracted
from the hardware.
There are two types of low-level programming languages:
Machine Language and Assembly Language.
9
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.
10
Characteristics of 1GL
Fastest to execute because it is already in the language that the
computer can understand
Difficult to interpret, write and understand (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
Machine dependent/ Hardware-Specific
Programing becomes more difficult as the complexity of the
program increases
11
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:
MOV (move data)
LDA (Load data)
ADD (add two values)
SUB (subtract one value from another)
12
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
STR B, A : store contents of accumulator A into
storage register B
NOP : no operation (stop here)
13
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.
Easier to write than machine language
Assembly languages have to be translated into machine language
by language translators known as assemblers for the processor
to understand.
The code is not very easy to understand, hence the introduction
of high level programming languages.
14
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 of:
Third Generation Languages (3GL), Fourth Generation
Languages (4GL), Fifth Generation Languages (5GL)
15
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: C, C++, Java, Pascal.
16
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.
In SQL, you specify what data you want to retrieve (e.g., "Select all
records where age is greater than 30"), and the database
management system decides how to execute that query efficiently.
17 Examples: SQL, MATLAB, SAS.
Fifth Generation (1990’s):
5GLs are “natural” languages whose instruction closely resembles
human speech. E.g. voice-activated assistants:
"Alexa, what's the weather today?“
"Alexa, remind me to call Mom at 3 PM."
These are used in artificial intelligence (AI) and expert systems;
also used for accessing databases.
5GLs require very powerful hardware and software because of the
complexity involved in interpreting commands in human
language.
Examples: Prolog, natural language interfaces
18
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.
19
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.
20
Assembly Assembler Machine
Language Language
Source Machine
Compiler
Program 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.
21
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.
22
How a Compiler and Interpreters works
23
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.
24
Major Programming Paradigms
The development of programming languages has been marked
by significant milestones, each introducing features and
properties that have shaped the evolution of programming.
Among these the following are worth mentioning:
Procedural,
Structured and
Object Oriented Programming Paradigms.
25
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
26
II. Structured Programming
It is a subset of procedural programming that enforces a logical
structure on the program being written to make it more efficient
and easier to understand and modify.
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.
27
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.
28