HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascal Tutorials Table of Contents

Pascal Programming: First Steps, using Borland's Turbo Pascal



N.B. This version of the first tutorial is tailored to the needs of readers who wish to learn about Pascal using Borland's Turbo Pascal 5.5 (Ver 5.5 available as free download.)

Alternate versions of the tutorial are available tailored to the needs of others-

First steps with Pascal using the opensource FPC Pascal, aka FPK, aka "Free Pascal".

First steps with Pascal using the free Pascalite compiler.




"Let's start at the very beginning...."

A ten-year-old working at home without help should be writing programs in Pascal, using Turbo Pascal after applying him or herself to the following... if I've done it right! For those of you who are NOT new to programming, may I ask you to skim through the "Level One" material anyway? There are some things you might find useful. I'm working on a Windows XP machine. Windows 95 or the higher Windows should also work... Win98 SE might even work better than the XP environment!

"First catch your rabbit", in this case download and install the free software. I know: Its a hassle. It will take disc space. There are risks. Well, Pascalite, FBC and Borland's Turbo Pascal 5.5 (hereinafter referred to as "TP") installed for me with minimal hassle. As the man said... Try it, you (may) like it. And at least in this case, you have some reassurance that it "should" work.

Everyone: Please make use of the good helpfiles that come with your Pascal! My tutorials are not meant to supplant those, merely to take you through what is there in an easy (I hope) sequence. A useful feature of Pascalite and FPC is that if you are editing your program, and you want to look up say, GetTime, in the compiler's helpfile: Just type "GetTime" (without the quotes), leave the cursor in the word, or just past it, press F1, and, hey presto, you'll be taken to the right entry in the help file! (Ctrl-F1 for Delphi.) With Turbo Pascal on a Win98 machine, ctrl-F1 works but... it doesn't (yet) on my XP machine... I expect just a little tweaking of my mouse driver, or perhaps the compatibility mode would fix things. With TP, even on a Win XP machine, F1 brings up a guide to the editor... it is worth studying at some point.

Launch your Pascal. It will run inside an MS-DOS window. Right click the title bar, select properties, to set a more legible font size.

Once your Pascal is running, and you are in the edit window, enter the following:

program first;
uses crt;
begin
clrscr;
writeln('Hi');
end.

Invoke a save, either with alt-F S, or with F2. There is no "Save As", as such, in Turbo Pascal, but the first time you save something, you are given a chance to designate a name for the file. (The equivalent of "Save As" is on the file menu, as "Write to".) You could just enter "My Program" in the file name box and click "ok", but I'm going to suggest a slight improvement.

You may be in the folder (directory) into which you installed TP. Or someplace else... TP may be saving where you are from invocation to invocation. I would suggest making a separate folder for the files you create. This isn't as easy in TP as one would hope. Use your tool of choice- I like Windows Explorer- to create, outside of TP, any folders you want to use. It may pay you to stay near the top of the folder "tree", i.e. near the root directory of your drive, and to use MS-DOS folder names. (Start with a letter, use only letters and digits, and a maximum of 8.)

Once the folder is created, to move into that folder. Navigating the tree of folders is a little strange at first, but you can do it! The first strange thing is that you can't move around the directory during a "Save As" or "Load", or you can use File | Change Directory to get to where you want to save/ load something, and then do Save. The next strange thing is that, if you want to make a new folder for saving something, you have to close the Save dialog, make the folder using some tool (I like Windows Explorer) outside of TP, and then re-start the Save. Tedious, but manageable. Big help: The "file" called "..\" is a link to the parent folder of whatever folder you are in. Assign a name for the file, I'd suggest "First", or "PLT1aft". (PascaL Tutorial 1aft, which is the name of the web page you are reading.) Click "Save".

Once you've started something from scratch, i.e. by using File|New, and saved it once, then from that point on, if you use File|Save (or press F2), your work is simply saved where ever, and under whatever name you specified during the first save. The previous version of your work is over-written. Also, when you load something pre-existing, then doing File|Save will again simply replace the previous version. If you have been working on something, and you don't want to destroy the previous version, then use "File|Write to...", to change the name used for saving what you have.

Keyboard shortcuts, like F2 for save, are always nice, but in the TP environment, you really need to master the ones for your common tasks, unless you've got your mouse working for you. Several common Windows keyboard shortcuts, notably ctrl-C / ctrl-X and ctrl-V don't do the same thing in TP as they do in Windows.

So... you've typed in the little program I gave you. You've saved your typing.

Now invoke "Run" on the menu (Alt-R), and "Run" again (R) from the sub-menu (or just press ctrl-F9, the equivalent... as you can see from the right-hand column of the sub-menu.) You may well get an error message, in which case you'll get messages, and, probably the words "Compile Failed". Your Pascal will even tell you where it got confused... but that isn't always where the problem lies. If you can't see a fault where your Pascal says to look, check preceding lines. One favorite trick of new Pascal programmers: Leaving out a semicolon. (Although, in rare cases (which you'll learn to be aware of), a superfluous semicolon may be the problem!) The error message even tells you which line of your program holds the place Pascal got confused.

When there's a message on the screen, you tell it "Okay, got that, now go away" by clicking on the little square in the upper left of the message's box.

The interface used with TP comes from the days before Windows. (Yes, there was such a time... When I was a boy... (Don't let me get started!!))

TP does work in a "windowed" environment, but you could be forgiven for not liking it. However the concepts of maximized and "restored" windows are present. (I'm not sure how to mimic a minimize.) You can... really... even re-size the windows. It's just all less intuitive than modern Windows. (I strongly suspect that the "windows" environment created for TP, which can create applications using the same "windows", heavily influenced the development of Mr. Gate's "Windows". Of course the people who created the Mac interface lso deserve a lot of the credit.)

If you had an error, find it, re-save, and try to run the program again.

Don't be alarmed if it seems that nothing has happened.

When you finally (or if you are a careful copier, the first time you...) ran the program and got no "complaints", you may have noticed the screen flash, and you might even have seen the "Hi" you've accomplished. But it didn't stay. Your sourcecode ("program first; begin...") came back. Use the menus: Alt-R (To get the "Run" menu, then U for "user screen". That SHOULD bring you to a screen with "Hi" on it... someplace. There may be other "stuff" there, too. I must confess that I had a little trouble with this, so just keep at it... you'll get that User Screen thing eventually! It's been a while since I used TP, and I am a very new user of FPC. I have every confidence that both are usable... it just takes a little struggling to get fluent. I hope these pages are, at least, saving you some of the struggle? Use the menu to access the User Screen (the menu SAYS the key combination "Alt-F" should do it... but it wouldn't for me, on my XP machine.) And use the escape key to go back to the sourcecode.

Whew! It gets easier. I promise.

Just one last little flourish to finish off this first tutorial. You won't finish a real program in one go. Part of learning to program is learning to build something up by degrees. Add a second writeln line, specifically writeln('Bye'); Compile and run the program again; see your new masterpiece in action (or fix the typos!) and cheer... you're a programmer!


Oh yes... I said I'd take you through the program....

Every Pascal program begins with the word "program". After that, you supply a name; I chose "First" in this case. (PLT1a would have been better, but might have confused you at that stage.) And then the line is finished with a semicolon. You get a lot of semicolons in Pascal. More on them another time.

Next in our simple first program came "uses crt;" This tells the compiler to use some stuff that is available to it in an always present unit called "crt". We needed this for clrscr to work.

Next in our simple first program came the word "begin". For every "begin" there is an "end". You can have many begin/ end blocks, and they always nest. Leaving out the rest, a complex program might look like.....

begin
  begin
    begin
    end;
  end;
  begin
    begin
      begin
      end;
      begin
      end;
    end;
  end;
end.

Did I scare you? I hope not... I wanted you to look forward to the excitement of a really sophisticated project! Anyway. Begins are always paired with ends, as I said. You do NOT put a semicolon after the begin (unusual), but you do put one after the end... usually. (I'll discuss the exceptions another time.) After the final end in a program, you put a period (full stop) instead of the semi-colon.

"Clrscr" cleared the screen.

write and writeln are words built into all the Pascals we've been discussing. (A rough equivalent for Delphi, by the way, is "showmessage", e.g. showmessage('Hi');).

Note that we put apostrophes around the text we want written, rather than the quotation marks we would use in everyday English.

And we've already covered the "end.", so we're almost done! In a moment, you can go give yourself a treat.

Or return to the index of Pascal tutorials for another one!

One little thing before we go. You see the way things are indented in the big block of begins/ ends above? You can move text around to set it out any way that helps you see what is going on. Pascal is unfussy about spaces and new-lines. Guess what? "More on this later." (Again!)


Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
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.


Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers. The Google advertisers pay me. I know nothing about them or their services, of course.)



Valid HTML 4.01 Transitional Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Valid CSS!


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....