Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

IT Notes Unit 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

SECTION 8: PROGRAM IMPLEMENTATION

1. Distinguish between low-level and high-level programming languages.

Low Level Languages (Machine and assembly)

 Low level language is a programming language that deals with a computer’s hardware
components and constraints.
 These are languages that are classified as machine dependent, meaning, only a particular CPU
will understand this type of language.
 Different types of computers have various types of processors, this means that programming
codes that are written on any type of computer may not work on another.
 There are two main types of low-level languages.
o First generation (1 GL) – machine language.
o Second generation (2 GL) – assembly language.

First Generation Language (1 GL)


 A first generation language (1 GL) is a grouping of programming languages that are called
machine language. It is also known as 1st generation language.
 This language is machine dependent, different brands of computer use different code.
Therefore, a program written for one brand of CPU will not run on another brand of CPU.
 There was originally no compiler or assembler to process the instructions in 1 GL because the
codes were already written in the language of the computer, that is binary (machine language).
 The Central Processing Unit (CPU) of a computer understands a very specific language called
machine language.
 Machine language consists of binary code that a microprocessor can execute directly.
 Binary code is a sequence of 1’s and 0’s. eg 1001, 10100.

Characteristics of first generation language (advantages).


 It is the fastest to execute because the code is already in the language that the computer can
understand.
 The instructions are executed directly by the CPU.

Characteristics of first generation language (disadvantages).


 It is machine dependent.
 Time consuming and tedious to write.
 When there is an error, it is difficult to locate and fix because it is in binary.
 It is very confusing to the programmer because of the amount of 1’s and 0’s.

Second Generation Language (2 GL)


 A second generation (programming) language (2 GL) is a grouping of programming languages
associated with assembly languages.
 Programs can be written symbolically using English like words called mnemonics.
 Mnemonics are short codes that suggest their meaning and are therefore easier to remember.
o These codes represent the operations and addresses that relate to main memory.
o Examples include;
 ADD
 STO
 ADD

Characteristics of 2nd generation language (2GL)


 It is easier to write than machine language.
 As with machine language, assembly language is also machine dependent.
 Assembly language is translated into machine code using a special program called an assembler.
 Assembly language is also a low level language.

High Level Languages.


 These are programming languages that are not machine dependent as low level programming
languages.
 This means that programs created on one computer can be used on another computer.
 It uses English like words and symbols and is therefore even easier to write.
 There are 3 main types of high level languages;
o Third Generation Language ( 3 GL)
o Fourth Generation Language ( 4 GL)
o Fifth Generation Language ( 5 GL)

Third Generation Language (3 GL)


 These are a grouping of programming languages that introduced significant enhancements to
second generation languages.
 More programmer friendly than 1st and 2nd generation language.
 English words are used to represent variables, programming structures and commands.
 Examples of 3 GL are;
o Pascal
o Fortran
o COBOL
o C/C++
o BASIC

Characteristics of Third Generation Language ( 3 GL)


 Machine independent.
 Easy to read, write and maintain.
 Program code have to be translated to machine language by a compiler or an interpreter.

Translator – converts one language into another. A translator, in software programming terms, is a
generic term that could refer to a compiler, assembler, or interpreter;

Compiler – a special program that converts high level language (source code) to machine language. The
entire program is converted at once.
Interpreter – a special program that converts high level language (source code) to machine language.
The conversion takes place one line at a time.

Assembler - An assembler translates a program written in assembly language into machine language.

Fourth Generation Language (4 GL)


 The 4th generations of computer languages (4 GLs) has been designed for the development of
commercial business software.
 It uses English like statements that are very user friendly.
 Designed to reduce the overall time, effort and cost of software development.
 A non-procedural programming language that requires less coding than lower-level languages.
 It has a lot of built in features so the user does not have to write codes to perform certain tasks,
you just have to select the feature from the programming language. There are built in function
wizards to assist users.
 The programmer, in creating a program, will just state what should be done and does not have
to worry about how to create the code or program.

Characteristics of Fourth Generation Language ( 4 GL)


 They possess user friendly interfaces.
 They can be used to create databases easily.
 They can be used to generate reports.
 They run slower than earlier languages because their machine code is longer and more complex.
 These programs can be very ‘wordy’ and or lengthy.

Fifth Generation Language (5 GL)


 Designed to build specific programs that help the computer solve particular problems.
 Programs are designed to solve the problem for the user, all the codes for the program will be
automatically generated.
 This is achieved by specifying the problem, parameters and the constraints, the 5 GL will do the
rest.
 Used mainly in the field of artificial intelligence (AI).

2. Describe the sequence of steps in implementing a program.

The process of implementing a program consists of 4 major steps;


1. Create source code (in a high level programming language).
 These are the set of instruction that are used to write computer programs using a high
level program such as Pascal.

2. Translate source code into machine code.


 This is the process where interpreters or compilers are used to execute a program. The
programs convert the source codes to machine code.
 The first role of the compiler or interpreter is to check that the high level source code is
correct according to the syntax rules of the language.

3. Execute the program.


 At this stage the compiler would have translated the source code into machine code and
put the result in an executable file.
 The executable file is the final product of the program which is now in a useable state to
solve the problem it was intended for.

4. Maintain the program.


 When a program has been written, compiled, tested and documented it enters a
maintenance phase.
 This phase continues until the program is no longer needed.
 Maintenance is of two types-
i. Corrective – for fixing program bugs or flaws in the program what was not
initially detected in the testing phase.
ii. Adaptive – for implementing new features or changing the way a particular
feature works (similar to when apps in your phone is updated).

3. Perform checks and tests on programs to verify correctness.


 In the writing of computer programs, the possibility exists that errors may occur at various
stages in the development of the program.
 There are three types of errors that we need to look out for which are as follows :
o Syntax error – this is an error when the rules (syntax) of the programming language is
not adhered to.
 Example below -
Write (‘How are you?’)
 The above line would return a syntax error because the ; was omitted at
the end of the statement.

o Logic error – this error when there is a mistake in the design of the program. Although
the program would compile and no errors would show up during compiling, the results
of the program may not be correct.
 Example below -
if mark <= 40 THEN
writeln (‘PASS’)
else
writeln (‘Fail’);

 In the above lines of Pascal codes, these lines would compile and no
errors would show up. However, if a student scores 60 marks,
according to the codes the program would output “Fail” because
according to the test condition, if the mark is <= 40 then “Pass” will be
printed. The problem here is the LOGIC in the code.

o Run-time error
 This occurs during the program execution stage when you attempt to run the
program.
 Are detected during the execution and usually cause the program to terminate
abnormally.
o Summary of the 3 types of errors are illustrated in the diagram below.

Testing (test data)


 Test data is used to test an algorithm and to test a program when it has been implemented.
 It is a set of data values selected to test a computer program.
 It should be selected to test all the possible paths through a program.
 For test data to be useful, the expected output must be known before the test data in inputted.
 For example
o Write a program to determine the largest number entered.
o Test data 7, 67, 5, 34, 2.
o Expected result should be the number 67.
o If the program returns 67, then you can safely say the program is working correctly.
o If the program returns any other value, then you definitely know that the program is not
working correctly.
o In this case, if the program compiles without showing any error, then this will be
considered to be a ___________ error? (What you think?).

Debugging techniques.
 Debugging is the routine process of locating and removing computer bugs, errors or
abnormalities which is systematically handled by the software programmers.
 Debugging checks, detects and corrects errors or bugs to allow proper program operation.
 Some common debugging techniques are
o Correcting syntax errors.
o Correcting logic errors in the program codes.
o Activating the debug feature of the compiler (eg in the Pascal program, execute from
the menu, then choose debug)
o Variable checker – used to determine if the variables are of the correct data type.
o Step mode – If the next instruction is a call of a function block or a sub-program, the
execution passes over (step over) to the following instruction.
o Break point – The programmer can set break points in the code causing the program to
stop at scheduled intervals so that the status of the program can be examined in stages.

4. Declare variables and constants using elementary data types


 Recall from unit 7, data types was mentioned and are listed below for your convenience.
o Integer – used to store positive and negative whole numbers.
o Real (floating-point) – used to store numbers with a decimal point.
o Char (character) – used to store a single unit of data including letters, numbers and
symbols (A, 9, v, $, &)
o String – used to store a sequence of characters such as names, addresses, phone
numbers etc (John, bus, 883 Papourie Road, 654-0892).
o Boolean – used to store a true or false value or a yes or no value.

 Examples of declaring variables and constants will be illustrated in advanced Pascal programs
that will be done.

Displaying/Outputting information using Pascal


(Note: This is the Programming Language that will be used in BWSS)
Pascal Basics.
 The following program will illustrate a few fundamental Pascal syntax. Consider the following
program named display. (Refer to program name p1 display)

program display;
begin
writeln('This is what will print.');
readln;
end.

This is the program after is has been executed.


Program Code Explanation
program display; This is the program name. All Pascal program begins with
the word ‘program’ followed by a name. the semicolon is
used to end the statement;
begin
begin is compulsory to indicate where the codes start. A
semicolon is not used with the begin.

Writeln(‘ ’); this is the code to display/output anything on


writeln('This is what will print.',); the screen. Note only whatever is inside the quotes is
displayed.
A semicolon is always used at the end of a writeln(‘’)
statement. Notice that this line is indented to show it
readln; belongs within the begin and end.

The readln is optional. Without it the program is still correct


and error free. It is used to allow the results of the program
to remain visible on the screen. If you do not have it in a
program and you run a program you will not see the result
end. as shown above, on your screen.

end. This is used to end the program. You would notice that
this last statement has a period (.) instead of a semicolon.
In advanced programs, end is used within a program for a
different purpose for example for something called block
statements.

Consider the following program named moreDisplay. (Refer to program name p2 moreDisplay)

program moreDisplay;

begin
writeln('Hello form 4 Science');
writeln('My name is Siri');
writeln('Watch now all dem iPhone people feeling nice.');
writeln('I''ll stick to my $99 Digicel mee-too.');
readln;
end.

This is what the program will produce after is has been executed.
The explanation from the first program is applicable to this program as well. Pay attention to the
following;
 Each new statement you want printed has to go in a new writeln(‘ ‘ );
 The 3rd writeln statement has I''ll, notice there is 2 single quote after the I. The reason for this
is, whenever you want to use an apostrophe, you must use the double single quote. If you use
only 1 single quote, that will signal to the computer that the statement has ended because
remember the syntax of the writeln statement is writeln(‘abcdef’); That is, writeln, open
bracket, open single quote, close single quote, close bracket and semicolon.

Points to remember about the Pascal Language.


 Pascal contains some reserved words that serve special purpose, such as write, writeln, read,
readln, begin, end, integer, real, char, string, var, real, div, mod, const, if, then, else, while,
repeat, until, to, for, array.
 Reserved words cannot be used as a variable name (variables will be done in advanced
programs)
 It is case insensitive, ie moreDisplay, MOREDISPLAY & moredisplay is considered to be the same
regardless of the differences in the cases of some of the letters. This rule is not the same for
some other programming languages.
 Writeln/write is used to display/output information on the screen.

Difference between writeln/write.


writeln('Hello form 4 Science'); write('Hello form 4 Science');
writeln('My name is Siri'); write('My name is Siri');
writeln('Watch now all dem iPhone people feeling nice.'); write('Watch now all dem iPhone people feeling nice.');
writeln('I''ll stick to my $99 Digicel mee-too.'); write('I''ll stick to my $99 Digicel mee-too.');

 Remember these codes from the program moreDisplay. The codes on the right was modified to
change the writeln to write. Although the program will still work error free, look at the
following output screen and note the differences.

Result using WRITELN


Result using WRITE

 Writeln outputs each statement on a new line.


 Write outputs each statement on a continuous line until the screen is full, then it will go to a
new line (this program does not contain enough output statements to illustrate it moving to a
new line, but it output on a single line as you can see).
 You can use a combination of write/writeln in you program codes.

Inputting information using Pascal and working with variables.

 So far we have learnt how to output information in Pascal using the write/writeln reserved
words.
 For a program to allow the user to input data/accept data, whatever data is required to be
entered as input, must be stored in a variable. For example, if you want the user to enter his
name, you would need a variable to store the name. The variable can be any sensible name
such as name or Fname.
 If the program requires you to enter 2 names, then you would need 2 variables to store the 2
names. For example, name1 and name2.
 A good method to help you to determine how many variables you need is to refer to the IPO
chart. In the INPUT column, whatever is required to be input, you would need a corresponding
variable for each item.
 Consider the following problem statement and IPO chart.
 Write a program to allow a student to enter his name, maths mark and English mark. The
program should output the student’s name, maths mark and English mark.

INPUT PROCESSING OUTPUT


Name None Name
Maths mark Maths mark
English mark English mark

 From the above IPO chart, you would recognize that there are 3 items we need to input so we
therefore require 3 variables. Note, the same variables used for the input are the same
variables in the output column, so you don’t need separate variables or another 3 variables for
the output. The variables in the input and output columns are all the same.

 When naming variables, there are some rules you should follow.
 The name of a variable -
a. Can start with an underscore “_” or a letter, lowercase or uppercase, such as a letter from a
to z or from A to Z. Examples are Name, gender, _Students, pRice
b. Can include letters, underscore, or digits. Examples are: keyboard, Master, Junction, Player1,
total_grade, _Score_Side1
c. include special characters such as !, %, ], or $
d. Cannot include an empty space
e. Cannot be any of the reserved words
f. Cannot be longer than 32 characters (although allowed)
g. Can consist of one word such as country. A name could also be a combination of more than
one word, such as firstname or dateofbirth.

 Given these rules we can safely use the following variables for the items in the input column above.
o Name – name
o Maths mark - MathMk
o English mark – EngMk.

 The corresponding program for the IPO chart above would be displayed below.
(Refer to program p3 inputOutput)

program inputOutput;

var name:string;

mathMk, engMk:integer;

begin
writeln('what is your name ?');
readln(name);
writeln('Enter your Maths mark :');
readln (mathMk);
writeln('Enter your English mark :');
readln (mathMk);
writeln;

writeln('Your name is ',name);


writeln('Your Maths mark is ',mathMk);
writeln('Your English mars is ',engMk);
readln;
end.

This is what the program will produce after is has been executed.

Copy exe screen


And
Do an explanation table

You might also like