Professional Documents
Culture Documents
t
u AN INTRODUCTION TO PASCAL
2.1 INTRODUCTION
A Pascal program is usually represented as a plain text (ASCII) file, i.e., a sequence of (ASCII
or keyboard) characters. Such a file is called a Pascal source file. This file is then transformed
by a Pascal compiler into an executable file which is a set machine instructions that are executed
or run by the operating system of the computer. The general process of creating, compiling,
and running a program is as follows :
Let us look at a simple Pascal program to find the area of a rectangle whose width is w and
whose height is h.
program AreaRect;
var
h, w, area : REAL;
begin
h := 12.5;
w := 5.0;
area : h * w;
Writeln(’The height of the rectangle is : ’, h);
Writeln(’The width of the rectangle is : ’, w);
Writeln(’The area of the rectangle is : ’, area);
end.
Chap 2 An Introduction To Pascal 2.2
Editor Create
Source
Program
Source
Source
Program
Compile
Compiler Source
Program
Executable
Program
Operating Run
System Executable
Program
Program
Output
Although it is obvious what the program does, it is not obvious how the program is created. The
program must be created according to strict rules of syntax (grammar) that specify how various
elements of the language are put together to form a valid program. All languages have such
rules, to a greater or lesser extent, but programming languages have strict, unambiguous rules
that the compiler uses when translating a source program into an executable program. Hence,
if a compiler detects something in a source program that breaks a rule it will stop translating
and issue an error message — programmers must stick to the rules whether they like it or not.
Chap 2 An Introduction To Pascal 2.3
The ASCII characters of a Pascal source file are grouped into 4 categories :
1. Letters : a,b,...,z,A,B,...,Z.
2. Digits : 0,1,2,3,4,5,6,7,8,9
3. Special : - * ( ) % $ + = } etc.
4. Control : non-printable
2.2.1 Tokens
The characters of a source file are grouped into tokens which are separated by at least one
space (blank). No spaces are allowed within tokens except for string constants (see below).
Thus tokens are strings of characters with no spaces.
The tokens of a Pascal program are classified into 6 categories :
Symbols
One-Character +, - , *, /, =, <, >, { } ( ) [ ] , ; : ^
and
Two-Character :=, <=, >=, <>, ..
Reserved Words Strings of alphabetic characters that have special meaning within Pascal.
The following are some of the reserved words
array, begin, const, do, end, for ,if, of,
procedure, then, to, type, var, while
Identifiers These are tokens that ‘name’ various elements in Pascal. They are strings of letters
and digits that must begin with a letter. Examples are : Alpha, x, i, X2x, Three3Four4.
We note that Pascal does not distinguish between upper-case and lower-case letters,i.e., the
identifier alpha is the same as ALPHA.
Numbers
123, -45877, 23.45, -0.001
2.3 Syntax
These are the rules by which tokens are combined to form larger elements and ultimately com-
plete programs. We will examine the syntax of the various parts of a Pascal program in detail
later. For the moment we consider the general layout of a program only.
Every Pascal program has the following syntactical structure.
program <name> ;
<CONSTant definition part>; optional
<TYPE definition part>; optional
<VARiable declaration part>; optional
<PROCEDURE definition part>; optional
begin
<statement part> optional
end.
1. Scalar : INTEGER, CHAR, BOOLEAN, REAL, along with subranges of the first
three and enumerated types.
2. Structured : ARRAY, FILE, RECORD, SET, STRING.
2.5 Statements
1. Assignment
2. if
3. while
4. for
5. Procedure and Function Call
6. Compound
We will go into the details of all these elements in later chapters. For the moment we will just
look at examples to see how these elements are used in programs.
Chap 2 An Introduction To Pascal 2.5
2.6 Examples
program Null ;
{This program does absolutely nothing}
begin
end.
program Message ;
begin
Writeln(’This is a program with no input’);
end.
program FarhToCels;
{This program converts Fahrenheit to Celsius}
var Fahr, Cels : real;
begin
Write(’Type in degrees Fahrenheit : ’)
Readln(Fahr);
Cels := (Fahr-32)*(5.0/9.0);
Writeln(Fahr,’degrees F = ’, Cels, ’degrees C.’);
end.
Fahr
68.0
Cels
20.0
Memory
program PayCalc;
{This program calculates the necessary information to print a payslip}
const
PayRate = 11.5;
TaxRate = 0.4;
var
Name : STRING;
Hours : REAL;
Gross : REAL;
Net : REAL;
Tax : REAL;
begin
Write(’Type in name : ’);
Readln(Name);
Write(’Type in hours worked : ’);
Readln(Hours);
TaxRate Name
0.4
PayRate Gross
11.5
Hours Net
Tax
Memory
PROGRAM PayCalcRepeat ;
{This program repeatedly calculates the necessary information to print payslips}
CONST
PayRate = 11.5;
TaxRate = 0.4;
VAR
Name : STRING;
Hours : REAL;
Gross : REAL;
Net : REAL;
Tax : REAL;
Answer : CHAR;
BEGIN
Answer := ’Y’;
WHILE (Answer = ’Y’) DO BEGIN
Write(’Type in name : ’);
Readln(Name);
Write(’Type in hours worked : ’);
Readln(Hours);
Type in the program PayCalcRepeat above and make sure it runs correctly. Then perform the
following alterations :
1. Get rid of the PayRate constant and read it in as a variable so that any pay rate
can be handled by the program.
2. Do the same for the tax rate.
Make sure that you get the program running properly after the first modification before you
attempt the second modification.