Fun

Imagine that youíre running for President against Calvin Coolidge. To win, you must pass Cal in the polls.

You must pass Cal. "Pass Cal" is the correct way to pronounce "PASCAL", the name of the computer language that programmers are falling in love with.

PASCAL is harder to learn than BASIC. But once youíve learned PASCAL, you have amazing power: you can write fancy programs more easily in PASCAL than in BASIC because PASCAL helps organize your thinking; and your programs will run faster, too!

American teachers require kids to master LOGO in elementary school, BASIC in high school, and PASCAL in college. Gifted kids are given the opportunity to start BASIC and PASCAL even sooner ó and so are kids in progressive schools.

Many high-school seniors take a PASCAL test given by the College Board. Seniors who pass can get "advanced placement" college credit and skip the collegeís first year of computer courses.

The nicest kinds of PASCAL are Quick PASCAL (published by Microsoft) and Turbo PASCAL (published by Borland). Theyíre easy to understand, run quickly, and cost little.

Quick PASCAL runs on the IBM PC and lists for just $99. Discount dealers sell it for just $42.

Turbo PASCAL is available for the IBM PC, Mac, and computers using the CP/M operating system. The newest version of Turbo PASCAL for the IBM PC is Turbo PASCAL 7, which discount dealers sell for $99.

If youíre on a tight budget, which should you buy ó Quick PASCAL or Turbo PASCAL? Since theyíre very similar to each other, youíll probably buy Quick PASCAL because it costs less. Another advantage of Quick PASCAL is that it lets you edit your programs more easily than Turbo PASCAL. On the other hand, Turbo PASCAL runs your programs faster, consumes less RAM and disk space, comes with instruction manuals that are larger and more thorough, provides on-screen tutorials that are easier to use, understands better commands for advanced programming, and is the standard against which all other versions are judged. (Quick PASCAL was invented just to imitate Turbo PASCAL more cheaply.)

This chapter explains how to use Quick PASCAL and the most common versions of Turbo PASCAL (versions 4, 5.5, and 6).

If you have Turbo PASCAL 7, follow the instructions for Turbo PASCAL 6, which is similar. If you have Turbo PASCAL version 5, follow the instructions for version 5.5.

Iíll comment on how other versions of PASCAL differ.

Copy to the hard disk

Quick PASCAL and Turbo PASCAL come on a pile of floppy disks. You should copy those disks to your hard disk. Hereís how.

Quick PASCAL Turn on the computer without any floppy in drive A.

The original version of Quick PASCAL comes on five 5ľ-inch floppy disks. When you see the C prompt, put the Quick PASCAL Setup/Utilities Disk in drive A and type "a:". The computer will display an A prompt. Type "setup". The computer will say "Microsoft Quick Pascal Setup Program".

Press ENTER twice. The computer will say "Easy Setup Menu". Type the letter I. Press ENTER four times.

Put the Quick PASCAL Program Disk in drive A, and press ENTER. Put the Quick PASCAL Advisor Disk in drive A, and press ENTER. Put the Quick PASCAL Libraries Disk in drive A, and press ENTER. Put the Quick PASCAL Express Disk in drive A; press ENTER twice.

Press R, so that the computer says "Setup Main Menu". Press X, so that you exit to DOS. Youíll see an A prompt.

Turn off the computer, so you can start fresh.

Turbo PASCAL 4 Turn on the computer without any floppy in drive A.

After the C prompt, type "md turbo" (so youíre making a subdirectory called TURBO). After the next C prompt, type "cd turbo" (so youíre changing to the TURBO subdirectory).

Turbo PASCAL 4 comes on three floppy disks. Put one of those disks in drive A, and type "copy a:*.*" (which copies all the floppyís files onto the hard disk); follow the same procedure for the other two disks.

Turn off the computer, so you can start fresh.

Turbo PASCAL 5.5 and 6 Turn on the computer without any floppy in drive A.

Your Turbo PASCAL comes on four 5ľ-inch floppy disks. When you see the C prompt, put the Install/Compiler disk in drive A, and type "a:install". The computer will say "Turbo PASCAL Installation Utility".

Press ENTER four times. Youíll see "C:\TP". Press the BACKSPACE key, then type URBO so you see "C:\TURBO". At the end of the URBO, press ENTER.

Press the F9 key.

If youíre using version 6, put the Turbo Vision/Tour disk in drive A, press ENTER, put the Help disk in drive A, press ENTER, put the BGI/Utilities disk in drive A, and press ENTER twice. (If youíre using version 5.5, put the Tour/Online Help disk in drive A, press ENTER, put the OOP/Demos/BGI/Doc disk in drive A, press ENTER, put the Utilities/Misc disk in drive A, and press ENTER twice.)

Youíll see an A prompt. Turn off the computer, so you can start fresh.

Start PASCAL

To start using PASCAL, turn on the computer without any floppy in drive A.

If youíve put the DO.BAT file onto your hard disk (as I recommended in the MS-DOS chapter), your life is easy! Just type "do qp" to do Quick PASCAL; type "do turbo" to do Turbo PASCAL.

If you have not put DO.BAT onto your hard disk, your life is harder! You must type "cd qp" and then "qp" to do Quick PASCAL; you must type "cd turbo" and then "turbo" to do Turbo PASCAL.

Press the CAPS LOCK key, so the computer will automatically capitalize everything you type (and your typing will look like the examples in this chapter).

Hereís what to do next.

Quick PASCAL You donít have to do anything!

Turbo PASCAL 4 Press the E key (which means "Edit"). Make sure the cursor is at a blank part of the screen. (If itís not, press the DELETE key several times.)

Turbo PASCAL 5.5 Press the F6 key (so the cursor moves to the bottom of the screen). Then press the F6 key while holding down the Alt key (so the bottom of the screen says "Output"). Then tap the F6 key without the Alt key (so the cursor moves to the top part of the screen).

Turbo PASCAL 6 Press F10 then W then the letter O (so the cursor moves to the bottom of the screen). Press F10 again then W then T. Press F6.

How to program

For example, type this PASCAL program:

BEGIN;

WRITELN('I WOULD LIKE TO KISS');

WRITELN('YOUR BOTTLE OF WINE');

END.

The program begins with the word BEGIN and ends with the word END. Every line ends with a semicolon, except that the bottom line ends with a period. The middle lines say WRITELN, which tells the computer to WRITE a LiNe.

Naming the program If you wish, you can put an extra line at the top of the program, to give the program a name. The name can be up to 8 letters long. For example, if you want to name the program WINE, you can begin the program by saying PROGRAM WINE, like this:

PROGRAM WINE;

BEGIN;

WRITELN('I WOULD LIKE TO KISS');

WRITELN('YOUR BOTTLE OF WINE');

END.

Colors If your screen can display colors, hereís what you see. In Turbo PASCAL, the program is yellow. In Quick PASCAL, most of the program is white, but the keywords (PROGRAM, BEGIN, and END) turn purple and the strings (`I WOULD LIKE TO KISSí and `YOUR BOTTLE OF WINEí) turn light blue.

 

Run the program When you finish typing the program, tell the computer to run it. Hereís how. For Turbo PASCAL 4, press F10 then R. For Turbo PASCAL 5.5 & 6, press F10 then type RR. For Quick PASCAL, press F5; and if the computer asks "Rebuild?", press ENTER.

If you typed the program correctly, the computer will write:

I WOULD LIKE TO KISS

YOUR BOTTLE OF WINE

If you typed the program incorrectly, the computer will say:

Error

(If youíre using Quick PASCAL, then press ENTER.) The computer will put the cursor near your error. Correct the error (by using the arrow keys, DELETE key, and other word-processing keys), then tell the computer to run the program again.

After a good run After the computer has run the program successfully, do the following: for Quick PASCAL, press ENTER; for Turbo PASCAL 4, press ENTER then E; for Turbo PASCAL 5.5 and 6, you donít have to do anything!

Youíll see the program again. If you wish, edit the program again and run the edited version.

Other versions Some versions of PASCAL require you to say PROGRAM WINE at the top of the program; if you donít say PROGRAM WINE, those versions gripe.

Ancient versions require the top line to say this:

PROGRAM WINE(INPUT,OUTPUT);

Some Apple 2 and Mac versions automatically indent all lines between BEGIN and END and force you to omit the semicolon next to the word BEGIN.

Advanced editing

While typing and editing your program, you can edit faster by using these tricks.Ö

To delete a whole line, move to that line. Then tap the Y key (which means "Yank it out") while holding down the CONTROL key.

To insert an extra line in the middle of your program, move to where you want the extra line to begin. Press the ENTER key, then the up-arrow.

Hereís how to delete or move a block of text.Ö

Turbo PASCAL 6 and Quick PASCAL Put the cursor at the blockís beginning. Hold down the SHIFT key while moving to the blockís end, so the entire block changes color.

Then say what you want to do to the block. If you want to delete the block, press the DELETE key for Quick PASCAL; press CONTROL with DELETE for Turbo PASCAL 6. If you want to move the block instead, press SHIFT with DELETE, then point where you want the block to appear and press SHIFT with INSERT.

Turbo PASCAL 4 and 5.5 Put the cursor at the blockís beginning. Press CONTROL with K (which means "kommand"), then press B (which means "block").

Point at the character after the blockís end. Press CONTROL with K, then press K (which means "klose"). The entire block changes color.

Then say what you want to do to the block. If you want to delete the block, press CONTROL K then Y ("which means "yank"). If you want to move the block instead, point where you want the block to appear and press CONTROL K then V (which means "visible").

Save your program

Hereís how to copy your program onto the hard disk.

Quick PASCAL Press the Alt key. Say "File Save" by typing FS. If the computer says "File Name", type a name for the program (such as WINE) and press ENTER.

Turbo PASCAL 4 and 5.5 Press the F2 key. If the computer says "Rename", type a name for the program (such as WINE) and press ENTER.

Turbo PASCAL 6 Press the F2 key. If the computer says "Save file as", type a name for the program (such as WINE) and press ENTER.

Switch programs

Hereís how to switch to other programs.

Quick PASCAL Press CONTROL with F4. If the computer asks "Do you want to save?", press N. The screen becomes blank. Press the Alt key.

If you want to invent a new program, say "File New" (by typing FN). If instead you want to retrieve a saved program, do this: say "File Open" (by typing FO), type the programís name (such as WINE), and press ENTER.

Turbo PASCAL 4 and 5.5 If you want to invent a new program, press F10 then type FN (which means File New). If instead you want to retrieve a saved program, do this: press F3 and ENTER; point at the program you want to use (by using the arrow keys and PAGE DOWN key) and press ENTER.

If the computer asks "Save?", press N.

Turbo PASCAL 6 Press Alt with F3. If the computer asks "Save?", press N.

If you want to invent a new program, press F10 then type FN (which means File New). If instead you want to retrieve a saved program, do this: press F3 and ENTER; point at the program you want to use (by using the arrow keys) and press ENTER.

Multiple writing

This program belongs in your bathroom:

BEGIN;

WRITELN('FAR','TIN','G');

END.

The computer will write FAR, TIN, and G all on the same line, like this:

FARTING

Exit

When you finish using PASCAL, hereís how to return to the DOS prompt.

Quick PASCAL Press the Alt key. Say "File eXit" (by typing FX). If the computer asks "Do you want to save?", press N.

If youíre using a fancy color monitor (such as VGA), youíll discover that Quick PASCAL has changed the monitorís colors. To reset the colors, turn the computer off and then back on.

Turbo PASCAL Press Alt with X. If the computer asks "Save?", press N.

EXE files

The computer canít really understand PASCAL. Whenever you tell the computer to run a PASCAL program, the computer secretly creates a second version of your program. The second version is written in machine language instead of PASCAL. (Machine language is the only language the computer really understands.) Then the computer runs the machine-language version of your program.

The program you typed (in PASCAL) is called the source code. The program the computer runs (in machine language) is called the object code.

When the computer is translating your program from PASCAL to machine language (from source code to object code), the computer is said to be compiling your program. Since Quick PASCAL and Turbo PASCAL make the computer compile your PASCAL programs, Quick PASCAL and Turbo PASCAL are called PASCAL compilers.

Example Suppose you type a PASCAL program, then tell the computer to save it on the hard disk and call it WINE. The computer will copy it to your hard diskís PASCAL subdirectory, where it will be called "WINE.PAS". The ".PAS" means "source code written in PASCAL".

When you tell the computer to run the WINE.PAS program, the computer translates it to machine language. The machine-language version (the object code) is called "WINE.EXE". The ".EXE" means "executable object code written in machine language".

If youíre using Quick PASCAL, the computer automatically puts WINE.EXE in your diskís PASCAL subdirectory. If youíre using Turbo PASCAL instead, the computer puts WINE.EXE in the RAM but not on disk ó unless you give a special "Destination Disk" command.

After the computer has run your program and put WINE.EXE on disk, you can run the WINE.EXE machine-language program without using PASCAL. Hereís how: quit PASCAL, then do a "cd" to the PASCAL subdirectory, then type "wine".

Experiment Try the following experiment.Ö

Step 1: type a PASCAL program.

Step 2: tell the computer to save the program and call it WINE. The computer will call your program WINE.PAS and copy it to PASCALís subdirectory. (If youíre using Turbo PASCAL, the computer will rename a previous WINE.PAS to WINE.BAK.)

Step 2Ĺ: if youíre using Turbo PASCAL, say "Destination Disk". Hereís how. Press F10 then C. If you see "Destination Memory" instead of "Destination Disk", press D. Then return to the editor (by pressing F6 for version 4, Esc for version 5.5).

Step 3: run the program. The computer will invent WINE.EXE (a machine-language version of WINE) and put it in PASCALís subdirectory. Then the computer will run that machine-language program. (If youíre using Quick PASCAL, the computer will also produce a WINE.QDB to help the Quick DeBugger.)

Step 4: exit from PASCAL, so you see the DOS prompt.

Step 5: notice that you can run WINE.EXE without using PASCAL. Specifically, go into PASCALís subdirectory (by typing "cd \qp" for Quick PASCAL, or "cd \turbo" for Turbo PASCAL), then type "wine" (which runs the WINE.EXE program).

Math

PASCAL distinguishes between integers and real numbers. Hereís how PASCAL defines them.Ö

Integers

An integer contains no decimal point and no exponent.

Integer Not an integer Comment

-27 -27.0 An integer contains no decimal point.

50000 5E4 An integer contains no exponent.

Real numbers

A real number contains a decimal point or the letter E. For example, it can be 0.37 or 5.0 or 6E24. If it contains a decimal point, you should put digits before and after the decimal point.

Correct Incorrect

0.37 .37

5.0 5.

Other versions Old versions of PASCAL require you to put a decimal point in every real number; so instead of saying 6E24, you must say 6.0E24.

Arithmetic

Like BASIC, PASCAL lets you do arithmetic by using the symbols +, -, *, and /.

If you combine real numbers, the answer is real:

4.9+2.1 is the real number 7.0 (not 7)

5.0-5.0 is the real number 0.0 (not 0)

If you combine a real with an integer, the answer is still real:

3.5*2 is the real number 7.0 (not 7)

3/.5 is the real number 6.0 (not 6)

If you combine integers, the answer is an integer, except for division:

7+4 is the integer 11

7-4 is the integer 3

7*4 is the integer 28

7/4 is the real number 1.75

This program makes the computer do all that arithmetic:

BEGIN;

WRITELN(7+4);

WRITELN(7-4);

WRITELN(7*4);

WRITELN(7/4);

END.

The computer will write:

11

3

28

1.7500000000E+00

(Quick PASCAL writes extra zeros, like this: 1.75000000000000E+0000.)

Letís try writing all those numbers on the same line:

BEGIN;

WRITELN(7+4,7-4,7*4,7/4);

END.

The computer will write:

11328 1.7500000000E+00

Like BASIC, PASCAL lets you use parentheses to indicate order of operations.

Other versions Some versions of PASCAL automatically write blank spaces in front of integers.

Some versions of PASCAL write 1.75 instead of 1.7500000000E+00. Some Apple 2 and Mac versions automatically round the answer and write 1.8e+0.

Functions

PASCAL lets you use these functions:

Function Meaning

ABS(-6) the absolute value of -6; itís 6

SQR(3) the square of 3; itís 9

SQRT(9) the square root of 9; itís 3.0

SIN(2) the sine of 2 radians

COS(2) the cosine of 2 radians

ARCTAN(2) the arctangent of 2, in radians

EXP(5) e5, where e is 2.71828182845904523536

LN(9) the natural logarithm of 9; itís loge 9

The number in parentheses can be either an integer or a real. The computerís answer is usually a real (exception: for ABS and SQR, the computerís answer is an integer if the number in parentheses is an integer).

For example, this program computes the square root of 9:

BEGIN;

WRITELN(SQRT(9));

END.

The computer will write:

3.0000000000E+00

Donít confuse SQR with SQRT. The SQR means square; the SQRT means square root. To find the square of 3, say SQR(3) or 3*3. In BASIC, you could say 3^2; but in PASCAL, you canít use the symbol ^; use SQR instead.

To turn a real number into an integer, use these functions:

Function Meaning

ROUND(3.9) 3.9 rounded to the nearest integer; itís 4

TRUNC(3.9) 3.9 truncated (by deleting the .9); itís 3

Other versions For UCSD PASCAL, say ATAN instead of ARCTAN.

If youíre using UCSD PASCAL on an Apple and want to use the functions SQRT, SIN, COS, ATAN, EXP, and LN, you must insert this line immediately under the line that says PROGRAM:

USES TRANSCEND;

Simple variables

You can use variables:

Program Meaning

VAR Here are the variables.Ö

FRED,MARTHA: INTEGER; FRED & MARTHA integers;

JILL,TOM: REAL; JILL & TOM are reals.

BEGIN; Now letís begin the program.

FRED:=2; FRED (an integer) is 2.

MARTHA:=4+5; MARTHA (an integer) is 9.

JILL:=9.2; JILL (a real) is 9.2.

TOM:=1.4+2.3; TOM (a real) is 3.7.

WRITELN(FRED*MARTHA,JILL+TOM); Write 18 and 1.2900000000E+01.

The top line says VAR. The word VAR doesnít have a semicolon after it; the lines underneath VAR are indented. The indentation is optional but a good habit. To indent easily, tap the TAB key.

For Quick PASCAL, the TAB key indents 8 spaces. For Turbo PASCAL, the TAB key indents just enough to get past the word above (VAR).

On the indented lines, say which variables are integers and which are reals.

Once youíve indented a line, the computer automatically indents all the lines underneath it. If the computer automatically indents a line that you donít want to indent, hereís how to undo the indentation: for Turbo PASCAL 5.5, press the BACKSPACE key once; for Turbo PASCAL 4, press the left-arrow key several times; for Quick PASCAL, tap the TAB key while holding down the SHIFT key.

In PASCAL, a variableís name can be as long as you like: the name can be FRED or WASHINGTON or even SUPERCALIFRAGILISTICEXPIALIDOCIOUS. The name can consists of letters and digits, but must begin with a letter.

In PASCAL, you must put a colon in front of the equal sign.Ö

Correct Incorrect

FRED:=2; FRED=2;

If you want to add a line saying PROGRAM WINE, put that line above the VAR line.

You canít set an integer variable equal to a real. For example, if you say ó

VAR

ANN: REAL;

BILL: INTEGER;

BEGIN;

ANN:=3.9;

you cannot then say:

BILL:=ANN;

Instead, you must say ó

BILL:=ROUND(ANN);

or

BILL:=TRUNC(ANN);

Other versions Some versions of PASCAL do not automatically indent the lines for you.

Old versions of PASCAL examine just the first 8 characters of a variableís name and ignore the rest of the name.

READLN

Instead of saying INPUT, PASCAL says READLN.

VAR

X: REAL;

BEGIN;

WRITELN('WHAT IS YOUR FAVORITE NUMBER?');

READLN(X);

WRITELN('ITS SQUARE ROOT IS',SQRT(X);

END.

When you run the program, the computer asks:

WHAT IS YOUR FAVORITE NUMBER?

Then the READLN(X) statement makes the computer wait for you to input a value of X. The computer expects you to input a real number; if you input an integer instead, the computer will automatically turn it into a real. For example, if you input 9, the computer will automatically turn it into 9.0 and will write:

ITS SQUARE ROOT IS 3.0000000000E+00

Other versions For old versions of PASCAL, say READ instead of READLN.

 

IF

The computer can criticize your age:

VAR

AGE: INTEGER;

BEGIN;

WRITELN('HOW OLD ARE YOU?');

READLN(AGE);

IF AGE<18 THEN

BEGIN;

WRITELN('YOU ARE STILL A MINOR.');

WRITELN('AH, THE JOYS OF YOUTH!');

END;

WRITELN('GLAD TO MEET YOU.');

END.

That program makes the computer ask:

HOW OLD ARE YOU?

If you input a number less than 18, the computer replies:

YOU ARE STILL A MINOR.

AH, THE JOYS OF YOUTH!

GLAD TO MEET YOU.

If you input a number thatís at least 18, the computer says just this:

GLAD TO MEET YOU.

In that program, the line that says "IF" is a heading (the lines underneath it are indented), so do not put a semicolon at the end of that line!

ELSE

To make your program fancier, insert the shaded lines:

VAR

AGE: INTEGER;

BEGIN;

WRITELN('HOW OLD ARE YOU');

READ(AGE);

IF AGE<18 THEN

BEGIN;

WRITELN('YOU ARE STILL A MINOR.');

WRITELN('AH, THE JOYS OF YOUTH!');

END

ELSE

BEGIN;

WRITELN('I AM GLAD TO HEAR YOU ARE AN ADULT!');

WRITELN('NOW WE CAN HAVE SOME ADULT FUN!');

END;

WRITELN('GLAD TO MEET YOU.');

END.

If your age is less than 18, the computer will say:

YOU ARE STILL A MINOR.

AH, THE JOYS OF YOUTH!

GLAD TO MEET YOU.

If your age is not less than 18, the computer will say:

GLAD TO HEAR YOU ARE AN ADULT!

NOW WE CAN HAVE SOME ADULT FUN!

GLAD TO MEET YOU.

In that program, the line that says "ELSE" is a heading, so do not put a semicolon after the "ELSE". Immediately above the word ELSE, youíll see the word END; do not put a semicolon after that END.

Symbols

Like BASIC, PASCAL uses these symbols in the IF line:

< > = <= >= <> AND OR

In the IF line, the symbol for "equals" is "="; outside the IF line, the symbol for "equals" is ":=".

Instead of saying ó

IF I=3 OR I=8 OR I=25 OR I=95 THEN

you can say:

IF I IN [3,8,25,95] THEN

That means "If I is in this set of numbers ó 3,8,25,95 ó thenÖ"

Loops

To create a loop, say FOR or REPEAT or WHILE. Hereís how to use those words.

FOR

Like BASIC, PASCAL uses the word FOR. This program makes the computer get drunk:

VAR

I: INTEGER;

BEGIN;

FOR I := 7 TO 10 DO

BEGIN;

WRITELN('I DRANK ',I,' BOTTLES OF BEER');

WRITELN('HOORAY!');

END;

WRITELN('NOW I AM DEAD DRUNK');

END.

Since the FOR line is a heading, do not put a semicolon at the end of it. When you run the program, the computer will write:

I DRANK 7 BOTTLES OF BEER

HOORAY!

I DRANK 8 BOTTLES OF BEER

HOORAY!

I DRANK 9 BOTTLES OF BEER

HOORAY!

I DRANK 10 BOTTLES OF BEER

HOORAY!

NOW I AM DEAD DRUNK

If you want the computer to count backwards ó from 10 down to 7 ó change the FOR line to this:

FOR I := 10 DOWNTO 7 DO

REPEAT

This program plays a guessing game:

VAR

GUESS: INTEGER;

BEGIN;

WRITELN('I AM THINKING OF A NUMBER FROM 1 TO 10');

REPEAT

WRITELN('YOU HAVE NOT GUESSED MY NUMBER YET');

WRITELN('WHAT IS MY NUMBER?');

READLN(GUESS);

UNTIL GUESS=6;

WRITELN('CONGRATULATIONS! YOU GUESSED IT! MY NUMBER IS 6');

END.

The computer begins the game by saying:

I AM THINKING OF A NUMBER FROM 1 TO 10

Then the computer REPEATs the following procedure several times: it says YOU HAVE NOT GUESSED MY NUMBER YET, asks WHAT IS MY NUMBER, waits for the human to input a guess, and checks whether the human guessed 6. It repeats that procedure again and again, UNTIL the human finally guesses 6. Then the computer says:

CONGRATULATIONS! YOU GUESSED IT! MY NUMBER IS 6

In that program, the computer REPEATs the indented lines, UNTIL GUESS is 6.

If a program contains the word REPEAT, it must also contain the word UNTIL. The lines between REPEAT and UNTIL are done repeatedly. After each time, the computer checks whether to repeat again.

Since the REPEAT lineís a heading, it has no semicolon.

Like an IF line, the UNTIL line can contain these symbols:

< > = <= >= <> AND OR IN

Hereís another example. Letís make the computer start with the number 3 and keep doubling it, like this:

3

6

12

24

etc.

Letís make the computer keep doubling but not go over 1000. So altogether, letís make the computer write:

3

6

12

24

48

96

192

384

768

THOSE ARE ALL THE NUMBERS BELOW 1000

Hereís the program:

VAR

I: INTEGER;

BEGIN;

I:=3; Start I at 3.

REPEAT Repeat the indented lines until I>=1000.

WRITELN(I);

I:=I*2;

UNTIL I>=1000;

WRITELN('THOSE ARE ALL THE NUMBERS BELOW 1000');

END.

WHILE

The word WHILE resembles the word REPEAT. For example, the previous program (which doubled 3 repeatedly) can be rewritten to use WHILE:

VAR

I: INTEGER;

BEGIN;

I:=3; Start I at 3.

WHILE I<1000 DO If I<1000, do the indented lines, and repeat them.

BEGIN; again and again, as long as I remains below 1000.

WRITELN(I);

I:=I*2;

END;

WRITELN('THOSE ARE ALL THE NUMBERS BELOW 1000');

END.

The indented lines say to WRITELN(I) and then double I. The computer repeats those indented lines many times. Before each repetition, the computer checks to make sure I is still below 1000. When I passes 1000, the computer stops looping and proceeds to the next line, which writes THOSE ARE ALL THE NUMBERS BELOW 1000.

These two structures resemble each other:

Using REPEAT Using WHILE

REPEAT WHILE I<1000 DO

etc. BEGIN;

UNTIL I>=1000; etc.

END;

Each of those structures makes the computer repeat the indented lines and check whether I is still below 1000. If you say REPEAT, the check is done at the loopís bottom, so the computer goes through the loop once before checking. If you say WHILE, the check is done at the loopís top, so the computer checks whether I<1000 before doing the loop the first time.

Logic tricks

Every statement must end with a semicolon. You can put several statements on the same line:

Normal Alternative

A:=3; A:=3; B:=7.6; WRITELN(A+B);

B:=7.6;

WRITELN(A+B);

You can continue a statement on the next line:

Normal Alternative

A:=5-2+1; A:=

5-2

+1;

But do not divide a statement in the middle of a word, number, symbol, or string:

Wrong, because Wrong, because Wrong, because

Normal Okay middle of word middle of number middle of symbol

IF AGE>=18 THEN IF AGE IF AG IF AGE>=1 IF AG>

>=18 THEN E>=18 THEN 8 THEN =18 THEN

Omitting semicolons

You can omit the semicolon after BEGIN:

Normal Alternative

BEGIN; BEGIN

You can omit a semicolon, if the next word is END:

Normal Alternative

A:=3; A:=3

END; END;

You can omit a semicolon, if the next word is UNTIL:

Normal Alternative

I=*2; I=I*2

UNTIL I>=1000; UNTIL I>=1000;

Omitting BEGIN and END

If a heading heads just one indented line (besides BEGIN and END), you can put that indented line next to the heading (and omit the BEGIN and END).

Long way Short cut

FOR I := 1 TO 9 DO FOR I := 1 TO 9 DO WRITELN(I);

BEGIN;

WRITELN(I);

END;

WHILE A<100 DO WHILE A<100 DO A:=A+7;

BEGIN;

A=A+7;

END;

IF AGE>=65 THEN IF AGE>=65 THEN WRITELN('OLD');

BEGIN;

WRITELN('OLD');

END;

IF WEIGHT>220 THEN IF WEIGHT>220 THEN WRITELN('FAT') ELSE WRITELN('OKAY');

BEGIN;

WRITELN('FAT');

END

ELSE

BEGIN;

WRITELN('OKAY');

END;

REPEAT REPEAT A:=A+7 UNTIL A>100;

A:=A+7;

UNTIL A>100;

Comments

To put comments into your program, surround the comment by braces:

BASIC PASCAL

10 'I HATE COMPUTERS {I HATE COMPUTERS}

Do not put a semicolon after the comment.

If your screen can display colors, Quick PASCAL makes the comments and braces turn green.

Other versions If your keyboard is old and lacks braces, use parentheses and asterisks instead:

(*I HATE COMPUTERS*)

GOTO

You can say GOTO:

LABEL 10;

BEGIN;

WRITELN('MY DOG');

GOTO 10;

WRITELN('NEVER');

10: WRITELN('DRINKS WHISKEY');

END.

The top line warns the computer that a future line will be labeled "line 10". The rest of the program makes the computer write MY DOG, then skip to line 10, which makes the computer write DRINKS WHISKEY. Altogether, the computer will write:

MY DOG

DRINKS WHISKEY

Do not put a space between GO and TO.

Hereís another example:

LABEL 10;

BEGIN;

10: WRITELN('LOVE');

WRITELN('HATE');

GOTO 10;

END.

The computer will write LOVE and HATE repeatedly:

LOVE

HATE

LOVE

HATE

LOVE

HATE

etc.

To abort the program, tap the C key while holding down the CONTROL key. (For Quick PASCAL, then press ENTER.)

If your program contains lines numbered 10, 20, and 100, put this statement at the top of your program:

LABEL 10,20,100;

Put that statement at the very top of your program: the only statement that should go above it is the one saying PROGRAM. The PROGRAM and LABEL lines go above all other lines ó even above VAR and BEGIN.

Each line number must be small: no higher than 9999.

In PASCAL, youíll rarely need to say GOTO. Instead, try using the words IF, ELSE, FOR, REPEAT, and WHILE.

Other versions In UCSD PASCAL, if you want to say GOTO, you must say (*$G+*) at the top of your program:

(*$G+*)

PROGRAM SKIPPER;

LABEL 10;

etc.

Procedures

This program teases you, with the help of some insults:

PROCEDURE INSULT;

BEGIN;

WRITELN('YOU ARE STUPID');

WRITELN('YOU ARE UGLY');

END;

{MAIN ROUTINE}

BEGIN;

WRITELN('WE ALL KNOW...');

INSULT;

WRITELN('...AND YET WE LOVE YOU');

END.

That program begins by defining INSULT to be this procedure:

WRITELN('YOU ARE STUPID');

WRITELN('YOU ARE UGLY');

The main routine makes the computer write ĎWE ALL KNOW...í, then do the INSULT procedure, then write Ď...AND YET WE LOVE YOUí; altogether, the computer will write:

WE ALL KNOW...

YOU ARE STUPID

YOU ARE UGLY

...AND YET WE LOVE YOU

The PROCEDURE, called INSULT, is a subroutine. In PASCAL, subroutines come before the main routine. (In BASIC, subroutines come after the main routine instead.)

Put a programís VAR above any procedures:

VAR The program uses a variable,

I: INTEGER; called I, which is an integer.

PROCEDURE DOUBLE; The subroutine is called DOUBLE.

BEGIN;

I:=2*I; The subroutine doubles the value of I

WRITELN(I); and writes the new value of I.

END;

{MAIN ROUTINE} Hereís the main routine.Ö

BEGIN;

I:=7; I starts at 7.

DOUBLE; Do subroutine DOUBLE, which writes 14.

DOUBLE; Do DOUBLE again, which writes 28.

WRITELN(I+1); Write 29.

END.

Advanced variables

Youíve seen that a variable can stand for a number. This section explains how to make a variable stand for a character, array, or string.

CHAR

A variable can stand for a character:

VAR

ANN,JOAN: CHAR; ANN and JOAN will each be a character.

BEGIN;

ANN:='U'; ANN is the character ĎUí.

JOAN:='P'; JOAN is the character ĎPí.

WRITELN(ANN,JOAN); The computer will write ĎUí and ĎPí.

END.

The computer will write U and P like this:

UP

You can put a character variable after the word FOR:

VAR

I: CHAR;

BEGIN;

FOR I := 'A' TO 'E' DO WRITELN(I);

END.

The computer will write:

A

B

C

D

E

ARRAY

Letís make X be this list of real numbers: 4.2, 71.6, 8.3, 92.6, 403.7, 1.4. Hereís how:

VAR

X: ARRAY [1..6] OF REAL;

BEGIN;

X[1]:=4.2;

X[2]:=71.6;

X[3]:=8.3;

X[4]:=92.6;

X[5]:=403.7;

X[6]:=1.4;

WRITELN(X[1]+X[2]+X[3]+X[4]+X[5]+X[6]);

END.

The computer will write the sum, 581.8.

Subscripts can be negative:

VAR

Y: ARRAY [-2..3] OF REAL;

BEGIN;

Y[-2]:=400.1;

Y[-1]:=274.1;

Y[0]:=9.2;

Y[1]:=8.04;

Y[2]:=0.6;

Y[3]:=-5.0;

WRITELN(Y[-2]+Y[-1]+Y[0]+Y[1]+Y[2]+Y[3]);

END.

The computer will write the sum, 687.04.

Hereís how to make Z be a table having 6 rows and 4 columns of reals:

VAR

Z: ARRAY [1..6, 1..4] OF REAL;

The number in the 3rd row and 2nd column is called Z[3,2]. The entire first row of Z is called Z[1]; the second row is called Z[2]; etc. For example, if you say ó

Z[5]:=Z[3];

the computer will look at the real numbers in the 3rd row of Z, and copy them into the 5th row. Suppose W is another array that has 6 rows and 4 columns; if you say ó

W:=Z;

the computer will look at each real number in Z and copy it into W.

You can have many kinds of arrays: you can have an array of REAL, an array of INTEGER, and even an array of CHAR.

STRING

A variable can stand for a string:

VAR

X: STRING;

BEGIN;

X:='I LOVE MY MOTH';

WRITELN(X,'ER');

END.

The computer will write `I LOVE MY MOTHí and then `ERí, like this:

I LOVE MY MOTHER

You can make the computer read a string:

VAR

X: STRING;

BEGIN;

WRITELN('WHAT IS YOUR NAME?');

READLN(X);

WRITELN('HELLO ',X,' THE BEAUTIFUL');

END.

When you run that program, the computer asks:

WHAT IS YOUR NAME?

Then the READLN(X) statement makes the computer wait for you to input a string. If you input the name MARILYN MONROE, the computer will write:

HELLO MARILYN MONROE THE BEAUTIFUL

Other versions Instead of letting you say STRING, some versions of PASCAL require you to say STRING[80].

The oldest versions of PASCAL require you to say PACKED ARRAY [1..80] OF CHAR instead. Some of those old versions canít read or write the whole array string at once: instead you must create FOR loops that read and write one character at a time.