• Keine Ergebnisse gefunden

A PROGRAMMER'S GUIDE TO TURBO PASCAL

7. THE BASICS OF PASCAL

7.1 A QUICK EXAMPLE

If you've gotten this far (and you obviously have, or else you wouldn't be reading this), then you should know the fundamentals of how to use TURBO Pascal. (That is, you should know how to use the parts of the Main Menu; I don't yet expect that you can write a program.) If my assumption is not correct, go back through Part I again. Of course, you won't really need that for the following example, since it's just a penciland-paper exercise anyway, but you will need it before you go too much farther into Part II.

Having gotten that out of the way, let's take a simple BASIC program and convert it into a simple Pascal program. Suppose our BASIC program looks like this:

100 REM SIMPLE BASIC PBDGRAM 110 INPUTA

120 INPUTB 130 C=A+B 140 PRINTC 150 STOP

160 REM END OF SIMPLE BASIC PROGRAM

This program allows you to enter two numbers, which are stored in variables A and B. These two values are then added together and stored in C. The contents of C are then printed out so that you can see the sum of the two numbers that you entered. So far, so good. Now comes the fun part: converting this innocuous little piece of code into a real, live Pascal program. You're going to do it a step at a time, so that you can understand what you're doing and why. As usual, the explanation is more complicated than the actual process.

The first step is to remove all the line numbers, since you don't need them. Even if you wanted them, you would still remove them, since Pascal uses other means of getting around than jumping to a given line. Your program now looks like this:

REM SIMPLE BABIC PROGRAM INPUT A

INPUTB C=A+B PRINTC

STOP

REM END OF SIMPLE BABIC PROGRAM

Next, you must change the names of the input/output (I/O) commands. Instead of INPUT, you will usethe Pascal procedure ReadLn, andyou will replace PRINTwith the procedure WriteLn.

Now you have:

REM SIMPLE BABIC PROGRAM ReadLnCA)

ReadLnCB) C=A+B WriteLnCC)

STOP

REM END OF SIMPLE BASIC PROGRAM

Next, three more changes. First, you will replace the equals sign (=) in the fourth line with a colon followed by an equal sign (:=).

Pascal uses := for assigning values and reserves the plain = for comparing values.

Your second change is to put a semicolon (;) between all executable statements. Since a Pascal statement can (and often does) occupy several lines, the semicolon marks where one statement stops and the next one begins. For neatness' sake, you'll placethesemicolon attheend of the preceding statement.

Lastly, you will eliminate the command STOP, which is not needed or used in Pascal (and isn't really needed in your BASIC version here).

Your program has changed a little more:

REM SIMPLE BABIC PROGRAM ReadLnCA);

ReadLnCB);

C:=A+B;

WriteLn(C)

REM END OF SIMPLE BABIC PROGRAM

Now for a major change. In BASIC, you can start execution on any line, selectively execute groups of lines currently in memory, and so on. In contrast, a PASCAL program is a well-defined group of instructions, with what is known as the main body.

Execution always begins at the start of the main body and proceeds through to the end. So'you need to tell Pascal that your, program is a program and then place your instructions in the main body. After you do this, it will look like this:

program Simple;

begin

REM SIMPLE BABIC PROGRAM Readln(A);

ReadLn(B);

C:=A+B;

WriteLn(C)

REM end OF SIMPLE BABIC PROGRAM end

On tothe next item. Firstsomebackground: Pascal was designed as a teaching language and, like most teachers, will pick nits (but only for your own good, mind you). Case in point: BASIC allows you to create variables as you go along, while Pascal demands

that all (and I do mean ALL) variables be declared before they can be used. So let's declare our variables:

program simple;

var

A,B,C : Integer;

begin

REM SIMPLE BASIC PROGRAM ReadLn(A);

ReadLn(B);

C:=A+B;

WriteLn(C)

REM end OF SIMPLE BASIC PROGRAM end

Only one thing remains to make your simple BASIC program into a simple Pascal program: change the comment statements. In BASIC, each comment line starts with REM (for REMINDER). In Pascal, a comment starts with the character { and ends with the character }, and you can have as many lines of comments between those two characters as you want. If your terminal doesn't have {and}, or if you just don't like curly braces, then you can use the sequences (* and *) instead. You will now add a few comments to ensure that your program is well documented. And here's your final program:

program Simple;

{

A simple Pascal program converted from BASIC.

DATE: 17 June 1985 AUTHOR: Put your name here var

A,B : Integer; { input variables } C : Integer; { output variable } begin { main body of program Simple

ReadLn(A);

ReadLn(B);

C:=A+B;

WriteLn(C)

end { of program Simple

Here it is, an honest-to-goodness Pascal program, guaranteed to compile and run under TURBO Pascal! It really doesn't look all that different from your original BASIC program. And you thought Pascal was hard!

Here's a review of the steps you used to translate your BASIC program into Pascal:

1. Delete all the line numbers 2. Replace 1/0 statements 3. Replace assignment = with :=

4. Add semicolons at the end of statements 5. Drop STOP at the end of the program 6. Add program Name I begin ... end.

7. Declare all variables in the var section 8. Convert REM statements

It would be convenient if this set of rules was enough for all such translations. Unfortunately, this isfarfrom true. In fact, these rules will only translate a small subset of all BASIC programs.

Moreover, most BASIC programs over a certain size cannot be translated at all, only rewritten. However, your purpose here is notto show you howto convert BASIC programs into Pascal; it is to show you that you can learn how to write simple Pascal programs in just a few minutes.