Professional Documents
Culture Documents
On completion of this unit, students should have an understanding of how to convert algorithms to programs using structured
programming techniques.
Assembly language
As with an assembly language program, the Fast: Programs written in machine language
3GL code (instructions) is called the source takes shorter time in execution.
program. Programmers must convert this source
program into machine language before the Translator free: Programs are directly
computer can execute the program. This translation understood by the computer; no translator is
process often is very complex, because one 3GL needed to interpret the code.
source program instruction translates into many
machine language instructions. For 3GLs, Efficient use of main memory
programmers typically use either a compiler or an
interpreter to perform the translation.
A program written in a high-level language is called Readability: The writing of source code in
source code. Since a computer cannot understand HLL does not require the knowledge of the
source code, a program called a compiler or internal working of the computer.
interpreter is used to translate the source program
into a machine language program called an object
program. The object program is often then linked Disadvantages
with other supporting library code before the object
can be executed on the machine. Less Efficient: The HLL are less efficient as
far as computation time is concerned.
The figure below shows the process of compiling,
linking, and running a program. Poor Control on Hardware: Programmers do
not have to know the internal architecture of
computer. As a result program written in HLL
cannot completely use the internal structure.
Application Generator: With application The steps associated with implementing a program
generators, the user writes programs to allow is broken into:
data to be entered into database.
4GLs are designed to be user friendly and 1. Creating source code
interactive, and to help you quickly develop an
application package. In general such products 2. Compiling
are marked by:
3. Linking and executing,
Non-procedural programming code;
4. and maintaining program.
A simple query language;
Centered around database.
3.3.1: Creating source code (using Pascal)
End.
The Instructions Segment
PASCAL recognizes particular reserved words in
The instructions segment tells PASCAL what to do
certain positions in the program, according to the
syntax of the language. You have to be careful with the data structures. It starts with the reserved
when writing your program that you do not use word begin. The first thing that the program does is
reserved words in the wrong context, for example call Readln. Readln is a standard procedure which
consider the implications of a program with the is built into PASCAL. Note that Readln is not a
name begin! reserved word; it simply shows that the writers of
the PASCAL system have recognized that users
need a way of getting information into and out of
The Program Heading
programs. Instead of building this into the PASCAL
standard a number of procedures have been defined
The program heading gives the program a name and which do the donkey work.
tells the PASCAL system what communication it
will have with the world outside. You can pick
whatever name you want, obviously you will
choose one which reflects what the program does.
Another very simple program is shown below: changes to the source code and correct errors in the
program.
3.3.2: Compiling
3.3.3: Linking and executing the program. Dry run: This is a form of testing where the
programmer goes through the program,
Successful compiling does not guarantee either statement by statement doing each operation
correct logic (necessary to get correct "answers") or by hand as it would be done by the computer.
nice input/output. It's the programmer's This is a long process but is a good method of
responsibility to check these matters. detecting logical errors in a program.
The screen below shows the running program after Debugging: This is the process of testing,
execution. locating and correcting mistakes by running
the program.
Program Dec_Variables;
var
3.6.2: Assign initial values
value, counter, quantity : integer;
unit_price, total_amount : real; Assignment statements in programming are used
letter, symbol : char ; to give a value to a variable/constant. The
assignment statement can also change the value of a
first_name, las_name : string;
variable or constant.
Begin
variable_name := value;
End.
or
The ";" at the end of each declaration marks the end
variable_name := expression;
of it, and separates it from the next declaration (if
any).
Example 3.6.3: Assigning values
Note that you do not necessary have to declare all
the variables of a particular type in the same Program Assign_Values;
declaration statement.
Var
number: real;
Declaration of constants
Begin
number := 385.385837;
const
number := number + 467.8;
Identifier1 = value1;
End.
Identifier2 = value2;
Identifier3 = value3;
Program Dec_Constants;
Var
surname: String;
Const
name = 'Victor';
age = 15;
Begin
Program Inputting_data;
Var
3.7.3: Use of Write and Writeln
Num1, Num2 : Integer; In Pascal Programming, the write statement, when
Begin used, leaves the cursor where it is after output is
complete.
Readln(Num1); {inputting}
Readln(Num2); {inputting} In contrast, the writeln statement for output, the
compiler returns the cursor to the next line on
End.
screen.
Real or
Begin
+ Addition Real or integer
integer Readln(Num1);
Subtraction or Real or
‒ negation
Real or integer
integer Readln(Num2);
Real or
* Multiplication Real or integer
integer Sum := Num1 + Num2;
/ Real division Real or integer Real Writeln(Sum); {outputting}
div Integer division integer integer Readln;
mod remainder integer integer End.
Program Lesson1_Program3;
Var
Num1, Num2, Sum : Integer;
Begin {no semicolon}
Write('Enter the first number: '); {prompt}
Readln(Num1);
Writeln('Enter the second number: ');
{prompt}
Readln(Num2);
Sum := Num1 + Num2;
Writeln(Sum);
Readln;
End.
Program Lesson1_Program1;
Var
firstname, surname: string;
Begin
Write('Enter your name:');
Readln(firstname);
Write('Enter your surname:');
Readln(surname);
Writeln;{new line}
Writeln;{new line}
Writeln('Your full name is: ',firstname,'
',surname);
Readln;
End.
Loop statements:
WHILE
REPEAT-UNTIL
FOR
(decision making)
program If_Demo;
Most programs need to make decisions. var
The RELATIONAL OPERATORS, listed below, number, guess : integer;
allow the programmer to test various variables begin
against other variables or values.
number := 2;
The IF – THEN control There are times when you want to execute more
than one statement when a condition is true (or false
The format of IF-THEN control is as follows: for that matter). Pascal makes provision for this by
allowing programmers to group blocks of code
if condition_is_true then together by the use of the BEGIN and END
execute_this_program_statement; keywords.
program If_Demo2;
var number, guess : integer;
begin
number := 2;
program If_Demo3;
var
number, guess : integer;
begin
number := 2;
writeln('Guess a number between 1 and
10');
readln( guess );
if number = guess then
begin
writeln('Lucky you. It was the correct
answer.');
writeln('You are just too smart.')
end
end.
'A' : end
's' :
'S' :
call_subtraction_subprogram;
end;
or, if only a single program statement is to be The REPEAT‒UNTIL statement is similar to the
executed, while loop, how-ever, with the repeat statement, the
conditional test occurs after the loop. The program
while condition_is_true do statement(s) which constitute the loop body will be
program statement; executed at least once. The format is:
The program statement(s) are executed when the
condition evaluates as true. Somewhere inside the repeat
loop the value of the variable which is controlling program statement;
the loop (ie, being tested in the condition) must until condition_is_true; {semi-colon depends on
change so that the loop can finally exit. next keyword}
const
The FOR loop
PI = 3.14;
var
The most common loop in Pascal is the FOR loop.
XL, Frequency, Inductance : real The statement inside the FOR block is executed a
begin number of times depending on the control
condition.
Inductance := 1.0;
Frequency := 100.00;
while Frequency < 1000.00 do
begin
3.9.1: Arrays
Each element of the numbers array is individually
An array is a structure which holds many variables, accessed and updated as desired.
all of the same data type. The array consists of so
many elements, each element of the array capable
Writing to an array
of storing one piece of data (ie, a variable).
An array can be defined as a type, then a working This assigns the integer value 10 to element 2 of
variable created as follows: the numbers array. The value or element number
(actually it’s called an index) is placed inside the
type
square brackets.
array_name = ARRAY [lower..upper] of
data_type;
var Reading from an array
myarray : array_name;
{this creates myarray which is of type array_name }
To assign the value stored in an element of an array
or by using a var statement as follows: to a variable, use
System documentation
User documentation
User documentation is written for the end user and
is concerned with what the program does and how
the end user uses the program.