Вы находитесь на странице: 1из 18

Reset

Files This is a mode where you can both read and write to the file. This is the
main mode that you should use in your programs. Sometimes a trick if
How to declare a file you want to create a file and have it open for reading and writing is to
The first thing you need to know about files is how to declare them in create the file with rewrite and then use reset on the file. Reset works
pascal. A file is declared similarly to any other variable like so... like so:--
reset (<file variable>);
var
< file variable > : file of < type > Close
...
The close command closes a file. (as if you hadn't guessed). You need
to do this at the end of your program because it saves the changes to the
The type in the declaration will by any of the predefined types or one file. To close a file you write :--
of your types that you have defined in the type section. The most close (<file variable>);
common type to use is a record which you have declared. This means
that the file will store records of that type. e.g. You could have a file Writing records to a file
containing entries of type person which would have attributes like Writing records to a file is VERY easy. For the examples coming up we
name, address, phone etc.. will be using the following record type.

Assign type
This is where you give the file variable that you have declared an
associated file name. It works like so:-- personType = record
assign (<file variable>,<file name>);. name : string[30];
address : string[60];
Now your file variable points to a real file on the drive. This is where phone : string[7];
all the information will be written to. Now you must open your file. end;
Two ways of opening a file are explained below.
Now lets say that you have taken input from the user and have all of the
Rewrite values of a person variable. Now you want to write it to the file right?
If the file name you have assigned to your file variable does not To do this you use the write statement. You should notice that this is
actually exist on disk then Rewrite will create it for you. What rewrite the same command that is used for writing to the screen. Well, writing
does, is clear the file so you can write to it. NOTE: You are not able to to a file is not much different. All you need to do is the following...
read from the file when using rewrite. Rewrite is used like this:-- write (<file variable>,person);
rewrite (<file variable>); You can write as many records to the file as you like now :)
Easy! Now you can write to the file.

Turbo Pascal 1
Reading records from a file file. The example program below contains a delete procedure if you
To read records from a file you need to use the read statement. This is want to see this in code.
how you do it:--
read (<file variable>,person);
This reads from the file into your record. When you next execute this program Example;
statement you will read the next record from the file into your record
variable. This is because once you have done a read statement the file uses Crt;
pointer is moved along to the next record in the file for your
type
convenience. :)
personType = record
Seek name : string[30];
The seek command is to go to a certain record in the file. The file starts address : string[60];
at 0 so to be at the first record you need to go phone : string[7];
end;
seek (<file variable>,0).
To be past the last record at a place where you can add records to the var
end of the file you will need to go... personFile : file of person;
seek (<file variable>,filesize(<file variable>));
In the above example filesize returns the number of records in the file. procedure Openfile;
But since the records are number from zero up, the seek command goes begin
to the next record after the last record entered, ie the end of the file.
assign (personFile,'person.dat');
reset (personFile); {This assumes the file already
Truncate exists}
The truncate command deletes all the records in the file that lie after the end;
current file position. So to delete the last record from the file you will
have to go... procedure writeToFile;
seek (<file variable>,filesize(<file variable>)-1); var person : personType;
truncate (<file variable>);
begin
Deleting a record from a file with person do
begin
The simplest way to do this is to go to the last record in the file, read write ('NAME: '); readln(name);
that record into a variable, then go to the record you want to delete and write ('ADDRESS: '); readln(address);
over-write this with the variable that contains the last record's write ('PHONE: '); readln (phone);
information. Once you have done this you go back to the last record end;
and use truncate to remove it from the end. Now you have deleted a
write (personFile,person);

Turbo Pascal 2
end; seek (personFile,fileSize(personFile)-1);
truncate (personFile);
procedure readFromFile;
var end;
person : personType;
begin begin
<Main program code here>
while not eof(personFile) do close(personFile);
begin
clrscr; read (personFile,person); end.
with person do
begin

BASIC INPUT AND


writeln ('NAME: ',name);
writeln ('ADDRESS: ',address);
writeln ('PHONE: ',phone);
end;

end;
end;
readKey; {waits for a key to be pressed}
OUTPUT
procedure deleteRecord; As you probably know, nearly all computer programs take input from
var the user.
i : integer;
person : personType; If you don't know how to take input then you won't get very far in the
begin programming world.

write ('What record number would you like to Pascal has two major functions for taking input from the user. These
delete? '); are:-
readln (i);
seek (personFile,i);
if eof(personFile) then exit; read
{The above line checks if 'i' is bigger than or
equal to the file size}
{After all, you can't delete a record that doesn't Syntax:
exist!}
read (variable);
seek (personFile,fileSize(personFile)-1);
read (personFile,person);
seek (personFile,i);
Explanation:
write (personFile,person); This reads all the characters typed, until the user presses enter, into the
variable.
Turbo Pascal 3
If the variable is of type integer, and the user types in string characters, is right aligned within the field width e.g. write ('Hello':10); will
then produce the following output...
an error will occur. If the variable is a string of defined length then read
will only 00000Hello
take the first X characters from the line and put them into the string,
where X is the size (0=space)
of the string. Read does not move the cursor to the next line after input.
Notice that 'Hello' is right-aligned within the field of ten characters , the
remaining spaces coming before 'Hello'
readln
When writing real numbers, you must specify the field width and
Syntax: number of decimal places displayed, otherwise pascal will write it to
readln (variable);
the screen in standard form (this is not good). A field width of zero will
just write the real as if you had not specified a field width.
Explanation:
This is exactly the same as read except for the fact that it moves the If you want to write a combination of things, separate these by a
cursor to the next line after the user presses enter. comma. e.g.
write ('Hello ' , name , ', you weigh ' , weight:0:2 , '
kg.');
The above will write something like this... Hello Joe Bob, you
weigh 76.54 kg.
The output commands in pascal are very similar in syntax to the input
commands. Note: The write command does not move the cursor to the next line
after execution.
write
writeln
Syntax:
write (variable); Syntax:
write (variable:f) writeln (variable);
write (real variable:f:d); writeln (variable:f)
f=field width d=number of decimal places writeln (real variable:f:d);
f=field width d=number of decimal places
Explanation:
The write command displays a string of characters on the screen. When Explanation:
a field width is included, the writing The writeln command is exactly the same as the write command except
Turbo Pascal 4
for the fact that it moves the cursor integerArray = array[1..30] of integer;
integerTable = array[1..25,1..25] of integer;
to the next line after execution.
stringArray = array[1..100] of string[15];
.....

NOTE: All of the above commands are also used when reading and This is an example of declaring an array in the type block. The array
writing to files. This will be covered later. called 'integerArray' is like a list of integers, if seen on paper it might
look like this...
EXAMPLE PROGRAM
1.______20
Program example;
2.______145
{This is an example program for input and output}
3.______39
uses Crt; 4.______2708
var 5.______25
6.______260
name : string[30]; -
begin -
clrscr; {This clears the screen}
write ('What is your name? '); {Writes the question
-
without moveing the cursor to the next line} 30._____300
readln (name); {take input from user}
writeln ('Hello ', name); {Output Hello joebob} The array called integerTable can be represented by a table. It is a two
while not keypressed do; {waits for a key to be dimensional array. You can have three,four or even five dimensions in
pressed}
end. an array if you want.

Accessing Arrays
Arrays To access an entry of an array during the program you must
go...arrayname[entrynumber]
Arrays are an important part of Pascal. You can think of them like a or to access a multi-dimensional array you go...arrayname[x,y,z] (3d
catalogue of information. Arrays are declared under the type section. array)
They are a collection of a number of variables, arranged in the form of
a table. OK HERE COMES AN EXAMPLE PROGRAM
type

Turbo Pascal 5
Program classTest; Arrays can also be declared in the variables section without making a
{An example program demostration arrays}
type. However it is better to use a type if you plan to use the same type
{Takes scores from a class test and tells people if they
passed or not} of array in multiple places. So this code in the last program...

uses Crt; type

type testScores = array[1..10] of integer;

testScores = array[1..10] of integer; var

var marks : testScores;

i : integer; Can also be written like this:


marks : testScores;
passOrFail : string[6]; var

begin marks : array[1..10] of integer;

clrscr; This would do exactly the same thing, but does not declare a type of
for i := 1 to 10 do
begin write ('Please enter test score number 'testScores'.
',i,': ');
readln(marks[i]);
end;
for i := 1 to 10 do
begin
if (marks[i] < 50) then
Mathematical
Operations
passOrFail := 'Failed';
else
passOrFail := 'Passed';
writeln ('Student no.',i,' ',passOrFail);
end;
Pascal can do many mathematical operations. They are all relatively
simple and easy to remember.
while not keypressed do; The first thing to remember is that pascal uses := not = to assign a value
to a variable.
end. e.g. int := 3;
Basic mathematical operators

Turbo Pascal 6
Addition ...................... x := y + z; SQRT(Real Variable)
Subtraction ................... x := y - z; Explanation:
Multiplication ................ x := y * z;
SQRT returns the square root of the real variable that is passed to it,
Division ...................... x := y / z;
Integer division .............. x := y div z; pretty simple really.
Modulo arithmetic ............. x := y mod z;
Example:
Integer Division: One integer is divided by another and the integer part x := SQRT(y);
of the result is returned. This finds the square root of y and puts the result in x.

Modulo Arithmetic (Remainder Arithmetic): x := y mod z;


The above finds the remainder of y/z and puts it into x. SIN
These mathematical operations are pretty self explanatory. Syntax:

SIN(Real variable)
Mathematical functions
SQR Explantation:
SIN returns the sin of the number that is passed to it. Unfortunately this
Syntax: is in radians(stupid radians).
2*pi radians is equal to 360 degrees, so to convert from degrees to
SQR(Real Variable) radians it is degrees/180 * pi,
and from radians to degrees it is radians/pi * 180. It is a bit of a hassle
Explanation: but nevermind.
SQR returns the square of the real variable that is passed to it, pretty
simple really. Example:
x := SIN(y);
Example: This finds the sin of y(radians) and puts the value in x.
x := SQR(y);
This finds the square of y and puts the result in x.
COS
SQRT
Syntax:
Syntax:
COS(Real variable)

Turbo Pascal 7
Explantation: To find INVERSE SIN or INVERSE COS do the following...
COS returns the cos of the number that is passed to it. This is also in INVERSE SIN = ARCTAN(y/sqrt(1-sqr(y)))
radians. If you want to know how to convert INVERSE COS = ARCTAN(sqrt(1-sqr(x))/x)
radians into degrees and vice-versa then read the explanation of SIN.
So x := arctan(y/sqrt(1-sqr(y))); finds the inverse sin of y and
Example: puts it in x.
x := COS(y); So x := arctan(sqrt(1-sqr(x))/x); finds the inverse cos of y and
This finds the cos of y(radians) and puts the value in x. puts it in x.

ARCTAN Loops
Syntax: Okay, this lesson you are going to learn about loops. There are two
main types of loop in Turbo Pascal. While loops and For loops.
ARCTAN(Real variable)

Explantation:
ARCTAN returns the inverse tanget of the number that is passed to it.
It returns the angle in radians (gasp).
While
Example:
x := ARCTAN(y); While loops take the form :
while <condition> do
This finds the inverse tangent, in radians, of y and puts the value in x. begin
statement block
end;
Finding TANGENT These are useful for things like waiting until the user presses a key or
waiting until a number entered by the user is valid.
To find tanget just divide sin(Y) by cos(Y).
e.g x := sin(y)/cos(y); finds the tangent of y and puts it in x For instance you might say :
var
(remember radians). valueIn : String; {This will be the value the user
enters}
I, code : Integer;
begin
Finding INVERSE SIN/COS valueIn := 'abc'; {Fill it with a non integer value}

Turbo Pascal 8
code := 1; statement block
while code <> 0 do {While code is zero do the end;
following} For loops are more useful for iterating through an array. For instance
begin writeln('Enter a number:');
you might want to add 1 to each integer in an array...
readln(valueIn); var
val(valueIn, I, code); i : Integer; {This will be the counter for the
{Move the integer value of valueIn to I or if it loop}
isn't an} myArray : array[1..30] of Integer; {See arrays
{integer then code will be something other than 0. for this}
NB: code is} begin
{the error code. I can only hold integers so there is for i := 1 to 30 do
an error} myArray[i] := myArray[i] + 1;
{raised when the string can't be converted to an end.
integer}
end; This does goes through each item in the array 'MyArray' and adds one
end. to the current value in it.

In this loop as long as condition is true (in this case code <> 0) it will
execute the code between the begin and the corresponding end. You Repeat
can miss out the begin and end but then you will only be able to have
one line of code, or even no lines of code at all if you want.
The repeat loop is very similar to the while loop except the condition is
eg: while not keypressed do;
at the end, and if you want to execute multiple lines, then you don't
need to put begin .. end keywords around the multiple lines. For
In the above example as long as there is not a key pressed the
instance, this loop using while...
instructions in the loop are executed. Since there are no instructions in i := 1;
the loop this stops the program until the user presses a key. while i <= 6 do
begin
writeln(i);
i := i + 1;
end;
The 'For' Loop
Would be like this with a repeat loop...
i := 1;
repeat
This loop takes the form: begin
for variable := value1 [to|downto] value2 do writeln(i);
begin i := i + 1;
until i > 6;

Turbo Pascal 9
uses crt;
var
Notice how the condition has changed slightly. For the while loop, it
input : String;
would do the loop while the condition is true, whereas the repeat loop begin
does it until the condition is true. (Or while the condition is false). clrscr;
writeln('Enter the password');
readln(input);

Flow Control if (input = 'Pascal') then


writeln('Pascal is easy!')
{Note no semicolon for one line}
Flow control is basically changing what your program does depending else if (input = 'Basic') then
on the circumstances. In pascal there is the if statement which is used begin
writeln('Basic is not');
like so: writeln('very hard!');
end
if <condition> then else
statement block writeln('Wrong password!');
else if <condition> then end.
.
.
. This first prompts the user to enter a password and then reads that into
else intput. First it checks to see if input is 'Pascal'. Note that this is case
statement block sensitive. If it is 'Pascal' then it writes 'Pascal is easy!' and goes to the
end of the if statement (which happens to be the end of the program)
The condition takes the form: otherwise it checks the next condition. Is it 'Basic'? if it is then write
'Basic is not very hard!' (over two lines).If it is not then try the next
[boolean expression (AND|OR) [NOT] boolean expression condition. The next condition is ELSE. The code in the ELSE part of
...]
the if statement is executed if none of the other conditions in the if
statement are met.
and this can go on for a long time. For instance you could have a
Another flow control command, which is actually considered bad
condition:
practice to use, but is quite useful in some situations is goto. To use this
if (i = 1) and (j <= 3) or not (k <> 0) then declare a label in the label section and use it in your code. Then just say
statement block : goto label;

Note : (not k <> 0) is the same as (k = 0). eg:


Now here is an example program:
label label1;
program security;

Turbo Pascal 10
begin isn't a char or integer) then Pascal will give you an error. You can only
.
use ordinal types with the case statment.
.
. Another useful set of commands are the 'EXIT' commands. These are:
label1:
{Note the colon!}
. Halt
.
. This does the simple task of ending your program.
goto label1;
{Note no colon}
.
.
Exit
.
end. This command exits from the current procedure/function.

Easy! Break
You might have noticed that sometimes the if statement may get a little
cumbersome to use. ie: This command exits from the current loop.
if i = 0 then
...
else if i = 1 then

Records
...
else if i = 2 then
And so on. This is really cumbersome and annoying (you have to type
the same thing over and over). So what you want to use is the case You may find that sometimes there are a lot of variables that are related
statement. The above example would be done like so: to each other in some way. For instance you might find that you have a
case i of
0:...
door and know its height, width etc. and want to keep them all together.
1:...
2:... This sounds like a job for : records
3:...
4:...
Records are just a way of defining your own type. It is done like this:
5:...
6:...
end; type
doorType = record
Very handy. If you want to have multiple lines of code for one of the width : Integer;
options, then you must put the multiple lines between begin and end. If height : Integer;
you try to use the case statement with a String type (or any type that

Turbo Pascal 11
color : String[10]; readln;
end; end.
Now when you want to use doorType in your program you would do
this:
var
aDoor : doorType;
begin
with aDoor do
begin

Procedures and
width := 30;
height := 50;
color := 'Blue';
end;

Functions
end.
It's that easy! You may notice I have used the with statement. What this
means is that you don't have to prefix each of the fields with the
variable. So you can see that it can save you quite a bit of work.
Sometimes when you are programming you might need to use the same
So here's my sample program for finding the area of a door. piece of code over and over again. It would make your program messy
if you did this, so code that you want to use multiple times is put in a
program doorFind; procedure or a function.
{Find the area of a door}
The difference between a function and a procedure is that a function
uses crt;
returns a value whereas a procedure does not. So if your program has
type
multiple Yes/No questions then you might want to make a function
doorType = record which returns Yes or No to any question.
width : Integer;
height : Integer; Procedures and Functions both take parameters. These are values that
end; the procedure is passed when it is called. An example of this would
var
door : doorType; be...
begin
clrscr; ... drawBob (x,y); ...
writeln('Enter the door height');
readln(door.height); This would call the procedure drawBob. It passes it the values x and y
writeln('Enter the door width');
readln(door.width);
which the procedure will use as Bob's coordinates. The drawBob
writeln('The door area is ', procedure would look something like this...
door.width*door.height);

Turbo Pascal 12
procedure drawBob (x,y : integer); begin <Code here> end. function dayName (dayNumber : integer): String;
begin
Somewhere in the code it would use x and y. Notice that x and y are
case dayNumber of
declared like variables except for the fact that they are in the 1 : dayName := 'Monday';
procedure's header. If you wanted different types of parameters, eg. 2 : dayName := 'Tuesday';
integers and strings, then you must separate them by semi-colons like 3 : dayName := 'Wednesday';
this... 4 : dayName := 'Thursday';
5 : dayName := 'Friday';
procedure doSomething (x,y : integer; name : string); 6 : dayName := 'Saturday';
var 7 : dayName := 'Sunday';
<variables used in the procedure> end;
begin end.
<Code here>
end. Notice dayName assigns itself a value. The type of value that is to be
assigned to it is declared in the Function header after the parameters by
There might be a situation where you want the procedure to modify the going, ': <variable type>' which is in this case, string.
values passed to it. Normally if you did this in the procedures it
modifies the parameters but it does not modify the variables that the You should know enough about procedures and functions now so this is
parameter's values were given from. Anyway if you want to do this you where this lesson ends.
need to put var in front of the parameters. So if you wanted to do
something which changed x and y you would make a procedure like
so...

procedure modifyXY (var x,y:integer);


begin
Objects
<Modification of x and y> C++, Java and JADE are all object oriented developement languages.
end. This means that everything is an object which has properties and
'Methods' which are actions which the object performs.
So by now you should know how to use a procedure. Next we will talk
about Functions. "So what exactly is an object in Pascal?" you ask.
Well, an object in pascal is like a record with the addition of methods
A function is like a procedure except it returns a value. You could also (procedures) which belong to that object.
think of a function as a dynamic variable, ie it gives itself a value when
you have a reference to it. An example function is shown below which An object declaration would look something like this...
returns the day name from a number between one and seven. The
numbers represent the days in the week of course. type

Turbo Pascal 13
...
thing = Object
property : type;
...
Basic Variables in
Pascal
procedure nameofproc;
function nameofFunc (parameters);
end;
...
NOTE: You do not need empty parameter brackets if your procedure
A variable is an expression which represents a value. A variable is
has no parameters.
named such because it can have any value - its value is variable.
In the body of the program your procedures/functions etc. will be called
There are many different types of variables. For the moment we will
'thing.nameofproc'. Now within the procedure you can reference the
say that a variable can store a word or a number.
object by going 'self'. Ok here is an example procedure of an object
which adds one to the objects number property.
eg. Number is a variable which stores a number.
... Number is assigned a value of 23456.
procedure thing.incNumber; Therefore later in the program saying:
5 * Number + 6 will be the same as saying:
begin 5 * 23456 + 6, since Number is equal to 23456.
self.number := self.number + 1;
end;
... In Pascal, assigning variables is done by:
Number := 23456;
WOW! what an amazing procedure! So all of the variables of type This will make the variable, 'Number', equal to 23456.
'thing' will have that method at their diposal. You call the method by
going [variable name here].incNumber or whatever the procedure
happens to be called. There are several different types of number variables:

*Integer -Any Integer (Whole number) from -2^15 to 2^15


There are also special kinds of procedures called constructors and - 1
destructors. Constructors are supposed to happen when the object is *LongInt -Any Integer (Whole number) from -2^31 to 2^31
created and destructors when it is deleted. But since pascal treats - 1
objects like variables these must be called on like a normal procedure. I *Real -Any Real number (Number which can have
don't see any particular need to use them, except that they may increase decimal points)
from 2.9 * 10^-39 to 1.7 * 10^38
readability of your code. (It is accurate to 11 significant figures)

Turbo Pascal 14
Plus there are a few others, (which we won't mention yet). It isn't This will create the folling variables:
necessary to know all the details. myInt - This will be an Integer.
aRealNumber - This is a Real number.
All you really need to know is that Integer variables store Integers. thisIsAString - This is a sequence of letters with a maximum length of
LongInt variable can hold much larger Integers, and Real variables can 30 characters.
hold any number. booleanVariable - This contains a true or false value.

There are two main types of variable which hold letters: It is interesting to note that 'string' is a keyword. This is because it is
different to other variable types, as it contains a sequence or 'Array' of
*Char -Any single character characters. We will study Arrays in depth in a latter lesson.
*String -A word or phrase up to a set length
Make sure you understand all of the above before proceeding further.
A string is a series of characters. The default maximum length of a
string in Pascal is 255 characters. However if you don't want your
string to be that big you should set it smaller. You do this by putting the
length of the string in square brackets after the variable.

Pascal also has variable of type 'Boolean'. These can have the values of
either true or false.
Text Effects and
In Pascal, variables are declared at the beginning of the program or
Management
procedure. They must be declared after the keyword 'var'. Variables are Sometimes people want to present their programs in a more
declared in two parts. First is then name of the variable - how it will be entertaining way. There are some things in the Crt unit that can spice
referred to in your program. This is followed by a colon. Then is the up your program and make it neater. Remember to use the Crt unit in
type of variable, eg Integer, Byte. This is followed by a semicolon. your programs you put uses Crt; at the start of your program.
eg. Colouring your text makes the program more attractive and pleasing to
var
the eye. You will also be able to put borders around your text, and make
myInt : Integer; headings clearer.
aRealNumber : Real;
thisIsAString : String[30];
booleanVariable : Boolean;
TextColor
The way that that this works is shown below...
Textcolor (int);

Turbo Pascal 15
Where int is an integer between 0 and 16. Pretty simple really. Here is a the cursor to a certain point on the screen would be quite useful. Well
list of the different colors represented by int. fortunately there is such a procedure. This procedure is known as
gotoXY.
0 - Black
1 - Dark Blue
2 - Dark Green gotoXY
3 - Dark Cyan
4 - Dark Red gotoXY (x,y);
5 - Purple
6 - Brown
7 - Light Grey A common text screen has width 80 and height 25, so make sure you
8 - Dark Grey don't place the cursor off the screen. Doing this may yield unpredictable
9 - Light Blue results. After you have moved the cursor to a place the next write or
10 - Light Green writeln instruction will start from there. If you do a writeln the cursor
11 - Cyan
12 - Light Red will move down a line but will not relocate itself to the same x co-
13 - Pink ordinate as the last gotoXY.
14 - Yellow
15 - White

A text effect that you might want to have in your program might be
highlighting behind the text. This is done with a nifty little procedure
Program Structure
The Pascal programming language has several important words in it.
called textBackground. Remember though, that when you use this to These are called keywords. In example programs keywords will be
change the text background colour and clear the screen, it will clear it displayed in bold. This lesson will teach you some basic keywords and
using that background colour. eg If you change it to blue and clear the structure of your pascal programs.
screen, the whole screen will be blue. So when you clear the screen be
sure to change the background color back to black (unless you want the It is optional to begin your program with the keyword 'Program',
screen to go blue)! followed by your program name. This keyword is useful only to you. It
lets you identify what the program does quickly and easily.
textBackground
After this comes the keyword 'var'. This is followed by any variables
textBackground (int) you wish to use in your program. If you are not using any variables
int is a number between 0 and 7. then you do not need the 'var' keyword. (More on variables in the next
lesson.) Pascal will report an error if you try to use the 'var' keyword
Another thing that you might want to do with your program is have a without any variables.
menu at the center of the screen. This means that a procedure to move
Turbo Pascal 16
After this comes the keyword 'begin'. This indicates the beginning of 'Type' declares any variable structures - explained later.
the main part of your program. After this comes your program code.
The end of the program is indicated by the keyword 'end.'. Note the full 'Const' declares any constant values to use throughout your program.
stop after the word 'end'. These are anything which is always the same, such as the number of
days in the week. Alternatively if you use a set value throughout your
It is a good idea to comment your code so you can understand what it program, it is a good idea to make this a constant value so that it can
is doing if you look at it later. It is also important so that other people easily be changed if you later decide to do so.
can understand it also.
The 'Uses' keyword allows your program to use extra commands. These
In pascal you can comment your code in two diffent ways. Either { to extra commands are stored together in what is called a module or
start the comment and } to end the comment or (* to start the comment library. These modules have names such as CRT, or GRAPH. Each
and *) to end the comment. modules contains several extra commands. These commands will be
related in some way. Eg. GRAPH contains extra commands to do with
eg. graphics. CRT contains many general commands. (Even though CRT
Program DoNothing; {This line is optional} stands for Cathode Ray Tube - ie. the screen)

var eg.

begin uses crt, graph; {This means pascal allows you to uses the extra
commands in the crt and graph modules}
(*Note that comments can carry over
&nbspmultiple const
&nbsplines*) &nbspInchesInFoot = 12; {These are some constants you might
end. use}
&nbspDaysInWeek = 7;
This program does absolutely and utterly nothing. &nbspe = 2.1718281828;

In fact this program will create an error on the begin command. It will type
say 'variable identifier expected'. This is because the var keyword {Type definitions go here - don't worry about these yet}
should only be included if you have variables to declare.
var (*variables are declared here*)
There are also several other keywords, which are optional and must
come before 'var'. Some of these are 'type', 'const' and 'uses'. begin

Turbo Pascal 17
end.

Turbo Pascal 18

Вам также может понравиться