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

Pascalite Programming: Introducing Variables

This version of this tutorial is fine tuned for a Pascalite, or the free software Pascalite emulation. Please send me an email with "complaints" if something doesn't work!

If you have the open source FPC, aka Free Pascal or Borland's Turbo Pascal (TP hereafter), then there's a separate tutorial for you, covering much of the ground as presented here for the Pascalite crowd.

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 was sitting in my first computer lesson. Per started us with the concept of variables. The specific machine he was showing us how to use was the same machine that he who would become technical director of "Toy Story" started his programming on not many years later.

So.. down to work.

Inside the computer, at one level, everything is numbers. You don't always have to work at such a low level... we have already made the Pascalite 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... but it pays to be aware that there are numbers inside the machine.

Given that there are numbers involved, it follows that they must exist somewhere. Where they are, again, at one level, is 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.

In the following example, we're going to look at the state of the b0 switch at one point in the program. If it is off, sometimes called open, then the computer "sees" an answer we should call "false" if it looks at b0. If b0 is on, aka closed, then the computer sees "true". This is why in our earlier programs we could say things like "If b0 then...". We're going to use the if.. then... else statement and variables to store a 1 when the computer sees "true", and 0 when it sees "false". We're storing the indication of the switch's state so that at a later point we can see what state it WAS in previously.

Actually, I lied. We're not going to look at the b0 switch, the one we used before. We're going to look at the switch next to it, which is called b1. (Computer people often call the first item of a set item "zero". Not what we're used to in everyday life, but there are advantages. A switch called b0 by any other name...)

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 guess what the point of the additions are.

program third;
var ItWas,ItIs: byte;
if b1 then ItWas:=1 else ItWas:=0;
if b0 then write(lcd,'b0 true'){no ; here}
   else write(lcd,'b0 false');
if b1 then ItIs:=1 else ItIs:=0;
until not(ItIs=ItWas);

Did you guess what the additions did?

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

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

This program will repeatedly put "b0 true" or "bo false" on the LCD, depending on the state of switch b0.
If the user changes the state of switch b1, the program will stop running.

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

When the program begins, the state of switch b1 is checked, and a zero or a one is stored in the variable ItWas. The program then enters a loop, displaying "b0 false" or "b0 true". On each pass through the loop, switch b1 is checked again, and a zero or one is stored in the variable ItIs. Just before program execution goes back to the start of the loop, the values stored in ItWas and ItIs are compared. If they are not equal, the program doesn't loop, it goes on past the "until" statement. As there's nothing more to do, the program stops.

(Yes.. there was a scheme which would have been less programming effort, but it would have meant putting the "do looping" switch back to "yes" every time you switched it to "don't" to exit the program.)

Notice: The second line of the program is new. "Var" is a word built into Pascal. It says we're going to "declare" some variables. We can't use them unless we've declared them. 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. 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.

"ItWas" and "ItIs" 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) ("byte" is a built in word of Pascal for one of the allowed data types) and finally, as (almost) ever, the line ends with a semicolon.

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

Once we have declared the variables, we can use them. Never assume you know what is in a variable that you have not first put something in. This "putting in" is called assigning a value. To put a 1 in the variable ItWas, the correct command is:

Think of the :=, which you type by pressing first the colon and then the equals sign as a single entity. When you write (by hand) a capital X by making two lines, you don't confuse it for "two things" do you? Likewise, think of := as the assignment operator. It pays to read the example above as "ItWas BECOMES 1". In other words, the variable ItWas will, until further notice, contain a 1.

Don't let the use of the equals sign in typing the assignment operator confuse you. When we say ItWas:=1 we're causing something to happen.... 1 is stored in ItWas. The equals sign (without the colon) IS USED in Pascal, and it is used logically: It is used for comparing things. We saw this in our first program which had the line "Until 4=5". We were comparing 4 and five, asking if they were equal. Clearly, they are not equal, so "4=5" boiled down to, was equivalent to something not true. That's why we repeated the loop endlessly. We were supposed to REPEAT.... UNTIL (something) was true. Until now, the "something" was "4=5", which will never be true.

In our latest program we have "until not(ItIs=ItWas)" look at that in layers, from inside to outside.....

"ItIs=ItWas" asks "Is what is in the variable called ItIs the same as what is in the variable called ItWas?" If what's going on is not immediately obvious, look closely at the program and notice where we put things into those two variables. ItIs will hold the same number as ItWas... at first. So (the contents of) ItIs will equal (the contents of) ItWas. (Usually we cut that down and just say "ItIs equals ItWas". The "the contents of" part is understood.) Now... if ItIs equals ItWas, then ItIs=ItWas boils down to "true".

Now look at the next layer outwards, that word "not". It is built into Pascal. If you say "not(something that's true)", it's equivalent to saying "false". On the other hand, if you say "not(something that's false)", it's equivalent to saying "true".

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 change the state of the b1 switch.

Before you change it, the program obeys the "until...", and loops back because ItIs and ItWas both hold the same number, and so ItIs=ItWas is true, but the overall condition that the until is looking at is "NOT(ItIs=ItWas)". When the number in ItIs changes, ItIs no longer equals ItWas, so ItIs=ItWas boils down to "false", and "not(false)" is true, so "until" sees "true", so Pascal STOPS looping back to the "repeat".

Simple ideas... really... just not simple to explain, and it is easy to get something back to front. You'll be doing lots with conditions as you go further into programming. You'll get it!

Most Pascals have a way of testing for "not equal", for numbers or for Boolean values. You could, usually, say something like
until (ItIs<>ItWas);
... but Pascalite has nothing like "<>" to stand for "not equal." Tedious, but not hard to work around. To recap: In place of A<>B you merely write not(A=B).

Good place to take a break. Come back to the index of Pascal tutorials for another one 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 .....