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

Pascal Programming: Second program

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.

Enter the following program....
program second;
uses crt;
until 4=5;

For FPC, you can copy that from here by selecting it and pressing ctrl-C in the usual way. Within FPC access Edit from the menu, and select the "Copy from Windows" option near the bottom of the list. Also: Within FPC, the keyboard shortcut for deleting a selected block of text is ctrl-delete.) You can probably do something similar under TP, but I haven't run the tests yet. Experiment!

When you've entered the code, first save what you've entered. Remember that...

"Second" would be a good name to use for saving this program. (You can put all these little programs from the tutorials in a shared folder), then run the program. You should get a black screen with white writing saying "Hi" on every line. You won't go back to the editor screen, as before, because the program you started is still running. It will run forever. This is called an infinite loop, and is generally thought to be a Bad Thing.

Before "ctrl-alt-delete" became the elephant gun that would kill most rogue processes, ctrl-C and ctrl-Break were popular, and it is the latter we need here. (The "Break" key is usually also the "Pause" key, and is usually at the extreme upper right of the keyboard. To the left of the numeric keypad, but above it. At the right hand end of the row of function keys.

Press ctrl-break to wrest your computer back from the infinite loop which you created. The Good News is that it will get your computer back. The Bad News is that FPC will be shut down, as well. (I have at some future date to check the exact performance of TP in this regard. For now, experiment! I'm sure you'll find what you need.)

Restart FPC. Sorry. We'll try to avoid infinite loops in the future! You may find that FPC comes up with the program you were working on back in the edit window. If not, re-load it. (You did save it, didn't you?)

Two details: The ClrScr clears the output screen. Without it, you would see "stuff" left over from starting up your Pascal, and, when you are getting more proficient, "stuff" left from previous runs (in the current session) of the compiler. The uses crt line tells your Pascal to load a "unit" (more on that, much later). Units are "libraries" of code for things that you don't need in every program. The ClrScr command is part of the Crt unit, so when you want to use ClrScr, you have to tell your Pascal to "use" the Crt unit, so that your Pascal will have the necessary code for ClrScr. ("Code" here as in "computer instructions", not as in "secret code").

The word Repeat is one of the rare instances of something that is not followed by a semicolon. It is like Begin, which was discussed in the first tutorial. "Repeat" is always paired with an Until. The Until is always followed by something which is either true or false. The "something" is called a condition, in this case "4=5". 4 doesn't equal 5, of course, so the condition is always false. You will soon meet more complex (and sensible!) conditions. (People with some experience of programming may wonder why I didn't say "until false". I find that beginners are often thrown by "Until false". "Until 4=5" is more intuitively something that hasn't happened, so the until is unsatisfied.)

Our program, because of the "repeat/ until" pair will perform the "writeln('Hi');" instruction over and over again. As I said, this is called an infinite loop. Loops are powerful and useful, though you rarely make infinite loops... on purpose! Some bugs are based on inadvertent infinite loops.

Modify the program so that it says

program second;
uses crt;
until 4=5;

The word "repeat" has been moved up one line. You can make the modifications "by hand" (deleting and re-typing), or you can do them by selecting the text, doing shift-delete, moving the cursor and doing shift-insert. Shift-delete is the ctrl-X of this environment, shift-insert the ctrl-C.

When you've made the change, re-save the program and re-run it. It almost looks as though "nothing" is happening. Depending on various things, the "Hi" may appear and disappear, and we didn't revert to the edit screen, as you "should have".... but that's not much, is it? None-the-less, the program is doing ClrScr / writeln('Hi'); over and over again. Notice the big effect on the results that arises from moving the "repeat" just one line. You should be able to see why the program behaves differently. I'm afraid you'll have to kill your Pascal with ctrl-break again, and re-start it again.

At this point, the FPC/ TP version of these Pascal tutorials takes quite a different path from that taken by the Pascalite version. Pascalite, being written for a specific piece of hardware, has a few special features not found in more general Pascals. As the hardware is a microcontroller, the usual way you give it input is more basic than is usual on a PC. The Pascalite tutorials were able to defer talking about input, and we can't. It isn't a big deal, though.

Change the program to what follows. You may have to re-size your browser window to see all of every line....
program second_V2;
uses crt;
var sTmp:string;

    writeln('Press a key, and then press the "Enter" key');
    if sTmp='X' then writeln('Bye')
        else writeln('Press X to eXit program. (It must be an upper case X).');
  until sTmp='X';

Save this. I'd suggest "Second_v2" as the name. In general, keep names short, and without any spaces. Also, it is best to use what is on the "program..." line of the sourcecode as the name with which the program is saved. The name in the sourcecode can't have hyphens, so either CombineWordsWithoutSpaces or use_the_underscore.

Run the program. You should get a message telling you what to do. We'll look at the code in detail in a moment. Press any key except X, and then press the "Enter" key. You should get the message again. Enter another key, and you get the message again... and again... and again. This is a loop! (This lesson is supposed to be about loops, after all!) But! This is not an infinite loop. Press an uppercase ("capital") X, and you may see "Bye" briefly (or the screen may go away before you can perceive the "Bye") and the program will end, putting you back in the FPC/ TP editor screen.

I hope that if you work through the code, what is going on is not too hard to discern? None- the- less, read what follows carefully, to learn about a few odds and ends

var sTmp:string;

"Var" is a Pascal word. It says we are about to "declare" some "variables". You will see var statements in many places within a program. The "sTmp" after var is a name we made up. We are declaring a "new word", for use in this program. We can be pretty inventive with our names, but...

There may be one or more names, separated by commas, after the "var", but eventually there must be a colon, and after the colon the name of a "type" known to Pascal. "Type" is a big topic, which you will master gradually. For now, all you need to know is that "string" is a good type for sTmp. (And its name is the reason we used "s" for the first letter of sTmp.)

"var sTmp:string;" created a variable (more in a moment) called sTmp, a variable capable of holding "string" type data. ("String type data" is data consisting of one or more characters in a string. Examples: "abc", "Fred", "123"... or even "234QEWFQ#$" !) You must always "create" ("declare" is the term usually used) variables before you use them.


When the program executes this line, it will sit and wait until the user types something, and presses the "enter" key. Once that has happened, sTmp will "hold" whatever the user typed. That's what variables are for: They "hold" things. We'll see what that's good for in a moment.

if sTmp='X'...

What, on the page, is two lines (or more, depending on your browser window).....

if sTmp='X' then writeln('Bye')
  else writeln('Press X to eXit program. (It must be an upper case X).');

... is, as far as Pascal is concerned, one line.

In essence, that line is....

IF (something) THEN (do this) ELSE (do that)

This is a tremendously powerful statement, but it does take some explaining.

The "something" part must be "a condition". A condition is something that is either true or false. In our little demonstration program, the condition is....


this is not going to make sTmp equal X; it is asking "Does sTmp hold the letter X?"

Does it? That depends on what we typed when the program came to the readln statement. What we put into the variable sTmp then is what the computer compares to "X" now. (Note: We are not doing anything mathematical or clever here. We don't mean "x" as in algebra, e.g. as in "y=mx+c".)

Another aside: be careful with quotes. Pascal distinguishes between single quotes ('s) and double quotes ("s). (You will sometimes even encounter double single quotes (''), but we haven't had any of them... yet!) Pascal is fussy. In general, it wants single quotes.

Back to the if... then... else...

If the condition is true, then the computer will do what is indicated as "something" above. In the program we are discussing, what the computer does is....


On the other hand, if the condition is not true, then the computer will do what is indicated as "do that" above. In the program we are discussing, what the computer does is....

writeln('Press X to eXit program. (It must be an upper case X).')

Note that there is only one semicolon for this whole, logical, line... at the end. Semicolons are the "glue" between statements. In particular, there is no semicolon before the "else".

Another thing to notice: the word "then" has at least two meanings in English. It can be a matter of sequence: I put on my socks, THEN I put on my shoes. Or it can be a matter of consequence: If you eat three pizzas, THEN you will be sick. While sequence comes into that, too, I hope you'll see the more significant "consequence" element. In programming, "then" is used in the consequence sense.

Time to talk about presentation. Note how I have indented the second part of the "if... then... else... statement. The indent is optional, but it makes reading through your program easier, because it emphasizes the fact that the "else..." stuff is just a continuation of what started on the previous line. Note that I've used indenting to emphasize other elements of the program's structure, too, for instance the extent of the repeat... until loop.

Let's talk some more about where semi-colons go. That is linked to "What is a statement?" The concept of statements is important in Pascal, and gets subtle in more complex cases. The big thing to notice here is that there is no semicolon before the else. The concept of statements is a little like the concept of sentences in everyday English. Both of the following are valid (regardless of where you break the lines, as in Pascal), and comparable to single statements in Pascal:

I will go to town.
I will go to town if it is raining.

It is incorrect to write:

I will go to town. If it is raining.

Pascal's semicolon is a bit like English's period (full stop). Because so many lines in Pascal end with a semicolon, and because putting one before an "else" causes problems, and because if...then...else statements are usually split across two (or more) lines, I still enter similar lines in my programs as follows:

if sTmp='X' then writeln('Bye') //no ; here
  else writeln('Press X to....
The text after the "//" is ignored by Pascal. The chance to add comments thus can be a real help to the programmer. Comments are also called remarks, or rems. Multi-line comments can be written thus...
(*This is a rem
   that splits across two lines*)

... or

{This is a long comment:
   0: zero
   ... etc}

The work so far may have seemed "boring", but you have to walk before you run. If you're not determined (stubborn?) enough to slog through this dull stuff, you're probably not temperamentally suited to programming, anyway. But that's okay: Higher wages for those of us who are! Even if you aren't looking for wages, hang in there? It does get fun!

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