Pascal Progra
Pascal Progra
Lesson 3 Useful Reserved Words of the Crt Unit: ClrScr(), GotoXy(), etc...
Lesson 10 Arrays
Lesson 12 Strings
Programming Basics
In a program, you must always obey the rules of the language, in our case, the Pascal language. A natural
language has its own grammar rules, spelling and sentence construction. The Pascal programming language is a
high level language that has its own syntax rules and grammar rules. As you go along with the lessons, you must
note what you can do and what you cannot do in writing a Pascal program. A very simple program is shown
below:
Program Lesson1_Program1;
Begin
Write('Hello World. Prepare to learn PASCAL!!');
Readln;
End.
The program is written only to display the message : 'Hello World. Prepare to learn PASCAL!!' - an introductory
message that is displayed to you whenever you are going to learn a new programming language. This is simply
shown on the screen. So, to display any message on the screen, you should use 'write' (or 'writeln'). The 'readln'
statement, here is used as to 'stop' the program and wait until the user presses enter. If the 'readln' statement is
missing in this program, then the message is displayed on the screen without giving any chance for the user to
read it and obviously halts! Try running this program with and without the 'readln' statement and notice the
difference. I suggest you see it!! Now, look at this:
Program Lesson1_Program2;begin
Write('Hello World. Prepare to learn PASCAL!!');Readln;End.
This first program is, what is commonly referred to in programming, as 'indented'. Indentation is a must in
writing programs as it aids in the way the code is written ie. neater. Indentation also helps with debugging and
code presentation. You will note how I indent programs.
A program in Pascal always starts by the reserved word 'Program' following the title of the program. There
are various restrictions on how to write this statement. Below is a simple example of a small program.
(Remember: you can copy and paste the program in a text file, save the text file as filename.pas and open it with
Turbo Pascal. The .pas extension is required.)
In the following program, the computer must prompt the user to enter a number, then the latter is added to the
second number input by the user.
Program Lesson1_Program3;
Var
Num1, Num2, Sum : Integer;
Now we must take a look at the program. A program in Pascal starts with the reserved word 'Program' (although
it is not explicitly required) and ends with 'End', following a full stop (this is required though). A full-stop is
never used within the program, except when dealing with records (later topics) and at the end of the program as
seen in the example above.
The 'Var' statement, is used to introduce any suitable variables which will be used later in the program. These
variables are non-constant terms so that they are used in the program for storing values. The terms 'Num1',
'Num2' and 'Sum' in the program are the variables which store any numbers, except those which are real (in fact,
during the execution of the program, a runtime error may occur if a decimal number is input). As you can see in
the example above, these variables are assigned to as integers. The term 'integer' means any whole number, i.e. a
number which is not a decimal number but a positive or negative number. The integer type ranges from -32768 to
32767. So values which are not within the specified range cannot be stored by an integer type. There are other
types which are wider in range, but for now the integer type is enough to hold up our values. The variables
'Num1', 'Num2' and 'Sum' are terms which are not reserved words, but can be used as variables in the program to
store data in them. They could be changed more than once. Moreover, I could have used 'number1', 'number2' and
'totalsum' (note that there must be no spaces within the variables), instead of 'Num1', 'Num2' and 'Sum',
respectively. As you can see, it is much better to shorten the variables than writing long words, such as
'variable_number1'.
After declaring all the variables which are required to be used later in the program, the main program always
starts with the reserved word 'Begin'. Without this word, the compiler will display a diagnostic (error message).
In the program above, both of the two types of 'write' are used. These are 'write' and 'writeln'. Both have the
The messages in between the braces ({ }) are called comments or in-line documentation. I guess you consider
the comments to be 'extra'. Very long programs which include thousands of lines, have already been felt in need
of describing certain functions or even complicated functions. In my experiences, I have already met many
problems, when refusing to write a program for a long time, and then resuming again writing it! I've made a long
time trying to understand what I have done. You must keep it into your mind that comments within the braces are
not read or compiled by the compiler/interpreter.
The 'readln' statement is another reserved word for input purposes, which enables the user to input a number or
text only i.e.: using the keyboard. But in our case the 'readln' statement is used to input numbers only (letters are
accepted but will cause a run-time error because it is not the input we want) and store them in the variables
'Num1' and 'Num2'. This is because both variables are assigned to as integers, and integer variables do not store
strings. A run-time error is detected by the OS (Operating System; ex. Windows or Linux) if something goes
wrong with the input. Later in the course, you will also learn how to control input and output exceptions -
unexpected runtime errors. One last thing on errors is this: there are 2 major error types which are - Runtime
Errors and Compilation Errors. Runtime errors are those which occur unexpectedly during the execution of the
program, whereas a Compilation error is one which is detected during the compilation process. Note that a
decimal number is also considered as a wrong input; a decimal number must not be input, since it is a real
number (more on this later).
After the prompts and inputs by the user, follows the addition. i.e.
The result of the above statement is the addition of the values stored in variables 'Num1' and 'Num2'. The
important thing that you should know is that one cannot make the same statement as follows:
This is another syntax error. It is the fact that transfer of information is from left to right and not from right to
left. So, mind not to make this error. The ':=' is called the assignment statement, and should be discussed later
on.
String Variables
Soon, you should learn how to input text by the user using 'string variables'. The following program is written
showing an example of a string variable, prompting the user to input his name, whatsoever:
Program Lesson2_Program1;
Var name, surname: String;
Begin
Write('Enter your name:');
readln(name);
Write('Enter your surname:');
readln(surname);
writeln;{new line}
writeln;{new line}
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
If we take a look at this program, we notice a new variable type : 'String'. Both the name and surname variables
are of type string. When the program is run and prompts the user to input his name, the name which is keyed in
by the user goes directly to its place in the memory called 'name'. Same occurs to surname. *I'd like to remind
you that the variables 'name' and 'surname' are not reserved words, but are used by the programmer as his
variables. I could have used 'n' instead of 'name' and similarly 'sname' instead of 'surname'. The two 'idle'
writeln's in lines 9 and 10 are used to move on for a new line. In this case, 2 lines are cleared. The next message
displays the full name of the user using the above format. If a string variable is required to be displayed on
screen, it should be put in between inverted commas and commas if it is concatenated with another message;
example:
It could be in the form of: (please note where you must or you must not put the inverted commas following
the commas)
or:
BUT you should put the inverted commas properly (underlined) to close the message.
Program Lesson2_Program2;
Var
surname: String;
Const {the reserved word 'const'
is used to initialize constants}
name = 'Victor';
Begin
Write('Enter your surname:');
readln(surname);
writeln;
writeln;
Writeln('Your full name is: ',name,' ',surname);
Readln;
End.
In the above program, the constant 'name' is assigned to as 'Victor' and is of type string. However, in other cases,
you might have used integer constants (whole numbers), i.e.:
Const
age = 15;
The constant 'age' is a value that could be used whever it is required in a program. Example:
age2 := 15;
The above example shows an addition of the value of the variable 'age' with the value 15. The value of the
constant 'age' remains 15, but the value of the variable 'age2' becomes 30. The assignment statement is not only
used for additions, but is also used to assign a variable: text if it is a string variable or a numeric value if it is an
integer variable.
name := 'victor';
age := 15; {also: "age:='15';" BUT in this case, 'age' is an integer variable}
writeln('Name:',name,'. Age:',age,'.');
I conclude lesson 2 with another simple program for you to read and think about:
Program lesson2_Program3;
Var PD, Dname, Cmodel : String;
TotalKM, CostPD, TCostPD, Distance : Real;
begin
TCostPD := 0;
{note that this is called an 'initialisation'.
It is important to initialise variables to 0
so that it is 'refreshed' from the previous
'rubbish' value in the memory.}
Writeln('This program prompts you to '+
+'input the cost per litre of');
Writeln('the petrol/diesel you spend '+
+'in and the average distance you travel');
Writeln('with your car every week. Then '+
+'the computer calculates the total cost');
Writeln('you spend in fuel every week.');
Readln;
Write('Diesel or Petrol?: ');
Readln(PD);
Write('Name Of Driver: ');
Readln(Dname);
Write('Car Model: ');
Readln(Cmodel);
Write('Cost of Diesel/Petrol: (£) ');
Readln(CostPD);
Writeln('Average distance you travel '+
+'with your car every week: (kilometres) ');
Readln(Distance);
Writeln;
Writeln;
Writeln('Name of Driver:',Dname);
Writeln('Car Model:',Cmodel);
Writeln('Diesel/Petrol:',PD);
Writeln('Average distance covered '+
+'every week: ',Distance:1:2,'Km');
Writeln('Cost of ',PD,' per liter: £',CostPD:1:2,'/litre');
Writeln;
Writeln;
TCostPD := Distance * CostPD;
Writeln('Total cost of ',PD,' per week:'+
+'£',TCostPD:1:2); {note this,}
TCostPD := 0;
Writeln('Total cost of ',PD,' per week:'+
+'£',(Distance * CostPD):1:2); {this}
Writeln('Total cost of ',PD,' per week:'+
+'£',Distance * CostPD); {and this - without ':1:2'}
readln;
End.
Lesson 3 - Special Reserved Words of the CRT Unit: ClrScr(), GotoXy(), etc...
Examples :
7. Halt; / Halt()
Program lesson3_Program1;
Begin
textbackground(brown); {background colour}
ClrScr; {Clear screen with a brown colour.
Try run the program without this!!!}
TextColor(lightgreen); {text colour}
TCostPD := 0;
Writeln('This program prompts you to '+
+'input the cost per litre of');
Writeln('the petrol/diesel you spend in and '+
+'the average distance you travel');
Writeln('with your car every week. Then, '+
+'the computer calculates the total cost');
Writeln('you spend in fuel every week.');
Readkey; {program move on as soon as a key is pressed}
ClrScr;{short for clear screen}
GotoXy(28,3);
{^move to a position on the screen:
x (horizontal), y (vertical)}
Write('Diesel or Petrol? Type p or d: ');
PD := Readkey;
{^as soon as a key is pressed,
it is stored in the variable 'PD'}
GotoXy(30,4);
Write('Name Of Driver: ');
Readln(Dname);
GotoXy(30,5);
Write('Car Model: ');
Readln(Cmodel);
(if you want to see the difference of the 2 programs then you should run them) What is the difference between
this program and the program which is program 3 in lesson 2?
The 'Crt' (short for cathode-ray tube) library has a wide range of functions and procedures that you will use
so frequently. Some of them are listed in the table below. There are many similar libraries, such as 'Strings' (you
will be learning something on this later) and 'Dos'.
Description of the following Reserved Words
Below is a table of the new words:
Example of each
gotoxy(10,10);
Writeln('The position is 10 pixels from the left of the screen, and ten pixels');
Writeln('from the top of the screen.');
readln;
Example 1:
Example 2:
Writeln('1');
Delay(1000);{1000 milliseconds}
Writeln('2');
Delay(1000);
Writeln('3');
Readln;
Note that instructions following 'halt' are not executed since the program terminates when halt is encountered.
The If Statement
1. If..Then..Else
2. Nested If Statements
While-Do Loop
Now, it is time to learn the most important rules of programming: the if statements - decision making, for loops
and the repeat-until loop. Almost, these 3 general programming constructs are common in every programming
language and you have to make sure that when you have finished reading this lesson, make sure that you have
practiced them enough before continuing with learning pascal because they are of outmost importance. If you fall
The If Statement
The 'if statement' executes a the proceeding statement(s) conditionally. This means that if an action comes to be
true, then the statement(s) proceeding the if statement are executed, else these statements are skipped. It works
like this:
OR:
If this happens(action), then do this(reaction, if action is true), else do this(reaction, if action is false).
OR:
If conditional expression then Begin instructions ... End; {if more than one action is required}
Note that you should not use an assignment statement in the 'if' construct, otherwise the compiler will raise a
syntax error. I.e.:
Wrong:
Correct:
Program lesson4_Program1;
Uses Crt;
Label 1; {this is used with a goto statement}
Var Sel: String;
N1,N2, Total : Real;
YN : Char; {this is a character variable type,
which holds single characters ONLY}
Begin
1:Clrscr;
Total := 0; {always initialise integer/real variables}
GotoXy(4,3);
Writeln('1.Addition');
GotoXy(4,4);
Writeln('2.Subtraction');
GotoXy(4,5);
Writeln('3.Exit');
In the above program, the 'goto' statement is used. So far, it has been a real damage to programs and it has
produced unwanted confusions. I strongly suggest you not to use it repeatedly.
-> If..Then..Else
In a normal if statement, the 'reaction' cannot be performed if the condition is not true. But in an
if..then..else statement, there is at least one set of statements to be performed. Let's take a look at the
example below:
Note that if the 'else' term is included with an if statement, then there should be no semi-colon before the
'else' term; just as seen in the above example.
This loop is used to repeat the execution of a set of instructions for at least one time. It is repeated until the
conditional expression is obeyed. The following example, shows the model of the 'repeat-until' loop:
Repeat
..(code)
..(code)
..(code)
Here's an example:
Uses Crt;
Var YN : String;
Begin
Writeln('Y(YES) or N(NO)?');
Repeat {repeat the code for at least one time}
YN := Readkey ;
If YN = 'y' then Halt; {Halt - exit}
If YN = 'n' then Writeln('Why not? Exiting...');
Delay(1800); { wait a second plus 800 milliseconds }
Until (YN = 'y') OR (YN = 'n');
See? It's very simple! In the above program, there is a Boolean expression in the 10th line (or). This will
be described later on.
{code...}
{code...}
End;
Now, an example of the for loop is shown below, but firstly, you should have an idea of the usefulness of the
for loop. Consider the following example:
Program lesson4_Program2a;
Uses Crt;
Begin
Writeln('for loop'); {somewhat boring writing all this!!!}
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Writeln('for loop');
Readln;
End.
Program lesson4_Program2b;
Uses Crt;
Begin
For Counter := 1 to 7 do {it's easy and fast!}
writeln('for loop');
Readln;
End.
Note that the two programs above perform the same function, but which programming style is more useful?
Suppose we have to make a program which designs a small box with some of the characters of the ASCII,
obviously the characters which are most likely to make up a simple box.
Program Program3a_lesson4;
Uses Crt;
Begin
Gotoxy(25,5);Writeln('+');
Gotoxy(25,6);Writeln('I');
GotoXy(25,7);Writeln('I');
GotoXy(25,8);Writeln('I');
GotoXy(25,9);Writeln('I');
GotoXy(25,10);Writeln('I');
GotoXy(25,11);Writeln('+');
GotoXy(26,11);Writeln('-');
GotoXy(27,11);Writeln('-');
GotoXy(28,11);Writeln('-');
GotoXy(29,11);Writeln('-');
GotoXy(30,11);Writeln('-');
GotoXy(31,11);Writeln('-');
GotoXy(32,5);Writeln('+');
Gotoxy(32,6);writeln('I');
GotoXy(32,7);Writeln('I');
GotoXy(32,8);Writeln('I');
GotoXy(32,9);Writeln('I');
GotoXy(32,10);Writeln('I');
GotoXy(32,5);Writeln('+');
GotoXy(26,5);Writeln('-');
GotoXy(27,5);Writeln('-');
GotoXy(28,5);Writeln('-');
GotoXy(29,5);Writeln('-');
GotoXy(30,5);Writeln('-');
GotoXy(31,5);Writeln('-'); {oh my God!!! Phew!}
Readln; { wait for user to read }
End.
Program Program3b_lesson4;
Uses Crt;
A nested for loop is similar to that of the nested if statements. A nested for loop is in the form:
{code..if more than one action, include begin in the second for loop}
The nested for loop is rarely used and it may cause problems.
This type of loop is executed while the condition is true. It is different from the 'Repeat-Until' loop since
the loop might not be executed for at least one time. The code works like this:
instruction 1;
instruction 2;
instruction 3;
etc...
Program Lesson4_Program4;
Uses Crt;
Var Ch : Char;
Begin
Writeln('Press ''q'' to exit...');
Ch := Readkey;
While Ch <> 'q' do
Begin
Writeln('I told you press ''q'' to exit!!');
Ch := Readkey;
End;
End.
*You should have an idea of the 'if statement' before you proceed with this lesson.
...
Now you should note the difference and the intelligent use of the case statement over the if statement.
Program Program1a_Lesson5;
Uses Crt;
Label Return; {used respectively with the
goto statement; beware of it}
Begin
Return: Clrscr;
Writeln('[1].PLAY GAME');
WRITELN('[2].LOAD GAME');
WRITELN('[3].MULTIPLAYER');
WRITELN('[4].EXIT GAME');
Writeln('note: Do note press anything except');
Writeln('numbers; otherwise an error occurs!');
Readln(SEL);
If SEL = 1 then
Begin
Writeln('Are you able to create a game');
Writeln('of yourself using pascal??');
Delay(2000);
Goto Return;
End;
If SEL = 2 then
Begin
Writeln('Ahhh... no saved games');
Delay(2000);
Goto Return;
End;
If SEL = 3 then
Begin
Writeln('networking or 2 players?');
Delay(2000);
Goto Return;
End;
If SEL = 4 then
Begin
Writeln('Exit?');
YN := Readkey;
If YN = 'y' then
Begin
Writeln('Nooooooooooooo...');
Delay(1000);
Halt; {EXIT PROGRAM}
End;
If YN = 'n' then
Goto Return;
End;
End.
Program Program1b_Lesson5;
Uses Crt;
Label Return; {use of the goto statement
is not recommended..avoid it}
Var SEL : Integer;
YN : Char;
Begin
Return:Clrscr;
Writeln('[1].PLAY GAME');
WRITELN('[2].LOAD GAME');
WRITELN('[3].MULTIPLAYER');
WRITELN('[4].EXIT GAME');
Writeln('note: Do note press anything except');
Writeln('numbers; otherwise an error occurs!');
Readln(SEL);
Case SEL of
1 : Begin
Writeln('Are you able to create');
Writeln('a game of yourself using pascal??');
Delay(2000);
Goto Return;
End;
2 : Begin
Writeln('Ahhh... no saved games');
Delay(2000);
Goto Return;
End;
3 : Begin
Writeln('networking or 2 players?');
Delay(2000);
Goto Return;
End;
4 : Begin
Writeln('Exit?');
YN := Readkey;
Case YN of {a sort of a nested case statement}
'y' : Begin
Writeln('Nooooooooooooo...');
Delay(1000);
Halt;
End;
'n' : Goto Return;
End;{End Case2}
End;{Close Conditional Expression 4}
End; {End Case1}
End.
This source code had a syntax error and is now corrected (Vaar changed to Var)
Again this is similar to the if..then..else statement. Study the program below to learn how to use the 'else'
term following the 'case statement':
Begin
Return:ClrScr;
Writeln('Exiting?');
YN := Readkey;
Case YN of
'y' : Halt;
'n' : Begin
Writeln('What are you going to do here, anyway?');
Delay(2000);
Halt;
End;
Else
Begin
Writeln('Either press ''y'' for yes');
Writeln('or ''n'' for no.. please try again..');
Delay(3500);
ClrScr;
Goto Return;
End;
End; {CASE}
End. {PROGRAM}
1. AND
2. OR
3. NOT
1. AND
2. OR
3. NOT
If (x = 0) AND (a = 2) then...
AND
OR
NOT
All of these logical operators have a different effect on the conditional expressions. Let's see what each of the
logical operator does two (or more) conditional expressions...
And
If *1(Str1 = 'a') AND *2(Str2 = 'b') then writeln('Yes, you got it right.');
AND
Expression 2
Expression 1 (result)
true true true
false true false
true false false
false false false
If expression 1 and expression 2 are both true(i.e. the user inputs 'a' and 'b' into variables 'Str1' and 'Str2'
respectively), the message will be displayed. Above is a table showing the possible combinations.
So, from the above table, one can conclude that for a logical operation such as AND, to give out a true result,
both conditional expressions should be true.
OR
If *1(Str1 = 'a') OR *2(Str2 = 'b') then writeln('Yes, you got it right.');
Expression 2 OR (result)
Expression 1
true true true
false true true
true false true
false false false
Either expression 1 or expression 2 should be true to display the message. If for example expression 1 is true and
any other conditional expressions are false, the result is true! Above is a table(the truth table) showing the
So, from the above table, one can conclude that for a logical operation such as OR, to give out a true result, only
one of the conditional expressions should be true.
NOT
Not is almost different from the two logical gates. It only accepts one input and is well-known as the 'inverter'. If
for example the result of two conditional expressions is true, the 'not' operator would invert the result to false! So,
the of the logical operator, 'not', is to output the inverse of the input. The simple truth table for the not operator is:
Input Output
true false
false true
Program Lesson6_Program1;
Uses Crt;
Var n1, n2 : string;
Begin
Writeln('Enter two numbers: (''0'' & ''0'' to exit)');
Repeat
Write('No.1: ');
Readln(n1);
Write('No.2: ');
Readln(n2);
If (n1 = '0') AND (n2 = '0') then Halt(0);
Until (n1 = '0') AND (n2 = '0');
End.
Program Lesson6_Program2;
Uses Crt;
Var n1, n2 : String;
Begin
Writeln('Enter two numbers: (''1'' & ''2'' to exit)');
Repeat
Write('No.1: ');
Readln(n1);
Write('No.2: ');
Readln(n2);
If (n1 = '1') OR (n2 = '2') then Halt;
Until (n1 = '1') OR (n2 = '2');
End.
Program Lesson6_Program3;
Uses Crt;
Var n1 : String;
Begin
Writeln('Enter two numbers: (any number except 0 to exit)');
Repeat
Write('No.1: ');
Readln(n1);
If not(n1 = '0') then Halt;
Until not(n1 = '0');
End.
Example Program:
Program Lesson6_Program4;
Var quit : Boolean;
a : String;
Begin
Repeat
Write('Type ''exit'' to quit:');
Readln(a);
If a = 'exit' then quit := True else quit := False;
If quit = True then Halt;
Until quit = True;
End.
From now on, lessons are becoming somewhat more than basic. The most important thing is that you understand
clearly the material especially the examples shown which are very important and you should keep on practicing
more often.
Procedures
Program Lesson7_Program1;
Uses Crt;
Procedure DrawLine;
{This procedure helps me to
avoid the repetition of steps [1]..[3]}
Var Counter : Integer;
Begin
textcolor(green);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
End;
Begin
GotoXy(10,5);
DrawLine;
GotoXy(10,6);
DrawLine;
GotoXy(10,7);
DrawLine;
There are some differences between these two programs which are very important to note. These are :
It is very important for a program to be small in size. The first program, say, its size is 1900 bytes, but the
second one holds about 1350 bytes!
Neatness
Adopting a neat style of writing for a program helps the programmer (and other future debuggers) to cater
with future bugs. I think that the first program is cumbersome, whilst the other is not! What do you think??!
Repetitions
Repetitions in a program can cause a hard time for a programmer. So procedures are an essential way to
avoid repitions in a program. They also enlarge the size of a program!
Debugging Efficiency
When you are required to debug the program, bugs could be much more easier to find out as the program is
sliced into smaller chunks. You may run the program and notice a mistake at a certain point and which is
located in a particular procedure/function. It would be much more difficult to find a mistake in a program if it
would be one whole piece of code. Do slice your program into smaller chunks, and this needs design of the
whole problem in hand prior to coding. Coding (or writing up your program) is just one section of the whole
software developement process. The whole Software Developement Process is an important part of
nowadays scientific computing.
Returning back to program Lesson7_Program1b, the gotoxy statement before the DrawLine; could be
"kicked off" so that we can avoid the repetition of the gotoxy! We cannot build up another procedure for the
gotoxy, but it should be done by adding parameters with the procedure. The new program is as follows:
Program Lesson7_Program2;
Uses Crt;
Procedure DrawLine(X : Integer; Y : Integer);
{the decleration of the variables in brackets are called
parameters or arguments}
Var Counter : Integer;
{normally this is called a local variable}
Begin
GotoXy(X,Y); {here I use the parameters}
textcolor(green);
For Counter := 1 to 10 do
Begin
write(chr(196));
End;
End;
Begin
Now, this program includes a procedure which uses parameters. Every time it is called, the parameters can
be variable, so that the position of the line could be changed. This time, we have also eliminated the gotoxy
statement before every DrawLine statement. The numbers in the brackets of the DrawLine are the
parameters which state the position of the line. They also serve as a gotoxy statement.
When you apply parameters to a procedure, variables should be declared on there own, and must be
separated by a semi-colon ";". They are put in between the brackets, following the procedure name. The
variables (known as the parameters) should be used by the procedure/sub-program only.
I made another program, which prompts the user to enter his/her favourite text colour and background
colour, which would be used to write text (in his/her favourite colours) further in the program.
Program Lesson7_Program3;
Uses Crt;
Var
UName, USurn, UCoun, UMail : String[50];
{These var's are global because
they are used by more than one procedure}
TxtB, TxtC, i : Integer;
InfoCor : Boolean;
Procedure EnterUserInfo(TxtCol : SmallInt; TxtBck : SmallInt);
Begin
textcolor(TxtCol);
textbackground(TxtBck);
ClrScr;
Write('Your Name: ');
Readln(UName);
Write('Your Surname : ');
Readln(USurn);
Write('Country : ');
Readln(UCoun);
Write('E-Mail Address: ');
Readln(UMail);
Write(' Thank you for entering your personal information!!');
Readkey;
End;
Procedure ConfirmationField(TxtCol : SmallInt;
TxtBck : SmallInt);
Var
YN : Char; { a local variable }
Begin
textcolor(TxtCol);
textbackground(TxtBck);
ClrScr;
Writeln('Your Name: ',UName);
Writeln('Your Surname : ',USurn);
Writeln('Country : ',UCoun);
Writeln('E-Mail Address: ',UMail);
Writeln;
Parameters of procedures may be varaible. In this case, data may flow through the variable in both ways.
What I am trying to say is that you can pass data and get data through the procedure using a variable
parameter. Here is a declereation of a variable parameter:
Here is an example of how to use a varaible parameter and what's its purpose:
Program VAR_PARAM_EXAMPLE;
Var
Res : Integer;
Begin
Writeln('The square of 5 is: ');
Square(5, Res);
Writeln(Res);
End.
Functions
The second type of sub-program is called a function. The only difference from the procedure is that the
function return a value at the end. Note that a procedure cannot return a value. A function start and end in a
similar way to that of a procedure. If more than one value is required to be returned by a module, you
should make use of the variable parameter. A function can have parameters too. If you change the sub-
program from procedure to a function, of the previous program, there will be no difference in the output of
the program. Just make sure which one is best when you can to implement a module. For example, if you
don't need to return any values, a procedure is more best. However if a value should be returned after the
module is executed, function should be used instead.
Program Lesson7_Program4;
Uses Crt;
Details: the function returns a real instead an integer; if..then..halt removed; repeat..until condition is also
changed
If you feel that Graphics is not important for you than skip to lesson 9 (File Handling). BGI Graphics is an
advanced topic and need not be explicitly covered. It depends on your preference. Graphics lovers will surely be
interested in this topic.
One might require the file egavga.bgi during this graphics tutorial. Click here if you don't have this file (or it is
corrupt) and place it in the BGI directory in the Turbo Pascal installation folder.
COPYRIGHT NOTICE: It is assumed that you have bought Turbo Pascal once you have downloaded this file.
This file is owned by its respective owners (Borland Company).
IMPORTANT DISCLAIMER: You will download and use this file at your own risk. We don't take any
responsability for any damage whatsoever.
Introduction to Graphics
Graphics is a method of representing output material in a more complex, user-friendlier way than the crt. The crt
is one of the few turbo pascal units or libraries. Turbo Pascal has a graphics library with which you can represent
The famous piece of code, used to initialise the graphics in Turbo Pascal (NOT Dev-PASCAL! Mind you!
Dev-Pascal does not accept the graph driver and graph mode be declared as integer values. If you have Dev-
Pascal then change all the variable types of the 'gd,gm' to smallint! 'Gd, Gm : Smallint;') is shown below:
Program Lesson8_Program1;
Uses Crt,Graph;
Var GraphicsDriver, GraphicsMode,
ErrCode : Integer;
{two var's are needed for initialisation}
Begin
Writeln('Initialising Graphics, please wait...');
GraphicsDriver := Detect;
InitGraph(GraphicsDriver, GraphicsMode,'');
{IMPORTANT, read the following or
otherwise graphics will not work!! ;)}
(*between the inverted commas,
type in the path of the graphics BGI file
(usually 'C:\TP\BGI'),
OR
change the dir in the file menu (PRESS Alt+F)
and roll down your mouse pointer to the 'change dir'
menu; then either type the path to the BGI file,
or go to C: -> TP -> BGI*)
ErrCode := GraphResult;
If GraphResult <> grOK then { <> means 'not equal to' }
Begin
ClrScr;
Writeln('Graphics error occured: ',
GraphErrorMsg(ErrCode));
Writeln('If a file not found error is displayed above');
Writeln('then, change the dir from the current');
Writeln('location to C:\ -> TP -> BGI, '+
+'from the file menu!');
Readln;
Halt(1);
End Else
Begin
Randomize;
SetColor(Random(15) + 1); {Set text colour}
{Output text at 20 pixels from the top of the screen,
and 20 other from the left side of the screen.}
OutTextXY(20,20,'Welcome to the new generation
of Pascal Programming:');
OutTextXY(20,30,'Pascal Graphics!!');
OutTextXY(25,70,'You will learn more
graphics procedures and');
OutTextXY(25,80,'functions, later in this lesson :-)');
Readln;
InitGraph(GraphicsDriver,GraphicsMode,'');
You may find it strange what does the two inverted commas (' ') mean, in the statement shown above. It is
the path which redirects the turbo pascal linker to the graphics BGI file. So, in between the inverted
commas, you have to enter the path to the graphics driver, or otherwise you will receive an error message.
You may leave it blank and let the compiler himself find the BGI file automatically (in the current directory),
but when I did this, it never worked!! :-) If you did not change the folder address (C:\TP) after the
installation, you may use this path: 'C:\TP\BGI', like this:
InitGraph(GraphicsDriver,GraphicsMode,'C:\TP\BGI');
After the 'initgraph()' statement, you should inform the user with the problem concerning a graphics error, if
the EGAVGA.BGI file is not found. Hope it does not happen to you :-) !! Then, if the user is error-free, then
you can start calling graphics procedures and functions from the graphics library (graph). In the example
program above, I used only the 'OutTextXY()' statement and the 'SetColor()' statement. However there are
many more functions other than these!
At the end of each graphic statments, you have to close the graphics section, by using the 'CloseGraph'
statement.
USEFUL INFORMATION:
Note that the screen resolution is 640 by 480 pixels (compare this with that of the CRT! - the CRT has a
screen resolution of 80 by 25 'pixels' only!)
Graphics Statements
Learning graphics, is very simple - all you have to learn is how to initialise graphics and then use your
graphics knowledge to display whatever you want! Now, the following statements are found in the graph
unit which are used to output graphically. Then I will show you an example program using the graph unit, to
demonstrate how the statments below work:
Below is a really fantastic graphics program, which uses most of the graphics statements above. If you could
not run the program because of a graphics error, then send me an e-mail and I will help you how to do so.
But first of all, try the path of the BGI file as shown above. You can download or copy this program:
Program Lesson8_Program2;
{Author: Victor J. Saliba}
{[email protected]}
{Website: http://pascal-programming.info/}
Uses Crt,Graph;
Have you successfully ran the program above? How's it? There are many different animations! I used some
statements which you are'nt still familiar with. But, sooner or later, you will get to know how to use them
and know their function.
There are many other graphics procedures and functions, which do not draw graphic objects, but manipulate
the format of the graphic objects you draw. For example, if I want to fill a circle with a colour, it's like this:
The command 'FillEllipse' will fill the circle with the current colour. Take a look at the table below:
SetFillStyle(XHatchFill,LightCyan);
Changes the style of the filling of an
SetFillStyle(Pattern, object. It can only be used with
OR
Colour); bar/bar3d and pieslice. It has 13
different fill styles.
SetFillStyle(10,3);
It is useful to try to run the program below yourself so as to understand the meaning of several reserved
words included in certain functions, such as:
SetTextStyle(SansSerifFont,VertDir,2);
OR
Bar3D(40,50,60,100,50,TopOn);
Program Lesson8_Program3;
Uses Graph;
Var Gd, Gm: Integer;
Begin
Gd := Detect;
InitGraph(Gd, Gm, ' ');
if GraphResult <> grOk then Halt(1);
SetTextStyle(TriplexFont, HorizDir, 1);
SetUserCharSize(2,1,10,1);
SetTextJustify(CenterText, CenterText);
OutTextXY(GetMaxX Div 2,200,'Is it big enough?');
readln;
ClearViewPort;
SetTextStyle(SmallFont, VertDir, 5);
SetTextJustify(LeftText,CenterText);
OutTextXY(200,250,'VertDir (vertical) Direction.
Do you understand now??');
Readln;
ClearViewPort;
Bar3d(150,80,450,330,50,TopOn);
SetTextStyle(SansSerifFont, HorizDir, 2);
SetColor(LightRed);
OutTextXY(130,410,'BAR3D(150,80,350,330,50,TopOn);');
Readln;
ClearViewPort;
This is just the beginning of the graphics adventure. If you would like to be a guru in graphics, just keep on
practicing and produce only graphical programs.
Intro
A file contains data which is saved in the hard disk. You can only view a file from the hard disk by using an
operating system. A can contain text data which is used by word processors. Many text files are saved in the hard
disk with the extensions: *.txt and *.doc. The file with extension of *.txt, means that it is created by the Microsoft
Notepad. Whenever you use the Microsoft Notepad and save a text file, this saved file is created and written on to
the hard disk. However, some programs have various formats on how to maintain the text - such as justification,
font, font colour. So many files contain more data other than text data! The program itself uses various techniques
to create a file in such a way that, when it is being read again it can read (using programming skills, etc..) if it is
justified, its font style etc..
The program that is able to read the file of extension *.xxx, is called the viewer of that file!
Before you go on with files, please make sure that you have enough hard disk space for our experiments, since I
am going to demonstrate to save a file to the hard disk!! :) Only a few kb, however..
Reading a file in pascal is very easy. Note that there are no reserved words in Pascal that are used to to
read or write to a file. We used the 'usual words': readln() and writeln(); Here's the technique of how to read
a text file (only):
Program Lesson9_Program1;
Var UserFile : Text;
FileName, TFile : String;
Begin
Writeln('Enter the file name '+
+'(with its full path) of the text file:');
readln(FileName);
{A .txt file will be assigned to a text variable}
Assign(UserFile, FileName + '.txt');
Reset(UserFile); {'Reset(x)' - means open the file x}
Repeat
Readln(UserFile,TFile);
Writeln(TFile);
Until Eof(UserFile);
Close(UserFile);
Readln;
End.
Details: the Assign()function on line 10 has been changed from Assign(UserFile, FileName, + '.txt') to Assign(UserFile,
FileName + '.txt') because the comma ',' after 'Filename' is a syntax error.
Program Lesson9_Program2;
Var FName, Txt : String[10];
UserFile : Text;
Begin
FName := 'Textfile';
Assign(UserFile,'C:\'+FName+'.txt'); {assign a text file}
Rewrite(UserFile); {open the file 'fname' for writing}
Writeln(UserFile,'PASCAL PROGRAMMING');
Writeln(UserFile,'if you did not understand something,');
Writeln(UserFile,'please send me an email to:');
Writeln(UserFile,'[email protected]');
Writeln('Write some text to the file:');
Readln(Txt);
Writeln(UserFile,'');
Writeln(UserFile,'The user entered this text:');
Writeln(UserFile,Txt);
Close(UserFile);
In the above program, I am using the 'writeln()' statement so that I write to the file I have previously
assigned to. Note that, since I am using writeln(), there is no output to the screen, it goes to the file I
initialised.
To check exactly what has just been written to this file, go to C:\, and see if there is a file named:
Textfile.txt. Open it and see what does it contain!! :-). If you had problems, or if you did not have a way
out, then in the start menu go to:
start -> find -> files or folders => look in drive C: and enter 'textfile.txt' in the text box; double click on it.
Writing to an existing file, means, open a file and add extra data, but not overwrite the file. Some beginner
programmers do not actually understand how to, not overwrite a file with data they would like to input. This
is the common problem:
Copy the program above in a text file editor and save it in the turbo pascal bin folder as 'example.pas'.
Open Turbo Pascal, and run this program for two times or more. Close Turbo Pascal and find the file named
'addtext.txt'. Is there more than one sentence?
A new reserved word which works with files, can complete our problem. The new reserved word is
'append(f)', where f is a variable of type text. This can be done by simply change the 'Rewrite(UFile)' to
'append(UFile)', and the text file is not overwritten, but appended! The above program changes to the
following:
Delete Files
In Pascal, the reserved word used to delete files from the hard this is the 'Erase(f)' where f is a variable of
any data type. This means that 'f' could be both file and text variable type. Note, that the file you are about
to delete is not taken in the recycle bin, but it is directly kicked off the hard disk!!
Unlike any other file functions, the erase() function does not open the file to delete it, so you don't need to
Example Program:
Compiler directives are used to adjust the compiler settings during the compilation process, so that
programmers can control their program in their preferrable way. Compiler directives are declared using the
symbols '{', '$' and '}'. Note that the braces are used for comments, but still, '{$something}' is taken to be
a directive. Instead of 'something' the programmer should replace it by a known directive (a letter). Now,
the '{$I}' is used to tell the compiler not to take into consideration I/O errors including file operations. Say,
for example, if a file is trying to be opened, but it does not exist, the program does not stop from executing
and halts with a runtime error, saying: 'File not found' or whatever boring error! Make sense? So, if you
apply this compiler directive before you open the file, either for writing or reading, I/O errors will not cause
the program to raise a runtime error forced halt.
Now, I must teach you how to use this compiler directive by letting you know where to put it and how to
cater with the error that might occur. It must be placed where you are going to open or delete a file. ie.:
{$I-}
action on file...
{$I+} { enable the i/o error check again }
It is important to enable again the i/o error check, after you have disabled it, so that any unknown future
errors would be automatically encountered. However, if you think that it would be better to disable i/o error
checking for the entire program, you can! Just apply the directive at the beginning and that's it but it is not
recommended since during the runtime of your program you will not be notfied of IO errors that might
occur!! After you control a file action, you must check whether an error has occured or not by using the
system function 'IOResult', which returns IO error information. This is a typical traditional exception
handling. You should test for errors using the statement:
The IOResult function should be explicitly used after an IO error check so that it will automatically clear the
error flag of the system otherwise, the IO error cause a 'mutation' to other IO processes resulting into a
runtime error. You are not required to grasp each word perfectly from what I said. The most important thing
is that you include that statement if the IO directives are to be used and everything would be fine. Now I
should show you how to use it by an example:
Program Lesson9_Program3;
Uses Crt;
Var t : Text;
s : String;
Begin
Assign(t,'C:\ABC.DEF');
{$I-} { disable i/o error checking }
Reset(t);
{$I+} { enable again i/o error checking - important }
If the required file is found the IOResult returns a 0 value, meaning no errors; ELSE if not found (IOResult
returns a non-0 value) display an error message!
IMPORTANT: if the file is successfuly found, the file is opened and you should close it as shown in the
program above. However, if it is not found, the common sense says that it couldn't be opened because it is
not found!?! So, you should not include a 'close(..)' after it is not opened and this is done conditionally as
shown in the example. Study carefully my program and run it several times with good paths and non-good
ones to see well the difference and how does the compiler directive works!!
Hopefully, the compiler directive could be applied to different functions, similarly as in the program above.
You can use it with 'rewrite()', 'append()', 'erase()', and also 'FSearch()'. You can try as many programs as
you wish and hopefully practicing the IO directive. I know, that this directive is somewhat complex and hard
to be understood but undoubtedly useful! I am there waiting for your e-mails, if you think that your program
does not work properly when using this directive.
In Pascal, there are functions with which you can either create or remove a directory from the hard disk. To
create a directoy, we use the function 'createdir(c)' where 'c' is of type PChar. Ohh, that's sound kinky...
What the heck does PChar means? Hehe, PChar is a pointer variable which holds the address of a dynamic
variable of a specified type. It is sort of a pointer for characters. Before you create the directory, however,
you have to check if the directory exists, else a runtime error shows up fiddling in front of your monitor!! :-)
The directory is created as follows:
NewDir := FSearch('C:\Pascal Programming',GetEnv(''));
if NewDir = '' then CreateDir('C:\Pascal Programming');
From only two lines of code, we have been able to create a dir. But, before you go experimenting on your
own, you should strictly read the following documented example: (I will explain in detail the 'FSearch()'
function)
Program Lesson9_Program4;
Uses WinDos, Dos;
{ note the inclusion of the 'windos.tpu' library }
Begin
{ search for the dir }
NewDir := FSearch('C:\Pascal Programming', GetEnv(''));
{ create a new one, if it does not exist }
if NewDir = '' then CreateDir('C:\Pascal Programming');
Assign(F,'C:\Pascal Programming\pprogramming.txt');
The variable type, 'PathStr', is new to you, and this is a variable defined in the 'dos.tpu' library. So, you have
to include the 'dos' library at the beginning of your program. The 'FSearch()' function is implemented also in
the windos library to search in a dir list. So, it was useful in our program and any other program to search if
the dir exists or not. It's not important to know exactly what does 'GetEnv()' mean, which is found in the
second parameter of FSearch(). The Borland Turbo Pascal reference says that its function is to return the
value of the specified function. Note that 'FSearch()' is implemented in the 'dos.tpu' library, while
CreateDir()' is implemented in the 'windos.tpu' library.
To remove a directory, it is quite simple. Just add 'remove()' at the end of your program! :-) Your operating
system will automatically erase the dir if it exists. It doesn't matter if you 'remove' a dir which does not
exist. Hopefully, no runtime errors, will show up! The 'remove(Pch)' function is also implemented in the
windos library, where 'Pch' is a variable of type PChar, again .
Now, for the fun stuff!! To return the size of a file, simply declare a variable of 'longint' type, and assign it to
the filesize of the file. A file variable of type byte, should be assigned to the file which you would like to
return its size, using 'assign()'.
Program Lesson9_Program4;
Var f : file of byte; { file var of type byte }
sz : longint; { var for the size }
Begin
Assign(f,'C:\anyfile.txt');
{$I-} Reset(f); {$I+}
f (IOResult <> 0) then
Begin { file found? }
Writeln('File not found.. exiting');
Readln;
End Else
Begin
{ Return the file size in Kilobytes }
sz := round(FileSize(f)/1024);
Writeln('Size of the file in Kilobytes: ',sz,' Kb');
Readln;
Close(f);
End;
End.
Lesson 10 - Arrays
An Array is a powerful data structure that stores variable data having the same data type. It is just like a
small fixed number of boxes linked together one after the other storing things that are related to each other.
An array is said to be a static data structure because, once declared, its original size that is specified by the
programmer will remain the same throughout the whole program and cannot be changed.
Up until now, we have used single variables only as a tool to store data. Now we will be using the array data
structure and here is how it is declared:
Var
An array data structure defines the size of the array and the data type that it will use for storing data. In the
above example, the array stores up to 20 integers however I may have used 30 integers or more. This size
depends on your program requirements.
Arrays are used just like ordinary variables. They are used to store typed data just like the ordinary
variables. You will now learn how to assign data to arrays and read data from arrays.
In the example above, I have declared 20 integers and I should be able to access each and one of them and
here is how I do it.
myArray[5] := 10;
myArray[1] := 25;
You just take the array in subject, specify the index of the variable of the array and assign it a value relevant
to the data type of the array itself.
Var
myVar : Integer;
myArray : Array[1..5] of Integer;
Begin
myArray[2] := 25;
myVar := myArray[2];
Just like ordinary variables, arrays should be initialised, otherwise scrap data will remain stored in them. If
we want to intialise 2 whole 20-sized integer and boolean arrays to 0 and false respectively, we do it like
this:
Var
i : Integer;
myIntArray : Array[1..20] of Integer;
myBoolArray : Array[1..20] of Boolean;
Begin
For i := 1 to 20 do
Begin
myIntArray[i] := 0;
myBoolArray[i] := false;
End;
End.
Now that we have used various built-in data types, we have arrived at a point were we want to use our
defined data types. Built-in data types are the ones we used lately, such as Integer, Boolean and String.
Now we will learn how to specify our own customised data types and this is just how it is done:
Type
<myDataType> = <particularDataType>;
The "Type" keyword is a reserved Pascal word used to define our own data types. So you start defining your
own data types by using this keyword. After defining your own data types, you may start using them just
like the other built-in data types as follows:
Var
<myVar> : <myDataType>;
Now lets define a new simple data type and note how it will be used in the program below:
Type
nameType = String[50];
ageType = 0..150; { age range: from 0 to 150 }
Var
name : nameType;
age : ageType;
Begin
Write('Enter your name: ');
Readln(name);
Write('Enter your age: ');
Readln(age);
Writeln;
Writeln('Your name:', name);
In the above example we defined a String[50] and a 0..150 data type. The nameType only stores strings up
to 50 characters long and the ageType stores numbers only from 0 to 150.
We can define more complex user-defined data types. Here is an example of more complex user-defined
data types:
Type
i = 1..5;
myArrayDataType = Array[1..5] of Byte;
byteFile = File of Byte; { binary file }
Var
myArrayVar : myArrayDataType;
myFile : byteFile;
Begin
Writeln('Please enter 5 number from (0..255): ');
For i := 1 to 5 do
Readln(myArrayVar[i]);
Writeln('You have entered the following numbers: ');
For i := 1 to 5 do
Writeln('Number ',i,': ',myArrayVar[i]);
In the above example I showed you how to incorporate arrays as user-defined data types. Note that you
may use user-defined data types more than once.
2 Dimensional arrays and multi-dimensional are arrays which store variables in a second or nth dimension
having n*m storage locations. Mutli dimensional arrays including the 2 dimensional array, are declared by
using multiple square brackets placed near each other or using commas with one sqaure brackets as an
alternative. Here is how multi-dimensional are declared:
my2DArray : Array[i..j][k..l] of <DataType>;
myMultiDimArray : Array[m..n][o..p][q..r][s..t]... of <DataType>;
Let us have the 2 dimensional array defined first. Think of a grid where each box is located by using
horizontal and vertical coordinates just in the example below:
1 2 3 4 5
3 3,4
5 5,3
Let us declare an array having 3 by 5 dimensions, assign a value to a particular variable in the array and
illustrate this on a grid just like the one above:
Var
my2DArray : Array[1..3][1..5] of Byte;
Begin
my2DArray[2][4] := 10;
End.
Having the vertical axis as the 1st dimension and the horizontal one as the 2nd dimension, the above
example is illustrated as follows:
1 2 3 4 5
2 10
Multi-dimensional arrays are rare and are not important. The single and 2D dimensional arrays are the 2
most frequent dimensions.
The following example is a bit more complex example than the previous examples and it also uses a 2
dimensional array to illustrate their use.
Uses Crt;
Type
myRange = 1..5;
arrayIntType = Array[myRange] of Integer;
myFileType = File of arrayIntType;
Var
i : myRange;
myFile : myFileType;
{ the next array is 2 dimensional }
arrayInt : Array[1..2] of arrayIntType;
Begin
Clrscr;
Randomize;
For i := 1 to 5 do
Begin
arrayInt[1][i] := Random(1000);
Writeln('rand num: ',arrayInt[1][i]);
End;
For i := 1 to 5 do
Writeln(i,': ', arrayInt[2][i]);
Readln;
End.
This concludes the arrays lesson. In the next lesson we will learn about Record data structures, their uses
and we will also learn how to use binary files used record data structures.
A record is a special type of data structure that, unlike arrays, collects different data types that define a
particular structure such a book, product, person and many others. The programmer defines the data
structure under the Type user definition.
Let us take the case of defining a book using a record data structure. The main entities of a book are its
title, author, unique ISBN number and its price. This is how it is defined in Pascal and in most other
programming languages:
Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;
Var
myBookRec : TBookRec;
That's it! Note that I could have used this structure as a variable by declaring it under Var. When type-
defining it, you can declare as many TBookRec variables as you wish.The entities of the record (title,
Now, you will see how we access the fields of the record by assigning them values and retrieve them back
later in the following small program.
Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;
Var
myBookRec : TBookRec;
Begin
myBookRec.Title := 'Some Book';
myBookRec.Author := 'Victor John Saliba';
myBookRec.ISBN := '0-12-345678-9';
myBookRec.Price := 25.5;
The with keyword is a quick way in accessing fields of a record. Although it helps with quick field evaluation,
it is rarely used since it has a lack of readability and increases ambiguity when coming to distinguish
between a variable having the same name as one of the field names of a record within a with statement.
Taking a snippet from the previous example, this is how the code above transforms when incorprating the
with keyword.
With myBookRec do
Begin
Title := 'Some Book';
Author := 'Victor John Saliba';
ISBN := '0-12-345678-9';
Price := 25.5;
End;
It may become very useful when records are required to be passed through arguments and this will be
demonstrated shortly. I will use the same data structure, pass it by reference as a parameter and return the
value back through the parameter also.
Var
bookRec : TBookRec;
Begin
EnterNewBook(bookRec);
Writeln('Thanks for entering the book details');
DisplayBookDetails(bookRec);
Readln;
End.
Arrays of Records
Records may be stored in arrays and this will become very useful and it is not that difficult to manage. We
will use an array of records to store a number of different books and by using this example, it will be
immensely indicative to learn how to use them.
In the following example I will use the procedures above to store 10 different books from input and then
output only one chosen record to display it back to screen in order to demonstrate how to access a record
from an array.
Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Var
bookRecArray : Array[1..10] of TBookRec;
i : 1..10;
Begin
For i := 1 to 10 do
EnterNewBook(bookRecArray[i]);
Writeln('Thanks for entering the book details');
Write('Now choose a record to display from 1 to 10: ');
Readln(i);
Writeln('Here are the book details of record #',i,':');
Writeln;
Writeln('Title: ', bookRecArray[i].Title);
Writeln('Author: ', bookRecArray[i].Author);
Writeln('ISBN: ', bookRecArray[i].ISBN);
Writeln('Price: ', bookRecArray[i].Price);
Readln;
End.
Note that you can also use arrays within records and this time the square brackets go on with the field name
instead of the record. Also you can embed records within records. More dots will be required to access
deeper records.
Records can also be stored into files and this could be done by using binary files. I will demonstrate storing
records into files by continuing from the previous example. Using binary files could be very handy, fast and
more reliable over text files. You can't afford storing hundreths of files by using text files since it becomes
confusing and even slower for the computer to process and read/write from/to the file.
In the following example I will use a file of the book record I have created and then store as many books as I
want in the file using the binary file system. Watch carefully how I will create the file of record and how I will
perform the file I/O for the binary file system. Also, I will make use of special built in functions that help me
position the file pointer to the record I want.
Note that with binary files, only Read and Write are allowed to read/write fro/to a file.
Type
Str25 = String[25];
Var
bookRecArray : Array[1..10] of TBookRec;
tempBookRec : TBookRec;
bookRecFile : File of TBookRec;
i : 1..10;
Begin
Assign(bookRecFile, 'bookrec.dat');
ReWrite(bookRecFile);
For i := 1 to 10 do
Begin
EnterNewBook(bookRecArray[i]);
{ bookRecArray[i] now contains the book details }
Write(bookRecFile, bookRecArray[i]);
End;
Close(bookRecFile);
Writeln('Thanks for entering the book details.');
Writeln('They are saved in a file!');
Write('Now choose a record to display from 1 to 10: ');
Readln(i);
ReSet(bookRecFile);
Seek(bookRecFile, i-1);
Read(bookRecFile, tempBookRec);
Close(bookRecFile);
Writeln('Here are the book details of record #',i,':');
Writeln;
Writeln('Title: ', tempBookRec.Title);
Writeln('Author: ', tempBookRec.Author);
Writeln('ISBN: ', tempBookRec.ISBN);
Writeln('Price: ', tempBookRec.Price);
Readln;
End.
The example program above demonstrated the use of the seek function. It's role is to place the file pointer
to the desired position. The first component of the file is marked as 0. So you have to keep in mind that if
you have a counter starting from 1, you have to decrement it by 1 to obtain the actual record you want.
The seek function is very important and has an important role in binary file system. Here are some uses of
the function and how it can be used effectively to obtain a particular position of the file.
Seek(myFile, 0);
Seek(myFile, FileSize(myFile)-1);
Seek(myFile, FileSize(myFile));
Seek(myFile, FilePos(myFile)+1);
When trying to access from a file position that is beyonf the file limits, a runtime error is automatically raised.
Try to avoid this type of error. This may be caused because you might have looped through the file and kept on
looping beyond its limits. Note that Seek(myFile, -1) is a typical runtime error becuase -1 position does not exist.
0 is the least and the first record in the file. Note that FilePos is also very useful and it returns the current positon
of the file. Please note that FileSize returns the number of components in the specified file and not the size in
Bytes. If the file is empty, 0 is the returned value. On the other hand, if the file contains 5 records (ie. 0 to 4), 5 is
returned.
The structure of a binary file is just like blocks being stored contiguosly in a line. Think of boxes being placed
one adjacent the other and each one of them has data. There is a space between this boxes that indicates the file
positon and we can easily depict this fact below.
The first row is the actual file showing the indexes of each record block and the second row shows the file pointer
ie. the file position. The current file position shown in the illustration above is relevant to Seek(myFile, 1). Now
you have been assured that the number 1 record of a file is not the first record of the file. After the last record,
there is an EOF marker that indicates the end of the file and it is not legal to go beyond this point except for only
one position to allow appending ie. Seek(myFile, FileSize(myFile)).
This concludes another lesson from the pascal programming tutorial. See you in the next lesson!
Lesson 12 - Strings
o Val
Introduction to Strings
Maybe you already know what is a string variable from previous program examples that you have tried, but
you don't know what operations and what functions one can apply to them and how can they be
manipulated in order to obtain another form of string whatsoever.
In this lesson we will cover some important functions that the Pascal programming language has for us in
order to cater for string operations far more easier to use than you think.
Let's jump into the strings staff straight away. In order to understand strings, one has to keep in mind that a
string is made up of an array of characters. The string data type is an in-built data type that is an array of
256 characters (Type String = Packed Array[0..255] of Char). When stored in memory, the processor
should know where the string starts and where it finishes. In order to know where the string finishes, in
Pascal, the 0th element of a string is defined as the length of the string. So, if you try to access character 0
of a string, the number of characters stored in that array is returned, thus letting the processor to know
where the string finishes.
Var
myString : String;
Begin
myString := 'Hey! How are you?';
Writeln('The length of the string is ',byte(myString[0]));
Write(myString[byte(myString[0])]);
Write(' is the last character.');
End.
Note that in the previous program I have used an automatic data-type conversion, normally referred to as
data type-casting. When type-casting from one data type to another, all that is happening is simply a
conversion from one data type to another based on the data type in subject and the wrapping data type to
which the old data type is being converted. In our case, the array variable myString has a special 0th
element storing the number of characters in that array in string format. This value is an ascii value, so trying
Note that myString[myString[0]] without having data-type casting around myString[0] will lead to a compiler
error because myString[0] is a character and not an integer.
Note that to retrieve the length of a string (ie. the number of characters in a string) there is no need to use
the method I showed to you because its too complicated and unfriendly. Instead, there is the simpler
function, length() which will return the number of characters of the string in subject. Eg. numOfChars :=
length(myString);
There are some basic Pascal functions that has to do with string operations and so there is no need to write
them yourself, and they are of course quite useful. I will explain all the string functions by describing what
they do and a simple example of each.
Description
This function will search for the string SubString within the string S. If the sub-string is not found, then the
function would return 0. If on the other hand the sub-string is found, then the index integer value of the first
character of the main string that matches the character of the sub-string is returned.
Var
S : String;
Begin
S := 'Hey there! How are you?';
Write('The word "How" is found at char index ');
Writeln(Pos('How',S));
If Pos('Why',S) <= 0 then
Writeln('"Why" is not found.');
End.
Description
This function will copy some characters from string S starting from character index Index and copies as much
as Count. The copied string is then returned.
Var
S : String;
Description
Deletes a specified number of characters from the string S. The starting position of deletion is from character
index Index. The number of characters that will be deleted is specified through Count. The new string is
passed back through the variable parameter S.
Var
S : String;
Begin
S := 'Hey Max! How are you?';
Delete(S, 4, 4); { 'Hey! How are you?' }
Write(S);
End.
Description
This function will insert a string of any length into a source string starting from an index character. The string
S will be inserted into string Source starting from the index character Index. No characters will be deleted
from the front except if the resulting string is longer than 255 characters. In this case, the front characters will
be truncated as to fit a 255-character string.
Var
S : String;
Begin
S := 'Hey! How are you?';
Insert(S, ' Max', 4);
Write(S);
{ 'Hey Max! How are you?' }
End.
Var
S1, S2 : String;
Begin
S1 := 'Hey!'
S2 := ' How are you?';
Write(Concat(S1, S2)); { 'Hey! How are you?' }
End.
is the same as
Var
S1, S2 : String;
Begin
S1 := 'Hey!'
S2 := ' How are you?';
Write(S1 + S2); { 'Hey! How are you?' }
End.
Description
Converts the character C to uppercase and returned. If the character is already in uppercase form or the
character is not within the range of the lower case alphabet, then it is left as is.
Var
S : String;
i : Integer;
Begin
S := 'Hey! How are you?';
For i := 1 to length(S) do
S[i] := UpCase(S[i]);
Write(S); { 'HEY! HOW ARE YOU?' }
End.
Description
Converts an integer or a decimal value to a string. The value parameter Val is converted into a string and
passed through the variable paramter S.
Begin
i := -0.563;
Str(i, S);
Write(S);
End.
Description
Converts a string to its corresponding numeric value. The string paramater S is converted into a numeric value
and passed back through the variable paramter Val. If the string to be converted is not a correct numeric value,
an error occurs and is returned via Code. If the conversion is correct then Code is 0.
Var
S : String;
error : Integer;
R : Real;
Begin
S := '-0.563';
Val(S, R, error);
If error > 0 then
Write('Error in conversion.')
Else
Write(R);
End.