Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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
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.
1. Interpreters
2. Compilers
Difference Between an Interpreter and a Compiler
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.
Content Loading, testing, debugging, syntax errors, logic errors, run-time errors, dry
run, test data.
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.
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
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.
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:
<arrayName>[<subscript>]
:
:
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]
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>;
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:
Read (country[count]);
Syntax:
Write(<arrayName>[<subscript>]);
If you need to print all values in an array use the following syntax:
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:
<statement(s)>;
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;
found: = ‘yes’;