0% found this document useful (0 votes)
425 views

Programming Languages

Programming languages can be categorized as either low-level or high-level languages. Low-level languages like machine language and assembly language are closer to machine code and allow direct access to memory addresses but are difficult for humans to read and write. High-level languages like BASIC, COBOL, and Pascal use English-like syntax that is easier for humans but require compilation to machine code. Programming languages can also be classified based on their programming paradigm, such as imperative/procedural languages which specify step-by-step instructions or declarative languages which specify rules and facts.

Uploaded by

ziggatron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
425 views

Programming Languages

Programming languages can be categorized as either low-level or high-level languages. Low-level languages like machine language and assembly language are closer to machine code and allow direct access to memory addresses but are difficult for humans to read and write. High-level languages like BASIC, COBOL, and Pascal use English-like syntax that is easier for humans but require compilation to machine code. Programming languages can also be classified based on their programming paradigm, such as imperative/procedural languages which specify step-by-step instructions or declarative languages which specify rules and facts.

Uploaded by

ziggatron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PROGRAMMING LANGUAGES

- A programming language is a set of symbols in computer language that are used in coding
computer programs.
- A programming language is a specially written code used for writing application programs e.g.
C, Pascal, COBOL, BASIC, Visual Basic, C++ and Java (Originally for intelligent consumer-
electronic devices (cell phones), then used for creating Web pages with dynamic content, now also
used for developing large-scale enterprise applications)
- Program: a set of detailed and unambiguous instructions that instructs a computer to perform a
specific task, for example, to add a set of numbers.
- Programming: A process of designing, coding and testing computer programs
- Programmer: A person who specialises in designing, coding and testing computer programs
- Problem: any question or matter involving difficulty or uncertainty and is proposed for a computer
solution.

TYPES OF PROGRAMMING LANGUAGES


1. Low Level Languages (LLL):
- These are programming languages used to write programs in machine code (i.e in 1s and 0s) or in
mnemonic codes.
- Low level languages are machine oriented (machine specific).
- Low level language is in two forms:
(a) Machine Language and
(b) Assembly Language.

a. Machine code (language)


- Is the language used to write programs in binary form (1s and 0s).
- Machine code executes without translation.
- Machine language has the following advantages:
 Programs run faster since they are already in computer language. There is no need for
conversion as programs are in machine language.
 Programs occupy very small disc storage space by storing just 1s and 0s.

Disadvantages of Machine language:


- They are very difficult to learn.
- They are difficult to understand.
- Very difficult to use and therefore very few programmers use them these days.
- It takes too long to debug and therefore is prone to some errors.
- It takes too long to develop working programs.
- They are machine dependent (they can only work on type of computer designed for and not
work on other computers)

b. Assembly Language:
- These are programming languages that use mnemonic codes in coding programs.
- Mnemonic codes are abbreviations used to represent instructions when coding assembly language
programs, for example, LDA for Load, ADD for Addition, etc.
- One assembly language statement is equivalent to one machine code instruction and therefore
programming lengthy and time consuming.
- However, assembly language programs are efficient.
- Programs also run faster as they are closer to machine language and therefore are used in
designing programs that needs efficient timing, e.g. games like chess, operating systems, etc.
- Assembly language is used when there is need to access registers and memory addresses directly.
- Assembly language instructions also occupy very little disc storage space.

Compiled by Kapondeni T. Page 1 of 27


- Mnemonic codes are very close to machine code, hence are low level language assembly language
codes.
- They however run on specific computer architecture since they are hardware aligned.
- They also contain different forms of instruction, e.g. jump, control, arithmetic, etc.
- Assembly language allows immediate, direct and other forms of memory addressing.
Application: Assembly language is used in:
- Coding operating systems
- Coding device drivers
- Coding programs for embedded systems like DVD players, decoders, etc.
- Coding encryption software

Advantages of Assembly language:


- One assembly language instruction corresponds to one machine code instruction and therefore
translation is easier and faster to code.
- Programs run faster since they are close to machine code.
- They occupy very small disk storage space hence are economical to use.
- Easier for a programmer to use than machine language.

Disadvantages of Assembly Language


- They are very difficult to learn.
- They are very difficult to understand.
- Takes too long to develop working programs.
- They can be machine dependent (machine oriented) unless the machines use the same
processor chip.

2. High Level Languages (HLL):


- These are programming languages that use English-like statements in coding programs, for
example COBOL, Pascal, BASIC, etc.
- High Level languages are mostly used for developing user applications like stock control systems,
personnel records, etc.
- There are so many high level languages because of competition from designers who want to
outpace each other.
- It can also be due to the fact that we have so many application areas in real life so each high level
language is designed for a specific problem (problem oriented/problem specific) to be solved in
our daily lives, for example BASIC was designed for learning purposes, COBOL for business
applications, FORTRAN for scientific purposes, etc.
- High Level languages are independent of the architecture of the computer.
- One statement is translated into several equivalent machine code instructions before it is executed.
- Below is an example of a BASIC program that accepts two numbers entered through the keyboard,
adds them and display the result on the screen:
INPUT “ENTER FIRST NUMBER.”, A
INPUT “ENTER SECOND NUMBER.”, B
SUM = A + B
PRINT SUM
END

- Programs written in High Level Language are first converted to machine code before running.
High level languages have the following features:
- Problem oriented (Machine independent): they are designed to solve an application problem and
therefore runs on any machine
- They are portable: they can be transferred from one machine to another and run without problem.
- Instructions are written in English statements which are easier to understand.

Compiled by Kapondeni T. Page 2 of 27


Facilities of High Level Languages
The following facilities of high level languages are not found in low level languages
- Selection structures
- Iteration structures
- Built-in routines to simplify input and output, e.g. INPUT, PRINT, PRINTLN, etc in BASIC.
- Built-in functions like sqr, sqrt, val, etc.
- Data structures like strings, arrays, records, etc.
- User-defined functions.

Advantages of High Level Languages:


- They are easier to understand since they are written in English-like statements which are more
readable.
- They are easier to learn.
- It is easier to work with, that is to correct errors (debug) and to test programs.
- They are problem oriented and therefore can be used on any computer (not machine
dependent)

Disadvantages of HLL
- Takes long to run since they need to be first converted to machine code.
- They occupy a lot of disk storage space as compared to low level languages.

Types Of High Level Languages/Programming Paradigms


- Programming paradigm refers to methods used to categorise high level languages in terms of
organising principles used by the designers. It is pattern that serves as a school of thought for
programming of computers.
- Such programming paradigms are as follows:
 Imperative/procedural languages
 Declarative languages
 General purpose languages
 Special purpose languages
 Object oriented languages (OOP)

1. Imperative /Procedural Programming languages


- These are high level programs in which the programmer specifies how the program is to be
executed, following a predefined execution sequence.
- The programmer has to write instructions (in source code) that are to be strictly followed when
performing a certain task.
- The programmer shows steps in the order of execution, providing a set of data.
- The order of statements is very important and is to be obediently followed by the computer when
performing a task.
- Examples include C, Pascal, COBOL, BASIC, etc.
- Imperative languages have the following features:
 built-in data types like integers, character, string, Boolean, etc
 user defined data types like records, arrays, etc
 Declarations for variables, arrays, constants, functions, procedures, etc.
 Programming statements like assignments, control structures, procedure calls, function
calls, etc.

2. Declarative Programming Languages


- These are programming languages that states what is to be done rather than how to do it.
- The programmer states the facts and rules associated with the problem.
- The order of stating the rules and facts is not important.
Compiled by Kapondeni T. Page 3 of 27
- Adding new rules, modifying existing ones and deleting some rules is very easy.
- They do not use loops, selection structures or procedures.
- Can also be called Very High Level programming languages or 4GL languages.
- They have the following benefits:
- Makes it easier to program computer applications due to the following reasons:
 Allow alternate methods of developing software, e.g. prototyping.
 One 4GL code is equivalent to several lines of 3GL languages code.
 Programmer productivity is increased.
Application: mostly used in designing expert systems.
Examples of declarative languages are Prolog (Programming in Logic)

3. General purpose languages


These are languages that can be used to perform a variety of takes, they are not dedicated to one
particular task.

4. Special purpose languages


- These are languages that are dedicated to a specific area, for instance, for running on embedded
computer systems like washing machines, mobile phones, controlling robots in a car assembly,
etc.
- such languages must support real-time facilities, and have the following facilities:
 Real-time control facilities: Programmer specifies times/frequency at which certain
actions are to be performed, e.g. taking readings after every 3 seconds for sensors.
Programmers must also program timeout if the sensor fails to take reading in the specified
interval and treat this as a fault and take appropriate action.
 Interaction with hardware interfaces: language must contain instructions to monitor
devices like sensors and control actuators.
 Ability to support concurrent programming: languages must allow two or more
actions to take place at the same time.

5. Object Oriented Programming languages (OOP)


- These are programming languages that use objects that stored both data and code for the
operations that may be applied on the data.
- Objects are data items with all the processing possible on the data item attached to it. (have states
and methods)
- OOP languages include Visual Basic, C++, Java, Object Pascal, Common Lisp, Scheme, Perl,
Python, Ruby, VB.Net and Oz etc.
- OOP languages support the following features:
 Encapsulation
 Inheritance and
 Polymorphism
Identifying the state and behaviour for real-world objects is a great way to begin thinking in terms of
object-oriented programming.

What Is an Object?
Objects
- are real world items that have state (attributes) and behaviour (operations) and belong to certain
class, e.g. desk, car, television set, bicycle, etc.
- In general, an object is an instance of a class and is an actual real-world entity.

State:

Compiled by Kapondeni T. Page 4 of 27


- These are attributes/characteristics/fields for an object, e.g. a car has colour, registration
number, model, etc.
- Bicycles have state of current gear, current speed, etc.
- Each object stores its own state.
- Dogs have state (name, colour, breed, hungry).

Behaviour (operations):
- Refers to methods that can be used on each object state, e.g. changing gear of a car, applying
brakes, etc.
- Each class has its source code that is associated with it and defines the fields and methods.
- Dogs have behaviour (barking, fetching, wagging tail).

Class:
- A set of objects which share a common data structure and a common behaviour.
- In coding a program, a class is taken as an abstract data type that describes the fields and methods
of the class.
- Each class has different access levels, which can be private, protected or public.
- Example of class declarations in Java are as follows:

class Bicycle {
int speed = 0;
int gear = 1;
void changeGear(int newValue) { gear = newValue; }
void speedUp(int increment) { speed = speed + increment; }
void applyBrakes(int decrement) { speed = speed - decrement; }
void printStates() {System.out.println(speed:"+speed+" gear:"+gear); }
}

- Bicycle is the name of the class


- Speed, gear are the states (fields) for the Bicycles class.
- ChangeGear, speedup, applyBrakes, printStates are the behaviour (methods) of the class. All
bicycles have the states and operations above.
- The following coding is for the creation of objects for the class Bicycle.

Bicycle bike1 = new Bicycle();


Bicycle bike2 = new Bicycle();
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.speedUp(10);
bike2.changeGear(2);
Compiled by Kapondeni T. Page 5 of 27
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}

- bike1 and bike2 are objects of the class Bicycle.


- Their behaviour and states are the same (though they may have different values) so they
belong to the same class.

Encapsulation
- This is a technique of combining operations (methods) and data (fields) into one unit as in classes.
- Encapsulation can be in two forms:
(a) Data encapsulation: Hiding states internally and requiring all interaction to be performed
through an object's methods. It involves restricting access to the states (fields).
(b) Procedural encapsulation: users do not need to know how the behaviour happens, that is
hiding operations from the user.

Inheritance
- In Object-oriented programming, inheritance is whereby classes can re-use (assume) commonly
used state and behaviour from their parent (base) class.
- Inheritance is the ability of a class to use the variables and methods of a class from which the new
class is derived (parent class)
- Inheritance allows a new class to be derived from an existing class.
- Inheritance therefore is a relationship among classes, where a sub-class (derived class) shares all
the fields and methods of the base (parent) class, plus its own methods and states.
- Consider the inheritance diagram below:

- Base Class: This is the parent class or the first class to be created from which other class can
inherit states and methods.
- Derived class: These are new classes that are created from the base class, and therefore have
methods and states of the base class plus their own methods and states.
- The syntax for creating a subclass is use the extends keyword, followed by the name of the class to
inherit from:
class MountainBike extends Bicycle
{
// new fields and methods defining a mountain bike
// would go here
}

Polymorphism

Compiled by Kapondeni T. Page 6 of 27


- In general, polymorphism is the ability to have the same operation performing differently in
different circumstances.
- Polymorphism allows an operation to perform differently depending on the parameters that are
passed.
- This is the ability of classes to use the same name in the class hierarchy for a method but each
class implementing the method differently.
- In polymorphism, derived classes are able to re-define some of the base (super class) methods.

Containment/aggregation/composition
- These are links/associations between objects that allow them to communicate.
- For instance: a form on a screen is an object. On the object form, there are other objects, e.g. delete
button, display button, exit button, etc.
- These buttons (which are objects) communicate with the form (another object).
- Thus the linkage between the form and the buttons is called the containment.

Event driven programming


- This is whereby the sequence of code execution is determined (triggered) by external events or by
user actions, e.g. clicking a button on a form is an event that can form a program to execute certain
sections of the code.
- Thus clicking a button or menu item is an event.
- This is important because the programmer cannot predict the exact sequence a user can perform
his/her tasks.

Event handlers: small program codes which are invoked (called) in response to external events.
Dispatcher: small program codes that call event handlers so that events can be processed.

Advantages of OOP
Grouping code into individual software objects provides a number of benefits, including:

- Modularity: The source code for an object can be written and maintained independently of the
source code for other objects. Once created, an object can be easily passed around inside the
system.

- Information-hiding: By interacting only with an object's methods, the details of its internal
implementation remain hidden from the outside world.

- Code re-use: If an object already exists, you can use that object in your program.

- Easy debugging: If a particular object turns out to be problematic, you can simply remove it from
your application.

- Reliability: if codes are designed by specialists, they are more likely to free from errors due to
intensive testing.

- Time saving: re-use of existing methods and states means less time needed to code programs.

- Decreased maintenance costs: programmers have les code to maintain.

- Smaller program codes: since the states and methods of classes are re-used, the code of the
program is smaller, taking less disk storage space.

- Storage structures of an object may be altered without affecting the programs that make use of it.

Compiled by Kapondeni T. Page 7 of 27


Source Code and Object Code
Source Code
- Refers to the program instructions written in English-like statements (High Level Language) by
the programmer, e.g. the Visual basic Code as typed by the programmer.
- Source code is human understandable language and cannot be understood by the computer.
- This therefore needs to be translated into machine code (binary) which the computer can
understand.

Object Code
- Refers to a machine code version of a source code.
- All programs written in source code must be converted to object code for the computer to
understand them.

TRANSLATORS
- These are programs used to convert High level Language programs as written by the programmer
(source code) into machine code that the computer can execute.
- Translators are in three types, which are assemblers, interpreters and compilers, which are
further explained below:

1. Assemblers:
- These are programs used to translate assembly language instructions (program) into machine
language instructions before execution.
- Since each computer has its own assembly language, it also has its own assembler.
- Assembler programs are written either using assembly language, or using high level languages like
C.
- Assemblers are simpler to program as compared to compilers.
- Assemblers are in two types: One-Pass Assemblers and Two-Pass Assemblers
 One-Pass Assemblers
- These go through the source code once and assume that all the symbols will be defined before
any instruction that reference them. This is equivalent to declaring variables at the top of the
source code regardless of where they are used in a high level language program.
- It has an advantage of speed since only one phase in gone through

 Two-Pass Assemblers.
- Creates a table with all the symbols and their values in the first pass and then use the table in
the second pass to generate code.
- Has an advantage that symbols can be defined at any stage in the source code.
- Two-pass assemblers make programs easier to read and to maintain.
Uses of Assemblers
- The uses of assemblers include:
 They generate machine code that is equivalent to assembly language.
 They are used to check the validity of instructions, that is, checking for syntax errors in an
instruction.
 They also assign memory locations to variables.

2. Interpreters
- These are programs that translate and run one (command) instruction of a program at a time
before going to the next instruction until the end of the program, e.g. the BASIC interpreter.
- Interpreter must be present to run the program. It is used during program writing (coding) because
it easily aids in debugging.
- An interpreter translates one instruction at a time and then executes it.
- They do not produce the machine code version (object code) of a program; hence translation is
repeated every time the program is executed.
Compiled by Kapondeni T. Page 8 of 27
- If the program is run 100 times, translation for each instruction is also carried out 100 times.

Functions of Interpreters
- They translate each instruction in turn into machine language and run it.
- Allocates storage space to variables.
- They check syntax error in a program statement.
- Gives error messages to the user
- Finds wrong and reserved words of the program
- Determines wrong use of variables

Advantages of interpreters
- It is easy to find and correct syntax errors in interpreted programs.
- There is no need for lengthy recompilation each time an error is discovered.
- It is very fast to run programs for the first time.
- Allows development of small program segments that can be tested on their own without
writing the whole program.
- It is easier to partially test and debug programs, especially during the programming stage.
- It is very fast to run small programs.
- individual segments can be run, allowing errors to be isolated
- running will be necessary after very minor changes
- continual compilation of whole code is wasteful/time consuming

Disadvantages of interpreters
- They are very slow in running very large programs.
- They do not produce an object code of a source code and hence difficult to use since
conversion takes place every time the program is run.

3. Compilers
- These are programs that convert a high level language program into its machine code equivalent at
one go (at once) and then run it, e.g. the COBOL compiler.
- Compiler must be present for compiling the program.
- Once compiled, the program no longer needs conversion since the machine code version is the one
that will be run, until some changes are made to the program code.
- Compilers run faster when called and therefore may be held as library routines.
- Once compiled, the program can then be run even on a computer without the compiler since the
program will already be in machine code.
- The compilation processes involves many complex stages which will be looked later in this course.

Functions of Compilers
- They check syntax errors in program statements.
- They allocate storage space to variables.
- Translate the whole program into machine code at one go.
- Run object code of the program.
- Produces a program listing which indicates position of errors in a program.
- Gives error messages to the user
- Finds wrong and reserved words of the program
- Determines wrong use of variables

Advantages of Compilers
- The object code can be saved on the disc and run when needed without the need for
compilation.
- Compiled programs run faster since only the object code is run.

Compiled by Kapondeni T. Page 9 of 27


- The object code can run on any computer, even those without the compiler. Therefore
compiled programs can be distributed to many users and used without any problems.
- The object code is more secure since can cannot be read without the need for reverse
engineering.
- Compilers indicate the line numbers with syntax errors and therefore assist programmers in
debugging programs.
- They are appropriate even for very large programs.

Disadvantages of Compilers
- Slower than interpreters for running programs for the first time.
- They can cause the computer to crash.
- Difficult to find errors in compiled program.
- There is need for lengthy recompilation each time an error is discovered.

Difference between High Level Languages and Low Level Languages

High Level Language Low Level Language


1 Written in English like statements Written in 1s and 0s (machine code) or in
mnemonic codes.
2 Easier to work with Difficult to work with
3 Easier to understand Difficult to understand
4 Are problem oriented and can be used on Machine oriented
any computer
5 Slower in execution since they need to be Faster in execution since they are in machine code
first converted to machine code before already.
running
6 Occupy large disk storage space on the Occupy small disk storage space on the computer
computer
7 They are machine independent They are machine dependent

3. Very High Level Languages (VHLL) – 4GLs:


- These only specify the desired end result and do not indicate steps that the computer needs to
take to make the calculation, e.g. SQL used in relational databases.
- 4GLs are flexible and easy to use since no coding is required.

4. Natural Language – Artificial Intelligence and expert systems:


These are programs that mimic human reasoning and learn from experiences.

Programming Language Generations


1. First Generation Languages (1951-58) – Machine language: These used binary form to code
programs. This is the Machine language version. Programming was tedious to do.
2. Second Generation Languages (1959-64) – Assembly Language: Assemblers, compilers and
interpreter became available to represent machine code. This generation saw the use of assembly
language. Programming was simpler and less tedious.
3. Third Generation Languages (1965-70)- High Level Languages: This saw the development of
High Level Languages like BASIC, COBOL, FORTRAN, etc
4. Fourth Generation Languages (1971) – Very High Level Languages (4GLs): This saw the
development of non-procedural languages like SQL, PARADOX, etc.
5. Fifth Generation Languages(1981) - Natural Language, artificial intelligence, expert systems like
PROLOG, LISP.

Compiled by Kapondeni T. Page 10 of 27


Features of High Level Programming Languages
1. Programming Constructs
These are the basis from which high level languages are built. Programming constructs
includes:
(a) Control Structures
(i) If – Then – Else Construct
(ii) Case statement
(b) Looping Structures
(i) For – Next construct
(ii) Repeat – Until construct
(iii) While – Endwhile
2. Operators
Operators are used to manipulate data and they can be
(a) Arithmetic operators
e.g. b+c-d*e/f
(b) Logical operators
And, or, Not
(c) Assignment operators
=
(d) Comparison/relational operators
>, <, <=, >=, <>, Is
3. Identifiers
- An identifier is a unique label of a data item of element of a program and comprises of one or
more characters.
- Identifiers includes variable names, procedure names, constants, etc. Identifiers must not be
reserved words.
- Identifiers can be user defined as long as they are not reserved words.
4. Constants
- A constant is a data item (variable) whose value does not change during program execution.
- Its value is fixed.
- Constants are used to represent data items with fixed values, e.g. the value of pi. In VB 6.0, a
constant is declared as shown below:

Public Sub compute_interest()


Const pi=3.14159
Dim rs As New Recordset
Dim rs_loanpay As New Recordset
……………..
……………….
……………..
End Sub

If the constant is a string value, it must be enclosed in quotes, e.g.


Const Name = “Kapondeni”
In this case, the value of name will never change (when Name is declared as a constant)

5. Variables
- A variable is a name given to a memory location that stores a certain value, which may change
(the value) during program execution.
- Variables can be field identifiers, e.g. surname is a valid variable name.
- Variables must not be reserved words.
- Variables must be unique in the procedure or program (if all are global).

Compiled by Kapondeni T. Page 11 of 27


- Variables are declared at the beginning or at some point inside the program code. Every
variable must be declared before use, otherwise an error is generated.
- Variable names, as are all identifiers, start with an alphabetic character.
- They can be one character or a string of characters.
- Variable names can be alphanumeric (combination of alphabetic and numbers).
- They must be one word and must be related to the data stored in them so that the programmer
cannot be confused, e.g. Surname should be variable that stores a surname.
- If two words are used as a variable, they must be joined by and underscore ( _ ), with no
spaces between the words, e.g. Student_Surname, NOT Student-Surname.
- Alternatively, one may join the words as follows, StudentSurname.
- Variable names should not be too long, 8 characters are ok although VB supports longer
variables names but must not be more than 255 characters.
- Variables can store numeric, character or string values and must be declared appropriately.
- In Visual Basic 6.0, variables must be declared first before they are used. The keyword Dim is
used to declare variables, and each variable should have a data type, e.g.

Dim number As Integer


Dim Name1 As String
Dim x, Number_1, AmountOwed As Integer
- The scope of variables can be global or local.
- Scope of a variable refers to the extent (within the program) that the variable is recognised
(used), especially in relation to other modules and functions within the program.
- Constants can also be declared as global or local.

a. Global variables
- These are variables that are accessed and can be used by any procedure or function within the
same program.
- They are public variables
- The value of the variable exists throughout the program.
- Global variables are declared outside the procedure.
- In VB 6.0, global variables are declared as follows:
Public Sname As String
The word Public implies that it is a global variable.

b. Local variables
- These are variables that are defined within a procedure and that are accessible just within the
procedure they are declared.
- They are defined within the procedure.
- They are private variables
- The value of the variable only exists within the procedure it is defined.
- They are therefore local to that procedure in which they are declared and therefore cannot be
used (not accessible) by other procedures.
- Local variables are declared as follows:
Private Sname As String

Syntax diagram of a variable:


- Syntax diagrams are graphical representations of the structure of valid variables.
- They convey what is legal for variable(or identifiers) in a certain programming language as shown
below:

Compiled by Kapondeni T. Page 12 of 27


- The above diagram shows that every variable name starts with a Letter, followed by any of the
following (Letter, digit or _ ) at any position, or a mixture of both in any order, as long as the
first character is a letter.
- Using the diagram above, a variable like 3_Name, is invalid since it starts with a number.
- variable names must follow the rules of the language
- the translator tries the rules against the variable names used and reports any errors
- The contents of variables must be of a specific type otherwise an error created by the attempted
use of anything else.

6. Reserved words
- Reserved (key) words are identifiers with a pre-defined meaning in a specific programming
language, for example Dim, if, End, integer, As, etc. in Visual basic.
- Reserved words must not be used as variables.
- Each programming language has its own reserved words, which may differ from other
languages.
- translator program maintains a dictionary of reserved words
- if the reserved word used is not in this dictionary then an error has been made and message
may be given which suggests one close to spelling provided

7.
Expressions
- An expression is a construct made up of variables and operators that makes up a complete unit
of execution.
Example:
NumberA = a+b-c*d
- The above is a statement. However, a+b-c*d is an expression found in a statement.
- Expressions can be arithmetic (as given above), Boolean or string expressions. For example,
(a>b) and (a>=c)
- Operator precedence is very important in evaluating expressions and therefore it is important to
enclose expressions is brackets where possible. Operator precedence is as follows, starting from
the highest to the lowest:
( ), Not, ^, {*,/,}\,Mod, {+, -,}{=,<,>,<=, >= },…
NB: Operators is set braces indicates that they are in the same level.
- Arithmetic expressions are evaluated first, followed by comparisons and lastly logical
expressions.

8. Statements
- A statement is a single instruction in a program which can be converted into machine code and
executed.
- A statement can just be one line of program code but in some cases a statement may have more
than one line.
- For example: Name = “Marian” is a statement.
Example 1
NumberA = a + b
- this is an assignment statement, that is, variable NumberA is assigned the sum of the values of
variables a and b. thus if a=2 and b = 3, NumberA is assigned the value 5.
- An assignment is an instruction in a program that places a value into a variable, e.g total = a + b
Compiled by Kapondeni T. Page 13 of 27
- The above is just one line statement.
Example 2
If a>b Then MsgBox "a is bigger than b.", vbExclamation
- The above is a one line statement composed of if statement.
Example 3
If b < 0 Then
MsgBox "b is less than zero. Command cannot be executed", vbExclamation
Exit Sub
End If
- This is a statement (starting at the first if and ending at End If.
- This state comprises of other statements between it.

9. Block structure
- A block is a group of zero or more statements between balanced braces and can be used
anywhere a single statement is allowed. For instance
if (condition) Then ( begin block 1)
………………. (end block 1)
else (begin block 2)
……………….
End If (end block 2)

10. Functions
- A function is a self-contained module that returns a value to the part of the program which
calls it every time it is called/executed.
- A function can be just an expression that returns a value when it is called.
- A function performs a single and special task, e.g. generate a student number.
- Because they return a value, functions are data types, e.g. integer, real, etc.
- Functions can be in-built functions or user-defined functions.
- In-built functions are pre-defined procedures of a programming language that returns a value,
e.g. Val (returns a numeric value of a string), MsgBox (creates a textbox on the screen), Abs
(returns an absolute value of a number), etc.
- Visual Basic has in-built date functions, string functions, conversion functions, etc.
- A user-defined function is a procedure (module) that returns a value whenever it is called. The
structure of a user defined function is as follows:
Public Function count_rec(ByVal rs As Recordset) As Boolean
If rs.RecordCount <= 0 Then
MsgBox "There is no record in the table.", vbExclamation
count_rec = True
Else
count_rec = False
End If
End Function
- Note that a Function starts with the word Function and ends with the statement End Function.
This function returns a Boolean value(either true or false). The function name as just after the
word Function, i.e count-rec in the case above.

11. Procedures
- A self-contained module that does not return a value.
- Procedures usually starts with the key word Procedure and then procedure name, e.g
Procedure FindTotal. Procedures are user defined.
- The name of the procedure should be related to its task
- Each procedure name must be unique within the same program.
- A procedure can be called from the main program or by other modules.
Compiled by Kapondeni T. Page 14 of 27
- A procedure is called by stating it name
- Parameters are usually passed when calling procedures.
- Parameters/arguments are values that are passed from one procedure to another and can be
the actual values or variable names. They are therefore values given to a function by
statements from other modules.
- Parameters can be formal or actual parameters
Actual parameters: these are arguments found in the calling module/statement, could be
variables or actual data like 30, 40.
Formal parameters are those variables that receive data from calling module or statement.

- When processing of the called procedure is finished, processing goes to the next stated after
the calling statement.
- Parameters can be passed by value or by reference

Passing Parameters by Value


- Passing by value is whereby the actual value (or value of a variable) is searched (e.g 20),
copied and passed to another module.
- In this case, variables whose values are passed will NOT be altered even if the values of
variables in the calling procedure or function change.
- Thus only the values of the variables are passed, not the variable itself.
- A copy of the value of the variable is passed and not the variable itself.

Compiled by Kapondeni T. Page 15 of 27


Passing Parameters by reference
- Passing by reference is whereby the address of the value is passed, allowing it to be altered if
necessary.
- This is when calling procedure passes the variable to another function, whose processes may
alter the value of the variable in the calling procedure.
- In this case, the original variable’s memory is addressed and can therefore be altered by the
called function. E.g.

- If the programmers does not specify ByVal or ByRef function, Visual Basic assumes that it if
ByRef by default.
12. Semantics
The meaning attached to statements and the way they are used in a certain language.
13. Syntax
- These are grammatical rules and regulations governing sentence construction and layout of
different programming languages.
- For example, Pascal uses a semi-colon(;) at the end of each instruction, it also uses the reserved
(key) word writeln to display items on the screen, etc.
- Each programming language has its own syntax.
- A program with syntax errors does not run.

14. Literals
- A literal is a variable which is given a fixed value within the code of the program.
- A literal is the source code representation of a fixed value.
- Literals are represented directly in your code without requiring computation, as shown:

Dim result As Integer


Dim Name As String
Result =20
Name = “Kapondeni”

15. Input operations


This Refers to statements that prompts the user to enter data into the computer. E.g.
ID = InputBox("Enter Member Data to Search")
The above displays an input box with the message in bracket that prompts the user to enter data that
needs to be searched.
16. Output operations
Refers to statements that allows display or printing of results on the screen or to the printer, e.g.
frmPayments.Show
allows, the payment form to be displayed on the screen.
MsgBox ("Record not Found")
Displays a message box with the message in brackets.
17. File handling operations
Allows user to manipulate files. E.g. in Visual basic:
Compiled by Kapondeni T. Page 16 of 27
rs.Close
While Not .EOF

DATA TYPES
- Data types describe the nature of data handled by programs.
- These are important as they enhance program readability and maintenance.
- Data types can be simple (integers, string, etc.) or complex (arrays, lists, records, etc.).
- Data types are declared at the beginning or at some point inside the program code.
- Data types for variables must be properly declared.

a. Numeric data types


1. Integer
- Used to represent whole numbers, positive or negative, and occupy 2 bytes of memory.
- Decimal values will be rounded to the nearest whole number.
- Values accepted range from -32 768 to + 32 767. Default value is 0.
2. Byte
- Stored in a single 8-bit unsigned data value ranging from 0-255.
- Used to store binary data.
- Default value is a 0.
3. Real
- A data type that is used to represent values (numbers) with decimal point values, e.g 23.56 is a
real number.
- Can be used to represent averages.
- Some languages take this to be float data type.
4. Currency
- Occupy 8 bytes with 0 as default value.
- Used for handling monetary values and support up to 4 digits to the left and 15 digits to the
left of the decimal point.
5. Date
Represent date and time values and occupy 8 bytes.
6. Double
- Stores a double precision floating point number with decimal places but longer than single.
- Default is 0 and occupies 8 bytes
7. Single
- Stores a single precision floating point number, with decimal places.
- Default is 0 and occupies 4 bytes.
8. Long
- Short for long integer and default value is 0.
- It occupies 4 bytes from -2 147 483 648 to -2 147 483 647.

b. String data types


1. String
- A string is data type that stores one or more characters, which can be alphabetic or alphanumeric.
- If a variable is declared as a string, it must not be used for daily life mathematical calculations
because strings are not numbers.
- Strings are declared as follows:

Dim Name As String


- If a value of string variable is assigned within the program code, it must be enclosed in quoates
e.g.
Compiled by Kapondeni T. Page 17 of 27
Name = “Tungamirai”
Name = “”
- The second example will be a null (un-initialised) string. It is different from 0 or space but is
distinct in nature.
- Strings can be of fixed length or variable length

Fixed length: has specified maximum number of characters. E.g.


Dim Sname as string * 20
- The field will be allocated 20 spaces in the computer memory.
- If a name shorter than 20 characters is entered, blank spaces will remain unoccupied since
they would have been assigned to Sname already.
- If too long than allocated space, the extra characters are cut.

Variable length:
Does not specify the number of characters, each data items occupies the number of spaces it
requires.
This is declared as follows
Dim Sname as string
2. Boolean
- Represents instructions that evaluate to either True or False only. The default value is
False and occupy 2 bytes of storage.
- Mostly used together with If….. Then……. Else Construct.
3. Character
- A data type that holds just one alphabetic or special character like %, $, a, etc.
- can be written as char in most programming languages.

User-defined data.
These are defined by the user depending on the method of solution, e.g classes when the used define
own classes.

Enumerated data:
These are data types with a list of items that are pre-defined, e.g days of the week, months of the year,
etc.

Word: a computer word is group of bits that can be handled or transferred by the processor as a
single unit (word length).

Factors to consider when choosing a programming language


- Nature of the application.
- Availability of suitable compilers/interpreters.
- Availability of needed facilities in the programming language for system implementation.
- Availability of compatible hardware.
- Availability of expertise of the programmers.

TOP-DOWN PROGRAM DESIGN (Modularisation)


- This refers to the splitting of a program into simpler subtasks called modules which will be easier
to solve.
- For example, a program can be split into modules to Accept Number, Add, Divide, Subtract and
to Display Results.
- The main program then will have calls to the modules, which may also call other modules.
- Modules are also called procedures, routines, sub-routines or functions.
- The splitting of a problem into a series of self-contained modules is called modularisation
(modular programming).
Compiled by Kapondeni T. Page 18 of 27
- However, there is actually a difference between a procedure and a function.

Advantages of modularisation (modular programming)


- Minimises coding since a module can be called many times from different modules using
parameters can be used to pass data within the program.
- Programmer can concentrate at one simpler task at a time.
- Modules are simpler and easier to understand and to solve.
- Modules are easier to test and to debug and therefore lessening testing time.
- Program modification is easier since changes are isolated with specific modules.
- Many programmers are employed, one on each of the modules.
- More experienced programmers can be assigned complex modules.
- It saves programming time by sharing tasks.
- A large project will be easier to monitor and control.
- It is easier to update (modify) modules than the whole program at larger.
- Fewer bugs since each set of programming commands is shorter
- Allows library programs to be inserted

Disadvantages of Modularisation
- Documentation will be long and thorough, therefore may take time to produce
- Can lead to problems of variable names as the modules are developed separately.
- However, it may be difficult to link the modules together.

*NB: Library programs: this refers to a collection of standard pre-written programs and subroutines
that are stored and available for immediate use by other modules in the system.
Library programs are programs that are required by other modules during execution, e.g. the Dynamic
Link Libraries (DLL) in the Windows environment.
Libraries contain common tasks like saving, deleting, etc.
Library programs are referenced by most modules in the systems and they provide data to other
modules.
*NB: Stepwise refinement: a technique used in developing the internal working of a module.

Aims of program design


 Program designing must produce a program with less maintenance and debugging costs later on.
 Therefore the aims are to produce a program with the following features:
- Readability: program must be easy for programmers to read and understand
- Reliability: program must always perform what is was designed to do.
- Maintainability: program must be very easy to modify or change when the need arises.
- Performance: the program must be efficient and fast in performing whatever it was designed
to perform.
- Storage saving: program must occupy as little storage space as possible.

BACKUS NAUR FORM AND SYNTAX DIAGRAMS


- These are methods of specifying a set of rules that specify precisely every part of the language.
- Backus Naur form can be used to define/expressed the structure of integers, variables, etc.
- For instance
An integer is a sequence of the digits 0, 1, 2, … , 9. Thus the following are all valid integers.
Compiled by Kapondeni T. Page 19 of 27
0
2
415
3040513002976
0000000123
- Thus, an integer can be a single digit. We can write this in Backus Naur form as
<integer> ::= <digit>
This is read as 'an integer is defined to be (::=) a digit'.
But we must now define a digit. A digit is 0 or 1 or 2 or … or 9 and we write this as
<digit> ::= 0|1|2|3|4|5|6|7|8|9
- The vertical lines means OR
- Thus our full definition of a single digit integer in Backus Naur Form is
<integer> ::= <digit>
<digit> ::= 0|1|2|3|4|5|6|7|8|9

Specifying integers of any length.


Let’s take our integer as 589, and see how we can define it in Backus Naur Form.
An indefinitely long integer is defined as
<integer> ::= <digit><integer>
This is a recursive definition as integer is defined in terms of itself. Applying this definition several
times produces the sequence

To stop this we use the fact that, eventually, <integer> is a single digit and write
<integer> ::= <digit>|<digit><integer>
We now have the full definition of an unsigned integer which, in BNF, is
<unsigned integer> ::= <digit>|<digit><unsigned integer>
<digit> ::= 0|1|2|3|4|5|6|7|8|9

The above can be shown using a syntax diagram as:

Signed integers in BNF


Signed integers includes +78, -90, etc.
This is simply an unsigned integer preceded by a + or – sign. Thus
<signed integer> ::= + <unsigned integer>| - <unsigned integer>

Thus the final answer will be as


Compiled by Kapondeni T. Page 20 of 27
<integer> ::= <unsigned integer>|<signed integer>
<signed integer> ::= + <unsigned integer>| - <unsigned integer>
<unsigned integer> ::= <digit>|<digit><unsigned integer>
<digit> ::= 0|1|2|3|4|5|6|7|8|9

Using syntax diagrams, this can be illustrated as:

Variables in BNF
Valid variables start with a letter (Upper or lowercase) and followed by any character (which must be
a letter, digit or underscore) upt to any length. This can be defined in BNF as:
<variable> ::= <letter>|<variable><character>
<character> ::= <letter>|<digit>|<under-score>
<letter> ::= <uppercase>|<lowercase>
<uppercase> ::= A|B|C|D|E|F|G|H|I|J|K|ZL|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
<lowercase> ::= a|b|c|d|e|f|g|h|i|j|k|zl|m|n|o|p|q|r|s|t|u|v|w|x|y|z
<digit> ::= 0|1|2|3|4|5|6|7|8|9
<under-score> ::= _
The above can be illustrated in a syntax diagram as:

The Compilation Process


The compilation process takes a number of stages, which are:
- Lexical analysis
- Syntax analysis
- Code generation
- Code optimisation

(a) Lexical analysis


- The first stage of compilation in which the source code is broken into logical smaller chunks
called tokens.
Compiled by Kapondeni T. Page 21 of 27
- This process is done by a program called a lexical analyser.
- A token is the smallest independent part of a program which has some meaning, e.g. variable.
- Lexical analyser also preforms the following:
 Removes redundant parts of source code, e.g. spaces/white space
 Removes comments from the program
 Checked validity of reserved words within the program code
 Tokenises reserved words (replacing of reserved words like If, While, etc. with shorter
tokens)
 Tokenises operators
 Checks validity of symbols and variable names variable names
 Creates the symbol table

(b) Syntax analysis


- The second stage which involves determining if the string of input tokens parsed from the lexical
analysis stage forms valid sentences and if they conform to the grammar of the language.
- Some of the activities involved are:
 Checking of grammar of statements to determine if they conform with the language
 Issuing of error diagnostics (methods/suggestions for solving errors)
 Checking for existence of jump destinations (presents of called modules)
 Control constructs checked to determine if they are valid
 Checking that variables used in the program have been declared
 Checking for existence of library modules
 Checking if expressions are valid, e.g. if the correct number of brackets are used.
 Determining the priorities of operators in an expression
 Generates a dictionary, which is a list of variables used in the program and their data types.
 Produces intermediate code
This stage can also be called parsing and is done by a program called a parser.

(c) Code generation


- This is a compilation stage where the code specific to the target machine is generated, i.e. the
machine code.
- It involves the production of a machine code program/intermediate code which will produce the
results intended by the source code.
- The following are also involved in code generation:
 Intermediate code produced which can then be turned into executable code/machine code
 the production of a machine code program
 Address of variables calculated and stored in symbol table

(d) Code optimisation


- In this stage, the machine code is optimised which involves using rules to make code as small
and efficient as possible by removing any duplicate or redundant instruction which improves
speed of execution

In summary, the stages of compilation are illustrated as follows:

Compiled by Kapondeni T. Page 22 of 27


Source Code

Lexical Analyser

Syntax Analyser

Semantic Analyser

Symbol Table Error


Manager Intermediate Code Handler
Generator

Code Generator

Code Optimiser

Object Code
Analysis Phases
Consists of the lexical analyzer, syntax analyzer and the intermediate code generator.

Lexical Analysis
This is where scanning of the source program is done. Each sequence of characters that have
an atomic meaning are recognized and represented by a token.
A token represents a class of valid letters, for example a program is going to analyse numbers,
variables and functions.

Syntax Analysis
This is the stage where tokens are grouped into large structures, for example, assignment
statements.

Semantic Analysis
This is where there is transition of tokens into code generation and complex errors are
detected.

Intermediate Code Generator


This is where compilation from the source language into a target program.

Synthesis Phase
This phase consists of the Code Optimiser and Code Generator.

Code Generation

Compiled by Kapondeni T. Page 23 of 27


The target code is generated from the intermediate code to perform static scheduling and
register allocation.

Code Optimiser
There is reduction of the number of instructions in order to allow the program to run faster.

Compiler Dealings with variables


During lexical analysis
- Characters in the variable name are tokenised
- Variable name is added to symbol table…
- Data type added…
- scope is added/block(s) in which variable is valid
During syntax analysis
- Variable names which do not match the rules are reported in error diagnostics
- Statements containing variables are checked for syntax
- Position in table is hashed from the name
- Variable declarations are checked/also variable use
During code generation
- Address of variable calculated
- added to symbol table

Compiler Dealings with syntax errors


- Reserved word is isolated
- if not in list of reserved words, then an error message is conveyed
- If reserved word identified then syntax table is checked for expected form of statement.
- If form of statement does not match an error issued
- Variable names checked against rules for variable names
- Variable declarations are checked to determine if they are present.

Loading programs and linking modules


Loaders
A loader is a program that loads compiled program/modules into the computer’s memory. Some
functions of loaders are:
- Loads the individual modules into the computer’s memory
- Loader decides where modules are to be placed in memory
- Adjusts memory addresses for individual module according to where they are placed
- loads library routines
- Copies program from storage location into memory

Loaders are in two forms, absolute and relocating loaders


(a) Absolute loader: refers to a program that loads program into a fixed area of memory each
time a program is run. The loaded program can only work when it is loaded to that single
address in memory.
(b) Relocating Loader: This refers to a program that loads a program anywhere in the memory
and the loaded program work without any problem. Thus address of a loaded program is
recalculated every time the program is loaded into memory.
Also relocating can be in two forms:
Static relocating: Once the program is loaded into a memory address, its relocatability is lost,
that is, it cannot be moved to any other address during execution.
Dynamic relocating: the program can even be moved to any other address during execution.

Linkers

Compiled by Kapondeni T. Page 24 of 27


A linker is a program that compiles all loaded modules and then create linkages between them. Other
functions of linkers are:
- Linker joins the modules compiled correctly
- Calculates addresses of the separate modules
- Allows library routines to be linked to several programs
- Ensures jump instruction from module to module properly addressed
- Produces an executable file
- matches up address references between modules
Editors
These are programs that are used to key–in and amend source code. It is also used to display and edit
text before compilation.

REVIEW QUESTIONS
1. Explain the terms
(i) data encapsulation
(ii) inheritance
(iii) Polymorphism
when applied to programming in an object oriented programming language. (4)
2. Distinguish between procedural languages and declarative languages. (4)
3. Explain the passing of parameters by reference and by value. (4)
4. (a) Explain the difference between the translation techniques of interpretation and compilation [2]
(b) Give two advantages of each of the two translation techniques. [4]
5. An amount of money can be defined as
• A $ sign followed by either
• A positive integer or
• A positive integer, a point, and a two digit number or
• A point and a two digit number
A positive integer has been defined as <INTEGER>
A digit is defined as <DIGIT>::= 0/1/2/3/4/5/6/7/8/9.
a) Define, using Backus Naur form, the variable <AMOUNT OF MONEY>
b) Using the previously defined values of INTEGER and DIGIT, draw a syntax diagram to define
AMOUNT OF MONEY.
6. State the three stages of compilation and briefly describe the purpose of each. [6]
7. Explain in detail, the stage of lexical analysis. [6]
8. Explain the role of
(i) linkers,
(ii) loaders
in the running of programs [4]
9. Two of the stages which a high level language program undergoes during compilation are lexical
analysis and syntax analysis.
Discuss how errors are discovered during each of these two stages. [5]
10. (a) (i) Describe what is meant by source code. [2]
(ii) Explain why source code needs to be translated into object code. [2]
(b) State what is meant by the following types of programming error:
(i) syntax error [1]
(ii) arithmetic error [1]
11. (a) Explain how the translator program prepares the programmer’s code into a program that the
machine can run. [2]
(b) (i) Explain what is meant by a procedure. [2]
(ii) Describe how procedures and the programming construct “selection” can be used to code a
simple menu system for a user. [3]
12. (a) Explain the difference between interpretation and compilation of a program written in a high
level language. [2]
Compiled by Kapondeni T. Page 25 of 27
(b) Explain what happens during the lexical analysis stage of compilation. [5]
(c) Describe two things that happen during code generation. [4]

13. (a) Programs can be designed in modular form.


Discuss the advantages and disadvantages of designing programs in modular form. [5]
(b) A program is to be written which will update the records in a sequential file and then produce a
backup copy.
Describe, using a diagram, the way that this problem can be split into modules to prepare it for
coding. [5]
14.(a) Explain why a program, written in a high level language, needs to be translated before it is run
on a computer. [2]
(b) Describe the difference between interpretation and the compilation of a high level language
program. [4]
(c) Explain how errors in the
(i) reserved words,
(ii) variables
Used in high level language instructions are recognised by the translator program. [4]
15. Describe each of the following programming paradigms
(i) Object-oriented, [2]
(ii) Declarative. [2]
16. A name is passed as a parameter to a function. The function uses a loop structure to search for the
name in an array. It returns the details found to the calling program.
(a) The name to be searched can be passed either
(i) by value, or
(ii) by reference.
Using this example, explain what is meant by a parameter being passed by value and by
reference. [2]
(b) Using examples from this function, explain what is meant by a
(i) local variable,
(ii) global variable. [4]
(c) Two types of translator are interpreters and compilers.
Describe the difference between an interpreter and a compiler and state why both would be
used with this function. [4]
(d) Explain the purpose of a loader in the running of the final program. [2]
17. (a) Explain the differences between the lexical analysis stage and the syntax analysis stage in the
compilation of a high level language program. [6]
(b) One phase of compilation is the code generation phase.
Describe the code generation phase. [3]
(c) Explain the purpose of a loader. [2]
(d) A program has been written using a top-down technique. The individual modules in the program
have been fully tested and there are no errors in any of them.
Explain why the program may fail to run or may produce incorrect results, despite the testing that has
been done. [2]
(e) When a computer runs a program, the program may fail to run successfully because there are
errors in the code.
Describe two types of error that may be present, giving an example of each. [6]
18. Explain why an interpreter would be preferred to a compiler as a translator when writing a high
level language program. [5]

19. In a particular object oriented programming language, the following classes are defined

Compiled by Kapondeni T. Page 26 of 27


With reference to the diagram explain the terms:
(i) Data encapsulation
(ii) Inheritance [4]

Compiled by Kapondeni T. Page 27 of 27

You might also like