HOME     >    >    >     TUTORIALS TABLE OF CONTENTS

An introduction to Pascal for Basic speakers

This has good information

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Delphi supersite: vote here Please click here to help my Delphi tutorials site get publicity


You want to know a bit about Pascal- good! When you do, you can make much better progress with Delphi, a great environment for Windows programming. (Generally... not just database applications.) When you know Pascal, you can turn out great DOS applications with minimal effort.

'But I don't have a Pascal compiler' I hear you cry. Don't worry! There are freeware compilers... quite good ones... or, treat yourself right, and buy Borland's Turbo Pascal. Lovely environment to work in.

Click here for information on freeware compilers. (If the FREE Pascalite software doesn't suit you... though I'd be interested to know what you think is wrong with it. Click here for Pascalite information.)

BASIC:
10 PRINT "Hello Cyberspace"

PASCAL:
program demo1;
begin
writeln('Hello Cyberspace')
end.

Note: ' instead of " to set off a string.

The most wonderful thing (to me!) about Pascal is that it has a highly formal structure. Learning what that is, and the variations available on the theme is the key to happy Pascal programming.

A notation that doesn't reduce to this text based medium is used to describe the structure. Other reference materials, including the Delphi and Turbo Pascal help files, make use of it. Master its interpretation early, and you will go far. I will attempt a sample in 'character graphics':

A pascal program is...
program NAME;-----------------------BLOCK-----------------------.
               \/              \/           \/         \/
                \ DECLARATION; /             \ ;BLOCK  /

You read these diagrams by following the lines. The one above says that a very simple Pascal program, with no declarations and the simplest possible block ( begin end ) consists of just...

program demo; begin end.

It wouldn't do anything, but it would compile and run.
(In most cases, you can start a new line or not whereever you see fit. If in doubt: start one!)
The diagram also allows our

program demo1; begin writeln('hello') end.

because...
begin 'writeln('Hello') end
.... is a block.


An incomplete definition of block would be that it is the word 'begin' followed by a statement (e.g.'writeln('Hello')) followed by the word 'end'.

Now the fun begins.... If you have two statements, and 'glue' them together with a semicolon, that is also a statement. Why is that fun? Because it means that the following also satisfies the rules for a legitimate program:

program demo1
begin
writeln('Hello');
writeln('Goodbye')
end.

Those semi-colons are going to take some remembering... but they are worth it. In general: if in doubt, put one in. I could have had one after the ('Goodbye')... it would have 'created' a statement made out of two obvious statements glued together and with a 'third' 'statement', consisting of nothing (!) glued onto the end. The extra semicolon does no harm. Missing semicolons do! This, by the way, is probably the moment to deal with a little detail: At the end of the program, there IS meant to be a '.', as shown.

We'll come back to the semicolon issue in a moment.

BASIC
10 x=5
20 IF x=5 THEN PRINT "It is 5" ELSE PRINT "It is not"

Pascal
program demo3;
var x:byte;
begin
x:=5;
if x=5 then
   writeln('It is 5') (*no ; here*)
  else
   writeln(It isn''t');
writeln('So there.');
end.


Seems like a lot of code for something BASIC could do in two lines.. but it is worth it! When you start writing real applications, the value of the extra material begins to emerge. (And the overhead becomes a small proportion of the package.)

Working through what we have...

var is a reserved word, i.e. one which is built into the language, has a particular meaning, like PRINT in BASIC. It comes from VARiable. You can't use a variable in Pascal unless you have previously declared it (said that you are going to), AND said what 'type' of data will be going in that variable. The word type is used in a very specific sense here. For now, the two types you need are byte and string. Byte type variables can hold the integers 0 to 255 (inclusive). String type variables can hold strings of characters. The var x:byte; line is a declaration. You can have several, as the lines in the program syntax diagram indicate.

x:=5; Notice that when you want to say 'make the value stored in x 5', you use two characters : and = together as if they were one. The statement x:=5 makes the variable x hold 5.

if x=5 then.... Here we see the equals sign again, but this time on its own, there is no colon in front of it. Here we are doing a test. We are comparing what is in x with 5.

In general, the if statement is as follows:

if CONDITION then BLOCK else BLOCK

The main place you do NOT put a semicolon is between the end of the then's statement and the word else. All of the above is part of a basic statement, and putting a semicolon in would be like...

BASIC
10 PRINT "You cannot
20 just break things up in their middle"

Putting something between (* and *) makes a comment, something the compiler ignores. { and } work the same way. Good programs almost always have LOTS of comments in them just helping to clarify what the programmer intended or required. I always put a (*no ; here*) comment just in front of an else. It saves time wasted tracking down why something won't work.

You don't have to have the 'else BLOCK' part, so the following is okay:

program Demo;
var x:byte;
begin
x:=5;
if x=5 then writeln('It is 5');
end.

______________
We have already seen the var declaration. Another important declaration has the general form....

procedure NAME;BLOCK

The following will work, comments in a moment...
program demo5
var x:byte;

   procedure XWasFive;
   begin
     writeln('X was 5');
   end;

   procedure XWasNotFive;
   begin
     writeln('X was not 5');
   end;

begin (*main*)
x:=5;
if x=5 then XWasFive else XWasNotFive
writeln('So there.');
end.


First a detail: The blank lines and indents mean nothing to the compiler, but they can be a big help to human readers.

Second: Important and not obvious: When the program runs, the first thing it does is put 5 in the variable x. In other words, it skips over the procedure declarations to the 'main' begin. The program only does what is declared in the procedure declarations if those procedures get called by some line in the program.

Third: Why use procedures? In the simple program above, the procedures (XWasFive and XWasNotFive) are overkill... but in real programming, it is enormously helpful to be able, as it were, to make up new words for the language. You can examine the program at a high level by looking at the main block. The details of what should happen if, say, x holds 5, can be hived off into a separate block to be studied in detail only when necessary. And when it is necessary, there won't be other things involved to distract you.

Fourth: Procedure declarations must come before any use of the procedure. It takes a little getting used to, but it means the compiler can be fast.

_____________
This scratches the surface. I hope you found it useful.

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.
Link to Tutorials main page
How to email or write this page's editor, Tom Boyd


- - o - -