HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascalite Tutorial Table of Contents

Pascalite Programming: Introducing Variables

This version of this tutorial is fine tuned for the open source FPC, aka Free Pascal. Most of what you read here should also directly apply if you are using Borland's Turbo Pascal (TP hereafter). It is also available for free. Please send me an email with "complaints" if something doesn't work!

If you have a Pascalite, or the software emulation, then there's a separate tutorial for you, covering much of the ground as presented here for the FPC/ TP crowd.


Variables... where its "at", Man.

Forgive a nostalgic digression? As I start this tutorial, I must just pay homage to Per Ranhoff. I can remember extraordinary detail of a day in the late sixties when I age 15, I was sitting in my first computer lesson. Per started us off with the concept of variables. The specific machine he was showing us how to use was the same PDP-8 that he- who- would- become technical director of "Toy Story" started his programming on. But I played with it first! So what have I done?

So much for nostalgia.. down to work....

Inside the computer, at one level, everything is numbers. You don't always have to work at such a low level... in the previous tutorial (which you should read before attempting this one), we made a Pascal program display "Hi" without having to worry about the fact that the code for H is 72 and the code for i is 105, for instance.

Given that there are numbers involved, whether we worry about what they are, or merely use them to generate text, it follows that they must exist somewhere. Where they are is, again "at one level", the essence of variables.

Suppose you were writing a programme to calculate the tax payable on some transaction. You would need to have the tax rate available to your program. It could be stored in a variable.


We're not going to attempt to calculate taxes. We are again going to simply store strings of characters in variables.

In the following program, when it starts, the user will type in a short word. That will be stored in a variable, and then disappear from the screen. The program will then enter a loop, and exit only when the original "short word" has been entered again. Think of it as a password for quitting the program. Useless program? Perhaps... but something like it could be part of a program that a teacher wants to start on a classroom machine, and the teacher doesn't want pupils stopping the program.

Here's the finished program. Type it in, get rid of any error inducing typos, play with it. In a moment I'll take you through the new stuff. Try to figure out what's happening by yourself.

program Third;
uses crt;

var sItWas, sLatest:string;

begin
ClrScr;
writeln('Type a short word, then press the "Enter" key.');
writeln('This will set the password to be matched later.');
readln(sItWas);
ClrScr;

repeat
  writeln('Type the password again to leave this loop, or');
  writeln('type something else. In either case, then');
  writeln('press the "Enter" key');
  readln(sLatest);
  if sLatest=sItWas then writeln('Bye') //no ; here
     else writeln('No, that was not the password.');
  writeln;//this causes a blank line.
until sLatest=sItWas;

writeln('Press the "Enter" key to end the program.');
readln;//see notes
end.

See what's going on?

You can describe it on two levels. Such descriptions are called documentation, and you'll need to do documentation to get very far in programming.

The first level is user documentation. For our little program, it would be....

This program will ask once when first started for a password. It will then ask for the password again, repeatedly, until it gets it, at which time the program will end.

The deeper level is programmer's documentation. For our little program, it could be....

When the program begins, a string is obtained from the user, and stored in the variable sItWas. The program then enters a loop. On each pass through the loop, the user is asked for another string, and that is stored in variable sLatest. Just before program execution goes back to the start of the loop, the values stored in sItWas and sLatest are compared. When they both hold the same string, the program leaves the loop. One more message ("prompt") is sent to the user, and the program waits for the "Enter" key to be pressed one more time, and then the program's execution ends.

Notice: The second line of the program: "Var" is a word built into Pascal. It says we're going to "declare" some variables. We can't ever use variables unless we've declared them first. Happily, declaring them is no big deal. The variables we want are entered after var. There are all sorts of rules about what names are allowed. For now:

a) Don't try to use a word that is a built in word of Pascal, e.g. repeat, var, begin, then, etc.

b) Start the name with a letter.

c) Make the rest of the name out of letters or digits only. Don't use punctuation marks, nor spaces.

"sItWas" and "sLatest" are both variables, both names made up by me for this program.

After the last variable in the list you are declaring, you need a colon, then a word to say what TYPE of variable they are to be (more on this in a moment) ("string" is a built in word of Pascal for one of the allowed data types) and finally, as is (almost) always true, the line ends with a semicolon.

Note that the variable declaration goes after the "program..." line, but before the program's main "begin".

I hope the rest of the program is self evident. More explaining by me at this point may just give you a headache. Before reading on, have a hard look at the program. Try to make sense of what's going on for yourself.

Still not clear? The thing that you should get straight is why the program stops looping when you type the right thing in at the "readln(sLatest);" line.

Until the two variables, sItWas and sLatest, do not both hold the same string of characters, the program obeys the "until...", and loops back. When what's in sLatest is finally changed to what's in sItWas Pascal STOPS looping back to the "repeat", because the "until" condition is now true. The following isn't a real program, but perhaps it will reinforce what you've learned about the repeat... until loop:

program Life;

begin

repeat
  Work
until YouAreRichEnoughToRetire;

EnjoyYourHobbies;

end.

A little thing to notice: Notice the "readln" near the end of the program. A readln like that can be useful to let you see what is on the output screen just before the program ends.


We're now going to make a few additions to the program, to see a variable holding a number. Make the program...

program Third_v2;
uses crt;

var sItWas, sLatest:string;
  iCount:integer; //** 1

begin
ClrScr;
iCount:=0; //** 2
writeln('Type a short word, then press the "Enter" key.');
writeln('This will set the password to be matched later.');
readln(sItWas);
ClrScr;
repeat
  iCount:=iCount+1; //** 3
  writeln('You have gone through the loop ',iCount,' times.'); //** 4
  writeln('Type the password again to leave this loop, or');
  writeln('type something else. In either case, then');
  writeln('press the "Enter" key');
  readln(sLatest);
  if sLatest=sItWas then writeln('Bye') //no ; here
     else writeln('No, that was not the password.');
  writeln;//this causes a blank line.
until (sLatest=sItWas) or (iCount>5); //** 5
writeln('Press the "Enter" key to end the program.');
readln;
end.

I've marked each new line with a rem starting "//**".

At the first, we see another variable declared. Note that we don't need to repeat the "var" word, as long as the new declaration follows another one.

The type of iCount is "integer", which means we can store whole numbers, but not fractions, and not non-digit characters in the variable.

At //**2 we initialize the variable. iCount will hold some number at this point, but you can't be sure what it is. It is always wise to initialize variables. Note the "assignment operator", which is a colon followed by an equals sign. (":="). It is best, in your Pascal life, to think of this as a new "character". When you see "iCount:=1", instead of saying "iCount equals 1", say "iCount becomes 1", or "the variable called iCount has a 1 stored in it."

In some program or other, you could see "iCount=1" (note the absence of the colon). This would be a condition (We've met them before.) "iCount=1" boils down to "true" or "false" depending on what was most recently stored in the variable called iCount. You might come across a line saying....

if iCount=1 then writeln('The variable iCount is currently holding the number 1.');

Just take care to keep assignments to iCount ("iCount:=1") separate in your mind from conditions, e.g. iCount=1. If you read the ":=" as "becomes", it will help you.

Moving on through our demonstration program....

At //** 3 we have

iCount:=iCount+1; //** 3

If you really took in what you've been told recently, this will come as no shock. We are changing what is in the variable iCount. "iCount becomes what was in iCount previously, plus one." So iCount, after this line executes for the first time, holds 2.

At //**4....

writeln('You have gone through the loop ',iCount,' times.');//**4

We've seen writeln before... but notice that now we're not just sending text to the screen, but we're also fetching something out of the variable iCount, and putting that on the screen to.

At //**5....

until (sLatest=sItWas) or (iCount>5); //**

.. we've added a second way for the "until" to be satisfied. Because of this, in conjuction with the iCount:=iCount+1; inside the loop, even if you've forgotten what you originally entered, you will eventually exit the loop.

You've done it! You've reached a good place to take a break. Come back and do another tutorial from the series sometime soon!


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