0% found this document useful (0 votes)
2 views

Pascal Progra

The document is a comprehensive guide to Pascal programming, covering essential topics such as program structure, variables, constants, and reserved words. It includes examples of simple programs, explanations of syntax rules, and the importance of indentation and comments. The lessons progress from basic programming concepts to more advanced topics like file handling, arrays, and graphics.

Uploaded by

Roland Ndzo
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Pascal Progra

The document is a comprehensive guide to Pascal programming, covering essential topics such as program structure, variables, constants, and reserved words. It includes examples of simple programs, explanations of syntax rules, and the importance of indentation and comments. The lessons progress from basic programming concepts to more advanced topics like file handling, arrays, and graphics.

Uploaded by

Roland Ndzo
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 57

Pascal Programming

Lesson 1 The First Few Steps in Pascal Programming

Lesson 2 Variables, Constants and the Assignment Statement

Lesson 3 Useful Reserved Words of the Crt Unit: ClrScr(), GotoXy(), etc...

Lesson 4 Program Control

Lesson 5 The CASE-OF Statement

Lesson 6 Logical Operators and Boolean Expressions

Lesson 7 Procedures and Functions

Lesson 8 BGI Graphics

Lesson 9 File Handling

Lesson 10 Arrays

Lesson 11 Record Data Structure

Lesson 12 Strings

Lesson 1 - The First Few steps in Pascal Programming

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.

[email protected] 6 77 20 00 53 Pascal Programming Page 1


This program also runs perfectly as the previous one. The only difference is: neatness and friendliness.

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;

Begin {no semicolon}


Write('Input number 1:');
Readln(Num1);
Writeln('Input number 2:');
Readln(Num2);
Sum := Num1 + Num2; {addition}
Writeln(Sum);
Readln;
End.

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

[email protected] 6 77 20 00 53 Pascal Programming Page 2


same function, except that the 'write' function, does not proceed to the following line when writing a statement. If
you run this program, you will notice the difference between them. When using these two terms, any message
that will be typed in between the brackets and the inverted commas '(' ')', is displayed on the screen. However,
if a variable is used instead of a message, without using the inverted commas, the CPU will display the stored
variable in the memory, on the screen. In line 9, the CPU will not display 'Sum' on the screen, but the stored
number in the memory. Another important thing which must be noticed is the semi-colon (;). The semicolon is
used after each statement in the program, except those that you will learn later. However, in the example above,
there isn't a semicolon after a 'begin' statement. This is because the flow of the program has just started and must
not be stopped by a ';'.

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.

Sum := Num1 + Num2;

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:

Num1 + Num2 := Sum;

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.

Lesson 2 - Variables, Constants and the Assignment Statement

This lesson will cover:

[email protected] 6 77 20 00 53 Pascal Programming Page 3


 String Variables

 Constants and the Assignment Statement

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)

*Writeln('Your name is: ',name);

or:

Writeln('Your name is:',name,'. Your surname is ',surname,'.');

*Note that you can even make it this way:

Writeln('Your name is: ',name,'');

BUT you should put the inverted commas properly (underlined) to close the message.

[email protected] 6 77 20 00 53 Pascal Programming Page 4


Constants and the Assignment Statement
Apart from variables, there are also items in the program which are referred to as 'constants'. Unlike variables,
constants keep their value or string unchanged for the whole program. Here I have made a program, not so much
different from the previous one:

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;

age2 := age + 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.

Try examine this yourself:

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 5


{real is a decimal (described later)}

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...

This lesson will cover :


 Use of the reserved words
 Description of the reserved words

Examples :

[email protected] 6 77 20 00 53 Pascal Programming Page 6


1. Clrscr;
2. GotoXy(_,_);
3. Textbackground();
4. Textcolor();
5. Readkey;
6. Delay();

7. Halt; / Halt()

Use of the Reserved Words


Before I teach you the if statements and for loops, I would like to give you an idea of some pascal functions
which are quite useful. This would require your program to include the library 'crt.tpu'. To include a library in
the program, one should use the reserved word 'uses', because it is used to call a library of functions and
procedures. Here is the program of lesson 2 (program 3) which is better handled and more user-friendly:

Program lesson3_Program1;

Uses Crt; {We will make use of the crt library}

Var PD, Dname, Cmodel : String;


CostPD, TCostPD, Distance : Real;
{real is a decimal (described later)}

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);

[email protected] 6 77 20 00 53 Pascal Programming Page 7


GotoXy(29,6);
Write('Cost of Diesel/Petrol: (£) ');
Readln(CostPD);
GotoXy(8,7);
Writeln('Average distance you travel with '+
+'your car every week: (kilometres) ');
Readln(Distance);
ClrScr;
GotoXy(28,3);
Writeln('Name of Driver:',Dname);
GotoXy(31,4); Delay(500);
Writeln('Car Model:',Cmodel);
GotoXy(32,5); Delay(500);
Writeln('Diesel/Petrol:',PD);
GotoXy(8,6); Delay(500);
Writeln('Average distance covered '+
+'every week: ',Distance:1:2,'Km');
GotoXy(25,7); Delay(500);
Writeln('Cost of ',PD,' per litre: £',CostPD:1:2,'/litre');
Writeln; Delay(500);
Writeln;
TCostPD := Distance * CostPD;
GotoXy(21,10);
Writeln('Total cost of ',PD,' per week:£',TCostPD:1:2);
TCostPD := 0;
GotoXy(21,12);
Writeln('Total cost of ',PD,' per week:'+
+'£',(Distance * CostPD):1:2);
GotoXy(18,14);
Writeln('Total cost of ',PD,' per week:£',Distance * CostPD);
readln;
End.

(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:

Reserved Word Crt: Yes/No Description

Clrscr Yes Clears screen

Gotoxy(int,int) Yes Takes the cursor to the pre-defined position

Textbackground(word/int) Yes Background colour

[email protected] 6 77 20 00 53 Pascal Programming Page 8


Textcolor(word/int) Yes Colour of text

Readkey Yes Reads a key; Could be assigned to a variable

Delay(int) Yes Waits for the included time(milliseconds)

Halt(parameter) No Program terminates

key (variable type) :

int - integer (-32768 to 32767), word - 0 to 65535.

Example of each

 Clrscr: (clear screen)

writeln('When you press enter, the screen would be cleared!');


readln;
clrscr;

 Gotoxy(int,int): (Go to position x and y);

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;

 Textbackground(word/int): (Background colour);

Textbackground(red); {word - red}


Writeln('Note the difference');
Textbackground(5); {integer - 5}
ClrScr;
Writeln('Note the difference'); Readln;

 Textcolor(word/int): (Text colour);

Textcolor(red); {word - red}


Writeln('Text colour');
Textcolor(5); {integer - 5}
Writeln('Text colour'); Readln;

 Readkey: (Reads a key-press);

Example 1:

Writeln('Press ANY key!!!');

[email protected] 6 77 20 00 53 Pascal Programming Page 9


Readkey;

Example 2:

Writeln('Press ANY key');


Keypress := readkey; {keypress is a DECLARED string variable(can be an integer variable)}
Writeln(Keypress);

 Delay(int): (Waits for some time);

Writeln('1');
Delay(1000);{1000 milliseconds}
Writeln('2');
Delay(1000);
Writeln('3');
Readln;

 Halt(int): (Program terminates with an exit code);

writeln('Press enter and the program terminates!);


Readln;
Halt(0);

Note that instructions following 'halt' are not executed since the program terminates when halt is encountered.

Lesson 4 - Program Control

This lesson will cover:

 The If Statement

1. If..Then..Else
2. Nested If Statements

 The Repeat-Until Loop


 The For Loop

1. Nested For loops

 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

[email protected] 6 77 20 00 53 Pascal Programming Page 10


in doubt about these programming constructs, then try to use the forums, describing your problem.

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:

If this happens(action), then do this(reaction, if action is true).

OR:

If this happens(action), then do this(reaction, if action is true), else do this(reaction, if action is false).

In Pascal, the 'if statement' should be written as follows:

If conditional expression then code ... ;{if one action}

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:

If x := 20 then x := x + 1; {the underlined character must be excluded}

Correct:

If x = 20 then x := x + 1; {only an equal sign is used for comparison}

A program is shown below as an example of how the 'if statement' works:

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');

[email protected] 6 77 20 00 53 Pascal Programming Page 11


GotoXy(6,8);
Write('Select: ');
Sel := Readkey;
If Sel = '1' {action} then
Begin {more than one statement}
ClrScr;
Write('Input No.1:');
Readln(N1);
Write('Input No.2:');
Readln(N2);
Total := N1 + N2;
Writeln('Addition: ',N1:2:3,' + ',N2:2:3,' = ',Total:2:3);
Write('Press any key to continue...');
Readkey;
Goto 1;{this leads back to the beginning of the program,
otherwise the program terminates}
End; {Closing the if statement(begin)}
If Sel = '2' then
{note that the assignment statement
is not used within an if statement}
Begin
ClrScr;
Write('Input No.1:');
Readln(N1);
Write('Input No.2:');
Readln(N2);
Total := N1 - N2;
Write('Subtraction: ');
Write(N1:2:3,' - ',N2:2:3,' = ',Total:2:3);
Write('Press any key to continue...');
Readkey;
Goto 1;
End; {Closing the if statement}
If Sel = '3' then
Begin
ClrScr;
Write('Are you sure?(Y/N)');
YN := Readkey;
If YN = 'y' then Halt; {1 action, so no need of Begin..End}
If YN = 'n' then Goto 1; {the goto statement is not
recommended for excessive use}
End;
End.

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:

writeln('Who has discovered the land of America?');


Readln(ans);
If (ans = 'Christopher Colombus') then
score := score + 1 {if this is false,}
ELSE

[email protected] 6 77 20 00 53 Pascal Programming Page 12


writeln('sorry, you''ve got it wrong!'); {then this is true}

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.

-> Nested If statements


The previous program have already shown an example of nested if statements. I.e.:

If Sel = '3' then


Begin
ClrScr;
Write('Are you sure?(Y/N)');
YN := Readkey;
If YN = 'y' then HALT; {Nested if statement}
If YN = 'n' then Goto 1;{Another Nested if statement}
End;

A nested if statement, is in the form:

If (this happens) then {if 1}


If (this happens) then {if 2}
(do this) etc...
Else (do this) {if 2}
Else (do this) etc... {if 1}

A nested if statement is an if statement within another if statement, as shown above.

The Repeat-Until Loop

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)

Until conditional statement;

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');

[email protected] 6 77 20 00 53 Pascal Programming Page 13


End.

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.

The For Loop


The for loop is a sort of repeat-until loop. The for loop, repeats a set of instructions for a number of times.
The for loop is in the form:

- If used for only one action:

for {variable}* := {original value} to/downto {final value} do

{code...(for one action)}

- If used for more than one action:

for {variable}* := {original value} to/downto {final value} do Begin

{code...}

{code...}

End;

*Generally, this variable is called the 'loop counter'.

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:

Without for loop:

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.

With for loop:

Program lesson4_Program2b;
Uses Crt;

[email protected] 6 77 20 00 53 Pascal Programming Page 14


Var Counter : Integer; {loop counter declared as integer}

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.

Without the for loop:

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.

With for loop:

Program Program3b_lesson4;
Uses Crt;

Var Counter : Integer; {loop counter}

[email protected] 6 77 20 00 53 Pascal Programming Page 15


Begin
For Counter := 1 to 5 do
Begin
gotoxy(25, 5 + Counter);
Writeln('I');
End;
For Counter := 5 Downto 1 do
Begin {an example of 'downto' instead of
'to', note the 'gotoxy(_,_)'}
gotoxy(32, 11 - Counter);
Writeln('I');
End;
For Counter := 1 to 6 do
Begin
gotoxy(25 + Counter, 11);
Writeln('-');
End;
For Counter := 6 Downto 1 do
Begin
gotoxy(32 - Counter, 5);
Writeln('-');
End;
{--------------The Corners(+)---------------}
Gotoxy(25,5);
Writeln('+');
GotoXy(25,11);
Writeln('+');
GotoXy(32,5);
Writeln('+');
GotoXy(32,11);
Writeln('+');
GotoXy(45,7);
Writeln('Just as simple as the for loop!!!');
Readln;
End.

Again, the two programs above perform the same function.

-> Nested for loops

A nested for loop is similar to that of the nested if statements. A nested for loop is in the form:

for {loop counter} := {original value} to {final value} do {Begin-if required}

{code if any..begin should be included (i.e more than one action)}

for {loop counter} := {original value} to {final value} do {Begin-if required}

{code..if more than one action, include begin in the second for loop}

{End; - if begin is included in the second for loop)}

{code if any..begin should be included in the first for loop}

{End; - if begin is included in the first for loop)}

The nested for loop is rarely used and it may cause problems.

[email protected] 6 77 20 00 53 Pascal Programming Page 16


While-Do Loop

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:

While <condition is true> do the following:

instruction 1;

instruction 2;

instruction 3;

etc...

End; {If while-do loop starts with a begin statement}

Example Program on the While-Do loop:

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.

Lesson 5 - The CASE-OF Statement

*You should have an idea of the 'if statement' before you proceed with this lesson.

The Simple Case Statement


So far, you have learned how to use an 'if statement'. But in some cases the 'case statement' is preferred to the if
statement because it reduces some unnecessary code but the same meaning is retained. The case statement is very
similar to the if statement, except in that the it does not accept literal conditional expressions (i.e.: strings) but
surprisingly enough, it allows single character conditional expressions. Here is how it works:

Case {variable of type: integer or character ONLY} of

{input statement- within inverted commas if of type char} : {code..}

{input statement- within inverted commas if of type char} : {code..}

...

[email protected] 6 77 20 00 53 Pascal Programming Page 17


End; {End Case}

Now you should note the difference and the intelligent use of the case statement over the if statement.

The Program is written using the if statement:

Program Program1a_Lesson5;
Uses Crt;
Label Return; {used respectively with the
goto statement; beware of 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);
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.

[email protected] 6 77 20 00 53 Pascal Programming Page 18


Now, the next program is written using the case statement and the output
is almost the same.

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)

The Case-Else Statement

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':

[email protected] 6 77 20 00 53 Pascal Programming Page 19


Program Program2_Lesson5;
Uses Crt;
Label Return; { avoid it }
Var YN : Char;

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}

Lesson 6 - Logical Operators and Boolean Expressions

This lesson will cover:


 What are logical operators?
 The Logical Operators

1. AND
2. OR
3. NOT

 Example of each logical operator:

1. AND
2. OR
3. NOT

 The Boolean Expressions

What are the Logical Operators


The logical operators are expressions which return a false or true result over a conditional expression. They are
also used in assignments (an example of this would be shown later). Such operators consist of simple logical

[email protected] 6 77 20 00 53 Pascal Programming Page 20


operators, such as 'Not' or 'And'. They should be used between two conditional expressions ; for example:

If (x = 0) AND (a = 2) then...

The Logical Operators


There are three types of logical operators, each of which are concerned with conditional expressions. These are:

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

[email protected] 6 77 20 00 53 Pascal Programming Page 21


possible combinations.

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

Example of the AND Operator

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.

Example of the OR Operator

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.

[email protected] 6 77 20 00 53 Pascal Programming Page 22


Example of the NOT Operator

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.

The Boolean Expressions


The boolean expressions are the terms 'true' and 'false'. These are simply similar to 1's (for true) and 0's(for
false). They describe an expression whether it is false or true. The variable types over boolean expressions is the
'boolean' type. Example:

Var bool : Boolean;

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.

Lesson 7 - Procedures and Functions

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

[email protected] 6 77 20 00 53 Pascal Programming Page 23


Procedures are just like small programs. Sometimes they are called sub--programs. They help the programmer to
avoid repetitions. A procedure start off with a begin and ends up with an end;. It can also have its own variables,
which cannot be used with the main-program. To have an exact definition of a procedure, you should compare a
program which includes a repeated section with another program avoiding the repeated sections by using a
procedure, which is called several times:
Program Lesson7_Program1a;
Uses Crt;
Var Counter : Integer;
Begin
textcolor(green);
GotoXy(10,5);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
GotoXy(10,6);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
GotoXy(10,7);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
GotoXy(10,10);
For Counter := 1 to 10 do
Begin {Step [1]}
write(chr(196)); {Step [2]}
End; {Step [3]}
Readkey;
End.

Now have a look at the next program which uses a procedure:

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 24


GotoXy(10,10);
DrawLine;
Readkey;
End.

There are some differences between these two programs which are very important to note. These are :

 Size of the program

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.

Using Procedures with Parameters

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

[email protected] 6 77 20 00 53 Pascal Programming Page 25


DrawLine(10,5);
DrawLine(10,6);
DrawLine(10,7);
DrawLine(10,10);
Readkey;
End.

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 26


Writeln;
Writeln('This is a confirmation field. Please verify that');
Writeln('your information is correct!');
Writeln;
Write('Is your personal information all correct? [Y/N] ');
Repeat
YN := Readkey;
Case YN Of
'N' : InfoCor := False;
'Y' : InfoCor := True;
End;
Until (YN = 'N') or (YN = 'Y');
End;
Begin { main program }
InfoCor := True;
ClrScr;
TextBackground(cyan);
TextColor(green);
Write('A list of colours is being displayed...');
For i := 1 to 16 do
Begin
Case i Of
16 : Begin
TextBackGround(white);
End;
End;
textcolor(i);
Writeln(i,': This is Colour No.',i);
End;
TextBackGround(black);
TextColor(white);
Write('Please, put into your mind your favourite colour. ');
Write('When you are ready press any key...');
Readkey;
ClrScr;
Write('Enter your favourite text colour: (only numbers) ');
Readln(TxtC);
Write('Enter your favourite background colour : __');
Readln(TxtB);
Writeln;
Writeln;
Write('Now, you must enter your personal information. ');
Write('Hit any key to continue...');
ClrScr;
EnterUserInfo(TxtC,TxtB);
ConfirmationField(TxtC,TxtB);
If InfoCor = False then
Repeat
Writeln;
Writeln('You verified that your information is, '+
+'for some reason, incorrect.');
Writeln('You are now going to re-enter your '+
+'correct information. Hit any key..');
Readkey;
EnterUserInfo(TxtC,TxtB);
ClrScr;
ConfirmationField(TxtC,TxtB);
Until InfoCor = True;
End.

[email protected] 6 77 20 00 53 Pascal Programming Page 27


The Variable Parameter

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:

Procedure <PROCEDURE_NAME(Var Variable_Name : Type);>

Here is an example of how to use a varaible parameter and what's its purpose:

Program VAR_PARAM_EXAMPLE;

Procedure Square(Index : Integer; Var Result : Integer);


Begin
Result := Index * Index;
End;

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.

Example of a program using a function is seen below:

Program Lesson7_Program4;
Uses Crt;

Var SizeA, sizeB : Real;


YN : Char;
unitS : String[2];
Function PythagorasFunc(A:Real; B:Real) : Real;
{The pythagoras theorem}
Begin
PythagorasFunc := SQRT(A*A + B*B);
{Output: Assign the procedure name to the value.
If you forget to assign the function to the value,
you will get a trash value from the memory}
End;
Begin
Repeat
Writeln;
Write('Enter the size of side A : ');

[email protected] 6 77 20 00 53 Pascal Programming Page 28


Readln(sizeA);
Write('Enter the size of side B : ');
Readln(sizeB);
Repeat
Write('metres or centimetres? Enter : [m or cm] ');
Readln(unitS);
Until (unitS = 'm') or (unitS = 'cm');
Writeln(PythagorasFunc(sizeA,sizeB),' ',unitS);
Writeln;
Write('Repeat? ');
YN := Readkey;
Until (YN in ['N','n']);
End.

Program code is updated (24th August 2005)

Details: the function returns a real instead an integer; if..then..halt removed; repeat..until condition is also
changed

Program Errors corrected thanks to Harold Smith

Lesson 8 - BGI Graphics

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.

This lesson will cover:


 Introduction to Graphics
 Initialising Graphics
 Graphics Statements

 Advanced Graphics Statements

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

[email protected] 6 77 20 00 53 Pascal Programming Page 29


output material. Before you can call turbo pascal's graphics procedures and functions, you have to include the
Graph unit in the Uses section (Eg. Uses Crt, Graph, Overlay, Printers etc...).
Initialising Graphics
Unlike the crt, graphics should be initialised, and it should be initialised at the beginning of the program or just
before you start using graphics procedures and functions. This will help the compiler to link to the graphics file.
Usually, the default graphics routines used by the Turbo Pascal's compiler is the EGAVGA.BGI. When you
initialise the graphics, automatically your computer will use this file to output graphics material.

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 30


End;
CloseGraph;
End.

The program above uses the statement:

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:

Statement Description Example


This will set the background colour of the
SetBkColor(Brown); or
SetBkColor(colour/colour code); screen to the colour indicated within the
SetBkColor(6);
parenthesis.
This will set the text colour to the colour SetColor(Green); or
SetColor(colour/colour code);
indicated within the parenthesis. SetColor(2);
This statement will output the text
OutText(''); OutText('BGI Graphics');
indicated within the inverted commas.
This is very similar to the one before this
OutTextXY(X,Y,''); statement, except in that it move the OutTextXY(300,100,'BGI Graphics');
cursor position indicated as shown
Takes the cursor position to that
MoveTo(X,Y); MoveTo(GetMaxX,GetMaxY);
indicated in the brackets.
Places a small pixel at X pixels left and Y PutPixel(50,128,Cyan); or
PutPixel(X,Y,colour/colour code);
pixels down. PutPixel(50,128,5);
Draws a line from the position of the
Line(X,Y); Line(30,111);
cursor to X, Y.

[email protected] 6 77 20 00 53 Pascal Programming Page 31


LineTo(X1,Y1,X2,Y2); Draws a line from X1, Y1 to X2, Y2. LineTo(0,0, GetMaxX,GetMaxY);
Draws a rectangle from point X1, Y1 to
Rectangle(X1,Y1,X2,Y2); Rectangle(30,50,25,45);
point X2, Y2.
Draws a circle from point X, Y with a Circle(GetMaxX Div 2, GetMaxY Div
Circle(X,Y,Radius);
radius length of Radius. 2, 100);
Draws an ellipse with centre point X,Y
starting from P degrees to Q degrees,
Ellipse(X,Y,P,Q,HorR,VerR); Ellipse(300,160, 0,360,60,40);
with a horizontal radius of HorR and a
vertical radius of VerR.
Draws an arc starting from X,Y starting
with an angle of Pdegrees and ending
Arc(X,Y,P,Q,Radius); Arc(100,200,0,90,70);
with an angle of Q degrees, with radius
Radius.
Similar to the arc();, except in that it is a
PieSlice(X,Y,P,Q,Radius); PieSlice(90,150,20,95,100);
pie.
Draws a bar filled with the current
Bar(X1,Y1,X2,Y2); Bar(50,100,150,500);
colour, starting from X1, X2 to Y1, Y2.

Similar to the one above, except in that


Bar3D(X1,Y1,X2,Y2, Bar3D(120,100,250,
it is 3D. The last parameter indicates the
Depth,ThreeD_Top); 150,50,TopOn);
3D top of the 3D Bar.

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;

Var Gd, Gm,


Radius, Grow, IncP, IncQ : Integer;
DecrP, DecrQ : Boolean;
Begin
Gd := Detect;
InitGraph(Gd, Gm, '');
{Do not forget to change the dir path}
{Try C:\TP\BGI}
if GraphResult <> grOk then
Halt(1);
Randomize;
SetColor(Random(15)+1);
{In the following loop, 600 circles:
circles with different radii are
drawn. Everytime the loop is repeated,
the radius increases by
one, and thus the circle becomes +1
larger than the previous one.}
For Radius := 1 to 600 do
Begin
Circle(GetMaxX Div 2, GetMaxY Div 2, Radius);
Delay(1);
End;
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'Prepare for another one...');

[email protected] 6 77 20 00 53 Pascal Programming Page 32


Delay(1000);
ClearViewPort;
Grow := 0;
{The ellipse loop, is similar to the one
above except in that the vertical
radius increases by 1}
For Radius := 1 to 600 do
Begin
Inc(Grow);
Ellipse(GetMaxX Div 2,
GetMaxY Div 2, 0, 360,
GetMaxX Div 2, Radius + Grow);
Delay(1);
End;
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'Now what?'); Delay(1000);
ClearViewPort;
{Here's another graphics invention of mine! Similar
to the first one, but using two circles positioned
on both edges of the screen,
continously increasing in size}
For Radius := 1 to 600 do
Begin
Circle(GetMaxX Div 2, 0, Radius);
Circle(GetMaxX Div 2, GetMaxY, Radius);
Delay(2);
End;
Delay(1000);
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'What are you lookin'' at?');
Delay(1000);
ClearViewPort;
Grow := 0;
{Below is a simple rectangle loop,
which inreases its size by 1
(increment x1,y1 and decrement x2,y2)}
For Radius := 1 to 600 do
Begin
Inc(Grow);
Rectangle(((GetMaxX Div 2) - Radius) - Grow,
((GetMaxY Div 2) - Radius)- Grow,
((GetMaxX Div 2) + Radius) + Grow,
((GetMaxY Div 2) + Radius) + Grow);
Delay(2);
End;
ClearDevice;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,
'Rotor of a helicopter thorn apart..');
Delay(2000);
ClearDevice;
{The demonstration below shows 4 rotating sectors,
moving horizontally,
and 4 other sectors in the opposite direction}
IncP := 0; IncQ := 0;
For Radius := 1 to 320 do
Begin
Inc(IncQ, 1);

[email protected] 6 77 20 00 53 Pascal Programming Page 33


Inc(IncP, (IncQ Div 2));
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 0 + IncP, 2 + IncP, 240);
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 180 + IncP, 182 + IncP, 240);
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 88 + IncP, 90 + IncP, 240);
PieSlice(GetMaxX Div 2 + IncP,
GetMaxY Div 2, 268 + IncP, 270 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 45 + IncP, 47 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 135 + IncP, 137 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 225 + IncP, 227 + IncP, 240);
PieSlice(GetMaxX Div 2 - IncP,
GetMaxY Div 2, 315 + IncP, 317 + IncP, 240);
Delay(10);
ClearDevice;
If IncP >= 220 then
Dec(IncQ, 4);
End;
ClearViewPort;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(230,GetMaxY Div 2,'Ah, now it is repaired..');
Delay(2000);
ClearDevice;
{The next demonstration shows a fantastic
rotor-like movement animation}
{Do not be amazed by the coding below - it's very simple}
{To make such animation, all you
have to do is to make a sector rotating
by simply increasing its degrees and clear the old one}
IncP := 0; IncQ := 0;
For Radius := 1 to 320 do
Begin
Inc(IncQ, 1);
Inc(IncP, (IncQ Div 4));
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 0 + IncP, 2 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 180 + IncP, 182 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 90 + IncP, 92 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 270 + IncP, 272 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 45 + IncP, 47 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 135 + IncP, 137 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 225 + IncP, 227 + IncP, 240);
PieSlice(GetMaxX Div 2,
GetMaxY Div 2, 315 + IncP, 317 + IncP, 240);
Delay(10);
ClearDevice;
End;
SetTextJustify(230, GetMaxY Div 2);
OutTextXY(220,GetMaxY Div 2,'The chop is on its way!');
Delay(1000);

[email protected] 6 77 20 00 53 Pascal Programming Page 34


ClearDevice;
Randomize;
IncP := Random(GetMaxX) + 1;
IncQ := 0;
DecrP := False;
DecrQ := False;
For Radius := 1 to MaxInt do
Begin
If not(DecrP) then Inc(IncP,3) Else
If DecrP then Dec(IncP,3);
If not(DecrQ) then Inc(IncQ,3) Else
If DecrQ then Dec(IncQ,3);
SetColor(LightCyan);
{Now FillCircle! - same radii (20)}
FillEllipse(IncP,IncQ, 20, 20);
Circle(IncP, IncQ, 40);
PieSlice(IncP, IncQ, 0 + (IncP * 80),
6 + (IncP * 80), 100);
PieSlice(IncP, IncQ, 180 + (IncP * 80),
186 + (IncP * 80), 100);
PieSlice(IncP, IncQ, 90 + (IncP * 80),
96 + (IncP * 80), 100);
PieSlice(IncP, IncQ, 270 + (IncP * 80),
276 + (IncP * 80), 100);
If IncP > GetmaxX then DecrP := True Else
If IncP < 0 then DecrP := False;
If IncQ > GetmaxY then DecrQ := True Else
If IncQ < 0 then DecrQ := False;
Delay(10);
ClearDevice; {Try to remove this :-)}
If KeyPressed then Break;
End;
ReadKey;
SetTextStyle(SansSerifFont,HorizDir,1);
SetUserCharSize(2,1,5,1);
OutTextXy(12,20,'Was it fun? Ehh???');
SetUserCharSize(1,1,2,1);
SetColor(LightCyan);
OutTextXY(35,190,'Now you can try to do it yourself.');
OutTextXY(20,240,'If you have any comments then send');
OutTextXY(180,300,'me an e-mail to');
OutTextXY(120,400,'[email protected]');
Readln;
CloseGraph;
End.

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.

Advanced Graphics Statments

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:

Circle(GetMaxX Div 2, GetMaxY Div 2, 60);


FillEllipse(GetMaxX Div 2, GetMaxY Div 2, 60, 60); {Note the radii}

The command 'FillEllipse' will fill the circle with the current colour. Take a look at the table below:

[email protected] 6 77 20 00 53 Pascal Programming Page 35


Statement Description Example

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);

SetUserCharSize(MultX, Display the text in a larger or smaller


SetUserCharSize(5,1,3,1);
DivX, MultY, DivY); size.

Display text using the font style,


SetTextStyle(Font,
orientation and size indicated as SetTextStyle(TriplexFont, HorizDir,2);
Direction, Size);
shown.

ClearViewPort; Clears the active screen page. ClearViewPort;

Get the driver name being currently


GetDriverName OutText('Using driver:' + GetDriverName);
used.

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 36


Bar3d(150,80,450,330,50,TopOff);
SetColor(LightGreen);
OutTextXY(130,410,'BAR3D(150,80,450,330,50,TopOff);');
Readln;
ClearViewPort;
OutTextXY(130,380,'SetFillStyle(BkSlashFill,7);');
SetFillStyle(BkSlashFill,7);
Bar3d(150,80,450,330,50,TopOn);
OutTextXY(130,410,'BAR3D(150,80,450,330,50,TopOn);');
Readln;
ClearViewPort;
OutTextXY(130,380,'SetFillStyle(XHatchFill,6);');
SetFillStyle(XHatchFill,6);
Bar3d(150,80,450,330,50,TopOn);
OutTextXY(130,410,'BAR3D(150,80,450,330,50,TopOn);');
Readln;
CloseGraph;
End.

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.

Lesson 9 - File Handling

This topic deals on how to:


 Read from a File
 Create and Write to a File

 Append data to an existing File


 Delete Files
 Use the '{$I-},{$I+}' compiler directive
 Create and Remove Sub-Directories

 FileSize() - return the size of the file in bytes

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..

[email protected] 6 77 20 00 53 Pascal Programming Page 37


Read from a File (file input)

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.

Program code is updated (13th June 2006)

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 Errors corrected thanks to James McNamara


I think that it is worth to take a look at various important lines of the program. A new variable of type: 'Text'
is new to you, and this should be used whenever you are going to edit a text file! The variable 'FileName' is
required to link to the file indicated by the user. The 'assign' statement is used to declare 'FileName' + '.txt'
to a text file, so that the file could be opened, using - 'Reset()'. To read from the first line to the very last line
of the file, you should use the repeat-until loop, ending the loop with : 'Until Eof(textfile);', which means:
'Until the End Of File [eof]'.

Create and Write to a File (file output)

The following program is an example of how to create-and-write or overwrite a file:

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);

[email protected] 6 77 20 00 53 Pascal Programming Page 38


End.

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.

Append text to an existing File

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:

Var UFile : Text;


Begin
Assign(UFile,'C:\ADDTEXT.TXT');
ReWrite(UFile);
Writeln(UFile,'How many sentences, '+
+'are present in this file?');
Close(UFile);
End.

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:

Var UFile : Text;


Begin
Assign(UFile,'C:\ADDTEXT.TXT');
Append(UFile);
Writeln(UFile,'How many sentences, '+
+'are present in this file?');
Close(UFile);
End.

Run the program two times or more to see the change...

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

[email protected] 6 77 20 00 53 Pascal Programming Page 39


apply a 'close(...)' after erase().

Example Program:

Var UFile : Text; { or it could be of 'file' type}


Begin
Assign(UFile,'C:\ADDTEXT.TXT');
Erase (UFile);
End.

The '{$I-},{$I+}' compiler directives

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:

If (IOResult <> 0) then ...

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 }

[email protected] 6 77 20 00 53 Pascal Programming Page 40


If (IOResult <> 0) then
Begin
Writeln('The file required to be opened is not found!');
Readln;
End Else
Begin
readln(t,s);
Writeln('The first line of the file reads: ',s);
Close(t);
End;
End.

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.

Create and Remove Sub-Directories

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 }

Var NewDir : PathStr;


{ for searching the dir and create a new one,
if it does not exist }
F : Text;

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');

[email protected] 6 77 20 00 53 Pascal Programming Page 41


{$I+} ReWrite(F); {$I-} { disable and enable back again
i/o error checking }
{ write to text file }
Writeln(F,'http://pascal-programming.info/');
{$I+} Close(F); {$I-}
End.

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 .

FileSize() - Return the value of a file in bytes

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

This lesson will cover:


 What are Arrays?

[email protected] 6 77 20 00 53 Pascal Programming Page 42


 Introducing User-Defined Data Types

 2 Dimensional and multi-Dimensional Arrays

What are 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

myArray : Array[1..20] of Integer;

<arrayName> : Array[n..m] of <Data Type>;

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.

To assign values to a particular integer of an array, we do it like this:

myArray[5] := 10;
myArray[1] := 25;

<arrayName>[index] := <relevant data>

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.

Reading a value from an array is done as follows:

Var
myVar : Integer;
myArray : Array[1..5] of Integer;

Begin
myArray[2] := 25;
myVar := myArray[2];

[email protected] 6 77 20 00 53 Pascal Programming Page 43


End.

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.

Introducing User-Defined Data Types

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);

[email protected] 6 77 20 00 53 Pascal Programming Page 44


Writeln('Your age :', age);
Readln;
End.

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]);

Writeln('Now writing them to file...');


{store the numbers in a file}
Assign(myFile, 'example.dat');
ReWrite(byteFile);
Write(myFile, myArrayVar[i]);
Close(myFile);
Writeln('Done, you may exit..');
Readln;
End.

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 and Multi-Dimensional Arrays

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

[email protected] 6 77 20 00 53 Pascal Programming Page 45


2

3 3,4

5 5,3

An example of a 5 by 5 2D array illustrated on a grid

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 46


Assign(myFile, 'test.dat');
ReWrite(myFile);
Write(myFile, arrayInt[1]);
Close(myFile);
ReSet(myFile);
Read(myFile, arrayInt[2]);
Close(myFile);

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.

Lesson 11 - Record Data Structure

This lesson will cover:


 What are Records?
 The With Keyword
 Passing Records as Arguments
 Arrays of Records

 Binary Files and Records

What are Records?

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,

[email protected] 6 77 20 00 53 Pascal Programming Page 47


author...) are called fields and these will be accessed through the instance of myBookRec variable. As you
might have noticed, the declaration of typical record data structure starts with the keyword Record and
always ends with the end keyword.

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;

Writeln('Here are the book details:');


Writeln;
Writeln('Title: ', myBookRec.Title);
Writeln('Author: ', myBookRec.Author);
Writeln('ISBN: ', myBookRec.ISBN);
Writeln('Price: ', myBookRec.Price);
Readln;
End.

The With Keyword

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;

Passing Records as Arguments

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.

[email protected] 6 77 20 00 53 Pascal Programming Page 48


Type
Str25 = String[25];
TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);


Begin
Writeln('Please enter the book details: ');
Write('Book Name: ');
Readln(newBook.Title);
Write('Author: ');
Readln(newBook.Author);
Write('ISBN: ');
Readln(newBook.ISBN);
Write('Price: ');
Readln(newBook.Price);
End;

Procedure DisplayBookDetails(myBookRec : TBookRec);


Begin
Writeln('Here are the book details:');
Writeln;
Writeln('Title: ', myBookRec.Title);
Writeln('Author: ', myBookRec.Author);
Writeln('ISBN: ', myBookRec.ISBN);
Writeln('Price: ', myBookRec.Price);
End;

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 49


Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);


Begin
Writeln('Please enter the book details: ');
Write('Book Name: ');
Readln(newBook.Title);
Write('Author: ');
Readln(newBook.Author);
Write('ISBN: ');
Readln(newBook.ISBN);
Write('Price: ');
Readln(newBook.Price);
End;

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.

Binary Files and 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];

[email protected] 6 77 20 00 53 Pascal Programming Page 50


TBookRec = Record
Title, Author,
ISBN : Str25;
Price : Real;
End;

Procedure EnterNewBook(var newBook : TBookRec);


Begin
Writeln('Please enter the book details: ');
Write('Book Name: ');
Readln(newBook.Title);
Write('Author: ');
Readln(newBook.Author);
Write('ISBN: ');
Readln(newBook.ISBN);
Write('Price: ');
Readln(newBook.Price);
End;

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.

[email protected] 6 77 20 00 53 Pascal Programming Page 51


Special Uses of the Seek Function

Seek the first record of the file

Seek(myFile, 0);

Seek the last record of the file

Seek(myFile, FileSize(myFile)-1);

Seek one position beyond the last record

Seek(myFile, FileSize(myFile));

Seek to the next position of the file from the current

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.

0 BookRec0 1 BookRec1 2 BookRec2 3 BookRec3 4 BookRec4

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

[email protected] 6 77 20 00 53 Pascal Programming Page 52


This lesson will cover:
 Introduction to Strings
 Some Useful String Functions
o Pos
o Copy
o Delete
o Insert
o Concat
o UpCase
o Str

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.

The following example shows some simple string operations.

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

[email protected] 6 77 20 00 53 Pascal Programming Page 53


to display it without converting it into a number, the alternative ascii character is displayed. So you have to
change the character value to an ordinary number by wrapping the variable by another data type, in our
case a byte data type (why use integer? - its all waste of memory and memory consumption).

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);

Some Useful String Functions

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.

Function Pos(SubString : String; S : String) : Byte;

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.

Function Copy(S : String; Index : Integer; Count : Integer ) : String;

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;

[email protected] 6 77 20 00 53 Pascal Programming Page 54


Begin
S := 'Hey there! How are you?';
S := Copy(S, 5, 6); { 'there!' }
Write(S);
End.

Procedure Delete(var S : String; Index : Integer; Count : Integer );

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.

Procedure Insert(Source : String; var S : String; Index : Integer);

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.

Function Concat(s1 [, s2, s3...sn] : String) : String;

[email protected] 6 77 20 00 53 Pascal Programming Page 55


Description
Concatenates 2 or more strings depending how long is the argument expression. Try to make sure not to
exceed the limit of 255 characters when concatening strings as it will result in truncation. This function can
also be obtained by using the plus (+) operator between strings that need to be concatenated.

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.

Function UpCase(C : Char) : Char;

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.

Procedure Str(Val : Integer / LongInt / Real; var S : String);

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.

[email protected] 6 77 20 00 53 Pascal Programming Page 56


Var
S : String;
i : Real;

Begin
i := -0.563;
Str(i, S);
Write(S);
End.

Procedure Val(S : String; var Val; Code : Integer);

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.

[email protected] 6 77 20 00 53 Pascal Programming Page 57

You might also like