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.
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 deeper level is programmer's documentation. For our little program, it could be....
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.
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.
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!
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.
....... P a g e . . . E n d s .....