Sei sulla pagina 1di 9

Section 3: Program Implementation

Objective 3.1: Distinguish between low-level and high-level programming


languages.

Content Machine language, Assembly language, Pascal, C, etc.

Low-level languages
These are languages that are machine dependent. Different brands of computers use
different program codes. The program code written for a particular brand of CPU will not run
on another brand of CPU as the codes are written with the internal operations and
architecture of a particular CPU in mind. Examples of low-level languages are Machine
language and Assembly language.

High-level languages
These are languages that are machine independent. They are not specifically written for any
one brand of computer. The program code written on a particular machine is not limited to
execution on that machine only but can also run on other similar machines. Examples of
high-level languages are Pascal, BASIC, C, etc.

Objective 3.2: Distinguish among the different generations of programming


languages.

Content Characteristics of first through to fifth generation languages

First Generation Languages (1GL)


These are low-level languages. They are called machine language and are written using 1’s
and 0’s i.e. binary code. It is the only program code that the CPU understands and can obey
or execute directly without the need to translate it.

E.g. of code 1101 1101 1011 1011

Characteristics of 1GLs
 It is the fastest to execute because it is already in the language that the computer
understands.
 It is difficult to decipher
 It is easy to make mistakes in the sequence of 1’s and 0’s
 It is time consuming or tedious to write
 It is machine dependent

Second Generation (2GL)


These are also low-level languages. They are called Assembly language and are written using
mnemonic codes- short codes .

E.g. of code LDA A, 20


ADD A, 10
STO B, A
Characteristics of 2GLs
 It is easier to write than machine language
 It is machine dependent
 It is easy to decipher

Third Generation Languages (3GL)


These are high-level languages. This generation of languages was designed so that it is even
easier for humans to understand. A compiler is used to convert the English–like statements
to machine language.

Characteristics of 3GLs
 It uses English words and symbols, and is even easier to write
 It is machine independent

Examples of 3GLs are Pascal, BASIC (Beginner’s All-purpose Symbolic Instruction Code), C, C+
+, FORTRAN (Formulator Translator), COBOL (Common Business Oriented Language) and
Java.

Fourth Generation (4GL)


These are also high-level languages. Unlike 3GLs, these are non-procedural languages which
let you perform computer operations without having to specify all the steps involved.They
are usually applied in the following types of jobs:
 Database creation and programming
 Report generation
 GUI creation
Some examples of 4GLs are Visual Basic, PowerBuilder, Delphi, and SQL.

Fifth Generation (5GL)


These are high-level languages. They are designed to build programs that help the computer
solve specific problems based on constraints and conditions. They are applied in expert
systems, artificial intelligence research and natural language systems. Examples of 5GLs are
Prolog, OPS5 and Mercury.

Objective 3.3: List the sequence of steps associated with implementing a


program.

Content Create source code, compile, linking, executing, maintain program

Steps in Implementing a Program:

 Create source code


 Compile source code
 Link the modules
 Run/execute program
 Maintain program
A program written in any programming language is called the source code. The source code
has to be translated into machine code before it can be executed. A translator is needed for
this purpose. The machine code is saved as an executable file. The executable program file
is called the object code.

A diagram Outlining the Compilation Process

Source Code Compiler Object Codes

There are two types of computer translators:

1. Interpreters

2. Compilers
Difference Between an Interpreter and a Compiler

An interpreter translates and executes one instruction at a time as it is encountered.

Advantages
 It translates one instruction at a time, therefore it uses a minimum amount of the
computer’s memory

 It is also helpful in the debugging process, as the interpreter can relate error
messages to the instruction being executed.

Disadvantage
 The interpreter takes longer to run a program as time is spent interpreting the source
code every time the program needs to be run.

Compiler
A compiler translates the entire program (source code) to machine code, and then the
machine code is executed. The translated codes are known as object codes.

Advantage
 It can be executed faster than when using an interpreter, as the object codes are
saved and can be run at any time

Disadvantage

 As the entire program is translated to object code, it uses much more of the
computer’s memory.

Objective 3.4: Explain commonly used terms and concepts in programming.

Content Loading, testing, debugging, syntax errors, logic errors, run-time errors, dry
run, test data.

Commonly Used Terms and Concepts in Programming

 Loading – is reading a program from the hard disk into main memory (RAM)
 Testing - a completed program must be tested for errors to prevent crashing or
stalling. The programmer must make sure the program works for any correct input
data that the user gives it. The testing phase is to detect any problems in the
program. Dry runs and computer testing could reduce the chance of error, before
releasing the program to its final users.

 Dry runs/Manual testing - a manual traversal of the logic of a program. After the
development of a program the programmer should examine the code to see how it
would run.

A dry run is carried out when the action of an algorithm is examined with a set of
input values.

A trace table is a useful tool for a dry run, to check how the program would behave.

 Test Data – a wide range of data should be used to test a program. This data should
include normal data values and extreme values e.g. large, small and negative
numbers. Using this wide range would help detect which values might cause a
program to fail.

 Debugging – is the process of correcting or removing errors from a program before


it is put into operation.

 Program Errors – A program error is a mistake/error in a program and is also known


as a bug.

During program development, three types of errors may be encountered as followed:

1. Logic errors

2. Syntax errors

3. Run-time errors

1. Logic errors – arise when the sequence of the instructions is not correct and
there are flawed comparisons and selection statements.

For example, let’s suppose we wanted to print the names of all girls who are under
the age of 18 and we wrote the following program:

.
.
If (gender = F) and (age <= 18) then
Writeln(name);

This code would result in the printing of the names of girls who are 18 as well as
those who are under 18. This is because in the if statement, we used <= instead of =
. This is a common error in the logic that will give undesirable results.

2. Syntax errors – occur when the source code has not been written in accordance
with the rules of the programming language. For example, if we omit to
terminate an assignment statement with a semi-colon. The following statement
would result in a syntax error during compilation:

total:=total + X

or

if we used a reserved word incorrectly, such as in:

var := a + b;

Syntax errors can be easily located and corrected as the compiler usually issue messages
which identify the location and cause of the errors.

3. Run-time errors – are generated during execution of the program. They result
when the processor encounters an instruction that violates the processing rules.
For example, if we had an instruction that attempts to divide by 0, such as:

Number:=1;
Number:= Number - 1;
Answer := total/Number; {illegal}

The last statement would generate a run-time error as an attempt is being made to
divide by a value of 0.

Objective 3.9: Manipulate data in a list.

Arrays
Arrays are used to store values of the same type as one variable e.g. the names of 100
countries. If we did not have arrays we would have to create a 100 variables to store each
name. This is very tiring, cumbersome to manipulate and time consuming.
An array is a single variable with multiple locations. Each location stores one element/value.
To refer/access an element a subscript is used. For example, country[5]. 5 is the subscript
and country[5] allows the program to refer to the country name stored in that location.

Declaring an array
Just like regular variables and constants, an array must be declared before it is used.

Syntax: var <arrayName> : array[1..<no. of values to be stored>] of <data type>;

e.g. var country : array[1..100] of string;

Referring to a value in an array

Syntax:
<arrayName>[<subscript>]

E.g. country[1] refers to the 1st value


country[2] refers to the 2nd value
country[3] refers to the 3rd value

:
:
country[99] refers to the 99th value
country[100] refers to the 100th value

N.B. it is an error to refer to a subscript that is out of the range e.g. country[105]

A subscript can be a constant e.g. 6, a variable e.g. max, or an expression n + 1

Storing values in an array

If a value has to be set to a specific location in an array, the following methods can be used:

Method #1

Syntax:
<arrayName>[<subscript>] := <value>;

E.g. country[1] := ‘Trinidad and Tobago’;

country[2] := ‘St. Kitts’;

Country[100] := ‘Guyana’;

This method is very time consuming, therefore it is better to use the following method:

Method #2

This method uses a variable for the subscript and a FOR loop to increment the value of the
variable.

Syntax:

For <counterVariable> := 1 to <no. of values to be stored> Do

Read (<arrayName>[< counterVariable >]);

E.g. For count:= 1 to 100 Do

Read (country[count]);

Writing from an array


To write a value store in a specific location the following syntax is used:

Syntax:

Write(<arrayName>[<subscript>]);

E.g. Writeln (country[6]);

If you need to print all values in an array use the following syntax:

For <counterVariable> := 1 to <no. of values to be stored> Do

writeln(<arrayName>[< counterVariable >]);

E.g.
For count:= 1 to 100 Do

writeln (country[count]);

Traversing an array
Traversing an array involves accessing all elements in the array from first to last, one by one.
This is accomplished using the FOR loop.

Syntax:

For( <counterVariable> := 1 to <final location in array> )Do

<statement(s)>;

E.g. For (index: = 1 to 6) Do

write (num [index]* num [index]);

Searching an array (linear search)


Searching an array involves traversing the array until you find the location of the element
you require and retrieving it.

A linear search requires that each element of the array is compared with the given item to
be searched for, one by one.

E.g. index: = 0;

found: = ‘no’;

Repeat

index: = index + 1;

If (names [index] = ‘Roger’) then

found: = ‘yes’;

until (found = ‘yes’);

Potrebbero piacerti anche