1 Introduction to Pascal
1.1 Features of Pascal
Creator : Professor Niklaus Wirth
Year : 1971
Features :
- High level language
- English-like
- Highly structured
- Relatively easy to learn
- Purpose : Teaching Tool
Pascal was named in honour of a 17th century French mathematician
and inventor Blaise Pascal. Professor Wirth created Pascal as
a teaching tool and it is now a powerful general purpose
language.
Since 1971 many versions of Pascal have emerged.
Among them are :-
Software developer Pascal
Compiler
Borland Inc. Turbo
Pascal
Microsoft Quick
Pascal
Apple Inc. Macintosh
Pascal
University College UCSD Pascal
of San Diego
On the IBM PC environment, one of the most popular Pascal compiler
is Turbo Pascal. This will be the compiler that will be used throughout
the lessons.
1.2 How to write a Pascal program
In order to create executable Pascal program the following is
needed :
Editor -to create Pascal source code.
Compiler-to convert source code to machine readable form known
as Object code.
Linker -to link the object code and required units in order to
create the executable code.
Compilation Process
The compiler that will be used in this course is Turbo Pascal.
1.3 Pascal program Structure
PROGRAM programname;
USES unitname;
CONST
constant name = constant value;
TYPE
type name = type definition;
VAR
variable name : variable type;
PROCEDURE/FUNCTION name;
begin
statements;
end;
BEGIN
statements;
END.
1.4 Components of a Pascal Program
The MAIN PROGRAM consists of :
Program heading
begins with the word PROGRAM followed by an user defined name
to describe the program.
Main Block
-the rest of the program after the PROGRAM heading.
Declaration
contains CONST, TYPE and VAR to describe various type of data
that will be used by the program.
Statements
the "active" part of the program that consists of program
instructions. Statements are enclosed between the reserved words
BEGIN and END.
Program comments .
Pascal allows the programmer to insert comments or remarks
by enclosing them within braces {...} or (* ... *). Comments can
be inserted at any point in a program. 1.5 Write and WriteLn Statement
WRITE
The WRITE statements is an output statement that will place the
value of one or more expressions on an output device, eg monitor
or printer.
The expressions can be
- strings
- numerical constants
- values of variables or expressions.
Data items must be separated by commas if there are more than
one. WRITE statements displays what you want and leaves the cursor
at the end of the same line.
WRITELN
The WRITELN statement is very simiar to WRITE statement except
that the WRITELN statement results in an end-of-line character
being written after the last data item. It displays whatever you
want and then adds and end-of-line marker (a carriage return and
line feed) at the end of the line. Any subsequent WriteLn statement
will begin on a new line.
The format for the statements is as follows :
WRITE (expression, {expression});
WRITELN(expression, {expression});
Program 1-1
PROGRAM Short;
BEGIN
END.
Program 1-2
PROGRAM First;
BEGIN
WriteLn('This is my first Pascal program');
END.
Program 1-3
PROGRAM UseWriteLn;
USES Crt;
BEGIN
ClrScr; { Clears the Screen }
WriteLn('This is my first Pascal program');
END.
Program 1-4
PROGRAM UseWrite;
USES Crt;
BEGIN
ClrScr; (* clears the Screen *)
Write('One.');
Write('Two.');
END.
Program 1-5
PROGRAM UseWriteWriteLn;
USES Crt;
BEGIN
ClrScr;
WriteLn('One.');
WriteLn('Two.');
END.
Program 1-6
PROGRAM UseWriteLn2;
USES Crt;
BEGIN
ClrScr;
WriteLn('One.');
WriteLn;
WriteLn('Two.');
END.
Explanation:
PROGRAM 1-1.
- This is the shortest way a Pascal program can be written.
- It shows that the reserved words PROGRAM, BEGIN and END. which
are compulsory in any Pascal program.
- Note : In Turbo Pascal, even the heading can be left out leaving
only : BEGIN
END.
This is only applicable in Turbo Pascal, other versions of Pascal
may make the PROGRAM heading compulsory.
- END must terminate with a full stop/period.
PROGRAM 1-2.
- WriteLn allows an output to be displayed on the standard output
device - the monitor.
- Displays one line of sentence and moves the cursor to a new
line.
PROGRAM 1-3
- ClrScr clears the screen and leaves it blank.
- Cursor is moved to the top left hand corner of the screen.
- In order to use the ClrScr procedure, the USES CRT clause
must be inserted after the program heading.
- CRT - library that contains instructions dealing with monitor
displays which include settings the cursor position, changing
screen colors and also clearing the screen.
PROGRAM 1-4
- Write allows an output to be displayed on the standard output
device - the monitor.
- Displays one line of sentence but does not move the
cursor to a new line. Instead the cursor is placed after the last
character of the output on the same line.
PROGRAM 1-5
- Shows the difference between Write and WriteLn.
- WriteLn displays "One" and "Two" on separate
lines.
PROGRAM 1-6
WriteLn without any output parameters will leave an empty line.
VARIABLES and CONSTANTS
VARIABLES
Holds data values which may change during program execution.
CONSTANTS
Holds data which cannot be changed during program execution.
Program 1-7
program Variables;
uses CRT;
const
Pi = 3.14159;
Greeting = 'Hello';
var
I : integer;
R : real;
C : char;
S : string;
begin { main program }
ClrScr;
I := 88;
R := 902.321;
C := 'M';
S := 'This is a string';
WriteLn;
WriteLn('Integer = ', I );
WriteLn('Real = ', R );
WriteLn('Char = ', C );
WriteLn('String = ', S );
WriteLn('The value of Pi is ', Pi:7:5 );
WriteLn('Pick up the phone and say ',Greeting );
WriteLn;
WriteLn('Integer = ', I );
WriteLn('Real = ', R );
WriteLn('Char = ', C );
WriteLn('String = ', S );
ReadLn;
end. { main program }
Identifiers which are declared under the CONST heading represent
constants. The values in the memory cells of constants cannot
change during program execution.
Data Types.
Identifiers declared under the VAR heading refer to variables.
The values in variables can be changed during program execution.
The identifiers must be associated to a certain data type to ascertain
the type of values which the variable can store. The simple data
types available in Turbo Pascal are INTEGER, REAL, CHAR, STRING
and BOOLEAN. These are divided into ordinal and non ordinal types.
Integer
An INTEGER variable can only store round/whole numbers within
the range of -32768 to 32767. Examples of integers include -43,
99, 327, -32768 and 32767. If a program tries to add 1 to 32767
it will give the result -32768. Integers are ordinal types.
Real
REAL variables can store real numbers (with fractions/decimals).
Examples of REALs include 556.912, 5.223100000E+2, -44.2, 1.0,
and 99. The range of values which can be stored by a variable
declared as real is 2.9E-39 to 1.7E+38. These numbers are in exponent
form. Use the field width parameters to format the output of REALs.
Char
The CHAR data type variable can only store one character out of
the 256 characters available in the ASCII table. CHAR variables
are ordinal variables.
Examples of characters are 'A', 'B', '2', '[', '?' and '@'. Characters
are always enclosed within apostrophes.
String
The STRING data type is a collection of CHARacters. The maximum
number of characters which can be stored by a string variable
is 255. The size of a string can be determined during declaration.
If the declaration shows STRING, the string size is 255, if the
declaration shows STRING[80], the string size is 80. Examples
of strings :- 'Hello', 'When the sun shines', 'January', 'Wednesday'
and 'My name is '. To specify a single apostrophe in a string,
two apostrophes are used eg. 'Here''s a phone', 'This is Edwin''s
book.'.
Boolean
The BOOLEAN variable can only store values TRUE or FALSE. They
are normally used as flags to control program execution. Boolean
variables are ordinal types.
Identifiers
Identifiers are names given to various objects in a Pascal program.
The objects include Program name, Variable names, Constant names,
Type names and etc.
Identifier naming rules
The names given to an identifier is created by the programmer.
- First letter cannot be a number or special character.
- No special characters allowed except for underscore (_)
- No spaces allowed.
- Lowercase or Uppercase names make no difference
- There is no limit to the number of characters allowed.
- Reserved words are not allowed.
- Use meaningful variable names.
Program 1-7
program SumAvg3Numbers;
uses CRT;
var
Num1, Num2, Num3 : integer;
Sum : integer;
Average : real;
begin { main program }
ClrScr;
Write('Enter An Integer : ');
ReadLn( Num1 );
Write('Enter Another Integer : ');
ReadLn( Num2 );
Write('Last Integer : ');
ReadLn( Num3 );
Sum := Num1 + Num2 + Num3;
Average := Sum / 3;
WriteLn('The sum is ', Sum );
WriteLn('The average is ', Average :7:5 );
ReadLn;
end. { main program }
READLN.
This statement is used to accept input from the user. The alternative
Read statment is not used for keyboard input but for file
handling (this will be explained later int he lesson involving
files).
Here's another program which uses the READLN procedure to read
several different data types.
Program 1-8
program UseReadLnWriteLn;
uses CRT;
var
A, B, C : integer;
D, E, F : real;
S : string;
Ch1, Ch2, Ch3 : char;
begin
ClrScr;
Write('Enter 3 integers : '); ReadLn( A, B, C );
Write('Enter 3 numbers : '); ReadLn( D, E, F );
Write('Type a sentence : '); ReadLn( S );
Write('Type 3 characters: '); ReadLn( Ch1, Ch2, Ch3 );
WriteLn;
WriteLn('The 3 integers : ',A:7, B:7, C:7 );
WriteLn('The 3 reals : ',D:7:2, E:7:2, F:7:2 );
WriteLn('Sentence typed : ', S );
WriteLn('Characters typed: ', Ch1:2, Ch2:2, Ch3:2 );
ReadLn;
end.
Note:
You will have to include spaces in between 2 numbers as they are
typed. Spaces are used to separate 2 numbers in Pascal. This applies
to INTEGERs and REALs. There are no spaces needed for characters.
Arithmetic Operators.
Another area which must be covered is the ARITHMETIC operators.
Arithmetic operators are used to manipulate numbers. The operators
available in Pascal are:
Operat Function Operand Result Type
or Type
MOD Returns integer integer
the
remainder
of a
division
DIV Returns integer integer
the
quotient
part of a
division.
/ Real real or real
division integer
* Multiplica real or Integer if
tion integer all operands
are integer,
otherwise
real.
+ Addition real or Same as above
integer
- Subtractio real or Same as above
n integer
Program 1-9
program Arithmetic;
uses CRT;
var
A, B : integer;
begin{ main program }
ClrScr;
Write('Enter 2 integers : ');
ReadLn( A, B );
WriteLn( A, ' DIV ', B, ' = ', A DIV B );
WriteLn( A, ' MOD ', B, ' = ', A MOD B );
WriteLn( A, ' / ', B, ' = ', A / B:4:3 );
WriteLn( A, ' * ', B, ' = ', A * B );
WriteLn( A, ' + ', B, ' = ', A + B );
WriteLn( A, ' - ', B, ' = ', A - B );
ReadLn;
end. { main program }
Power of a Number
Pascal does not have an exponent operator so the formula
below is used :
U to the power of V = exp( ln( U ) * V )
eg:
23 = exp ( Ln(2) * 3 )
Program 1-10
program Arithmetic;
uses CRT;
var
A, B : integer;
begin{ main program }
ClrScr;
Write('Enter 2 integers : ');
ReadLn( A, B );
WriteLn( A, ' power ', B, ' = ', exp (LN(A)* B );
ReadLn;
end. { main program }
ReadLn at end of program
If you look closely at the programs, you will notice that there
is a READLN statement placed at the end of every program. This
is done because Turbo Pascal always returns to the editors right
after the program completes execution. If the READLN statement
is not included, you cannot see the results.
Note :
Other Pascal compilers may pause after the program has ended.
Hence, may not require the ReadLn statement.
Back to my homepage