The Pascal Programming Language
The Pascal Programming Language
The Pascal programming language was created by Niklaus Wirth in 1968. It was named after
Blaise Pascal, a famous French Mathematician. Some of benefits of Pascal include:
It is well-structured
It is easy to implement
The syntax is easy to lean and follow
It encourages the programmer to adopt a disciplined approach to programming
Declaring Variables
Format:
var
Variable Name: Data Type;
Examples:
var
sum: integer;
x,y: real;
name: string;
Grade: char;
Constants are like variables except that their values cannot change.
Declaring Constants
Format:
const
Constant Name = Value;
Examples:
const
Pi = 3.14;
VAT = 17.5;
Input
The two basic Input functions are Read and Readln, which are used to read data from the
keyboard.
Format:
Readln(item1, item2...)
Examples:
Read(x,y);
Readln(name, score);
Output
The two basic output functions are Write and Writeln. The write statement leaves the cursor at
the end of the current output, whereas the writeln places the cursor at the start of a new line.
Format:
Example:
Output: I like
Information Technology
Example:
Format:
Variable := Expression;
Example:
sum := x + y;
Name := ‘John’;
Program ProgramName;
Const
Constant Declaration;
Var
Variable Declaration;
Begin
Main Body of Program
End.
Reserved Words
The Pascal programming language has several important words in it. These are called keywords
or reserved words. These keywords cannot be used as variable names. Examples of keywords
are: program, label, const, type, var, begin, end, and, array, case, div, do, else, file, for ,
function, goto, if, in, mod, nil , not, of , or, packed, procedure, record, repeat, set, then, to,
until, while, with, read, readln, write, writeln.
Program Square;
{This program finds the square of a number}
Begin
write('Enter number: '); {Prompt for input}
readln(number); {Store data into variable n}
sq:=number*number; {Calculate the square}
writeln('The square is: ', sq); {Output result}
readln;
end.
The if statement allows the conditional execution of one statement, or the choice between
execution of two statements.
Format 1:
If expression then
Begin
Statement(s)
End;
Example:
If x > y then
writeln(x);
Format 2:
If expression then
Begin
Statement(s)
End
Else
Begin
Statement(s)
End;
Program Larger;
{This programs determines and prints the larger of two numbers}
var
a,b: integer;
Begin
if a>b then
writeln(a)
else
writeln(b);
if a = b then
writeln('Numbers are equal');
readln;
end.
Definition: A set of statements which are repeated until some condition is met.
For Loop
Format:
Begin
writeln(‘Enter number’);
Readln( x);
Sq: = x *x;
writeln(Sq);
End;
Program LoopAverageKnown;
{Program finds the average of 3 numbers}
var
x,i,sum:integer;
avg:real;
begin
sum:=0;
for I := 1 to 3 do
begin
writeln('Enter Number');
readln(x);
sum:=sum+x;
end;
readln;
end.
The while loop executes the statements within the loop as long as the condition is true. The
condition is tested at the top of the loop.
Format:
While Expression do
Begin
Statement(s)
End;
Example:
begin
I := 0;
while I <= 5 do
begin
I := I + 1;
Sq:=i*I;
Writeln(sq);
end;
end.
Program AverageUnknown;
{Program finds the average of a set of numbers, the last number is 0}
var
x,i,sum:integer;
avg:real;
begin
sum:=0;
i:=0;
writeln('Enter Number');
readln(x);
while i<>999 do
begin
i:=i+1;
sum:=sum+x;
writeln('Enter Number');
readln(x);
end;
avg:=sum/3;
writeln(avg);
readln;
end.
The repeat until loop is like the while loop except that it tests the condition at the bottom of the
loop.
Format:
Repeat
Statement(s);
Until Expression;
Example:
begin
I := 0;
Repeat
I := I + 1;
Sq:=i*I;
Writeln(sq);
Until I =5;
end.
Program Average;
{Program finds the average of 3 numbers}
var
x,i,sum:integer;
avg:real;
begin
sum:=0;
repeat
i:=i+1;
writeln('Enter Number');
readln(x);
sum:=sum+x;
until i=3;
avg:=sum/3;
writeln(avg);
readln;
end.
Definition: A consecutive group of memory locations that have the same name and type. A
location is referenced by using the array name and the element’s index.
NB The Index type must be ordinal (byte or integer) or an expression that evaluates to these data
types.
Declaring Arrays
Format:
var
Arrayname: Array[Start Index .. End Index] of Arraytype;
Example:
var
numbers: array[1 .. 3] of integer;
NB Elements of numeric arrays are initialized to 0 by default. Elements of string arrays are
initialized to “ ” by default.
Format:
Arrayname[index]: = value;
Example:
Numbers[1]: = 10;
Numbers
Index
1 10
Format:
variablename := Arrayname[index];
Example:
x: = Numbers[1]; {x now contains the value 10}
Program Search;
{Linear search}
var
Accounts: array[1 ..5] of integer;
I, accno: integer;
Begin
I :=1;
While (accno <> Accounts [I]) and ( I <>5) do
I := I +1;
readln;
end.