• Keine Ergebnisse gefunden

The Statement Part

Im Dokument Turbo Pascal® Tutor (Seite 96-101)

The remainder of the program is enclosed by the reserved words begin and end. This part of the program consists of one or more statements and is therefore called the statement part. The computer starts with the first

statement in the statement part and continues to execute the statements, in top to bottom order, until it reaches the final end:

beqin { Main body of program Simple }

( Statements go here )

end. { of program Simple }

Adjoining statements are separated by semicolons. Since the last statement is not followed by another statement (end isn't a statement), no semicolon is needed-although having one doesn't hurt. (This is one of the few instances in which you'll get a break from Pascal syntax.) There is a period after the final end in the program. This lets the compiler know that the program is finished.

The first statement in the sample program is a WriteLn statement:

{ Start by greeting the user. As in our very first program, we use a WriteLn ("Write Line") statement to write a line to the screen. }

WriteLn (' Hello, " YourName, '.');

{ The WriteLn statement can take a LIST of things to write on a line, as well as just one thing. We wrote three things: the constant string 'Hello, " the value of the constant identifier YourName (another string), and a period (a character constant). }

As mentioned in the comment, the WriteLn statement takes a list of variables, constants, or expressions and writes their value(s) to the screen.

(It is also possible to have a WriteLn statement without a list of things to write, although we didn't show this in our program. The statement WriteLn; simply outputs a blank line to the screen.)

The program then prompts (that is, asks) for an Integer value to place in the variableA.

{ Then write a string to the terminal asking the user for an Integer. A message like this, which requests a response of some kind, is often called a "prompt."

WriteLn('Please type an Integer, followed by a Return.');

{ Wait for the user to type a number, then place that number in the variable A.

ReadLn, which is read as "Read Line," tells the computer to wait for the Return key to be pressed (starting a new line) before assuming that the number is complete. }

ReadLn (A);

The Basics of Pascal 75

The ReadLn statement does the work of getting the Integer we asked for from the keyboard. Like WriteLn, ReadLn can take a list of values to get from the terminal or can be used with no list at all. The statement

ReadLn;

just waits for the user to press Return, as we saw in our first program.

Our program then does a second WriteLn and a second ReadLn to get another number and adds the first number to the second:

Repeat the two steps for a second number.

Prompt for another number}

WriteLn('Now please type another Integer, followed by a Return.');

ReadLn(B); { Read the number and place it in variable B. } C := A + B; { Add A and B and place the result in variable C. }

The last statement of the preceding group is an assignment. The expression A + B is evaluated by adding the values contained in variables A and B together. The result of this operation is then placed in variable C.

It helps to think of the assignment operator, :=, as an arrow pointing to the left, indicating the flow of information from the expression on the right-hand side to the variable on the left-right-hand side. When a program is read aloud, the assignment operator can be read as "gets," as in

"e

gets A plus B."

Now our sample program displays the value saved a moment ago in variable C:

Write a line containing a message and the value of the variable C.

WriteLn('The sum of the two Integers is: " C)

{ Putting an identifier (here, C) in the list of things that a WriteLn statement is to write causes its VALUE to be written, rather than its name. If we wanted to print the letter "C," we would enclose it in single quotes--as we did with the period in the first WriteLn statement. }

Finally, the program reaches the final end and stops.

Throughout the sample program, everything has been arranged in an orderly fashion. Since the compiler doesn't care about spacing between words (unless, of course, the spaces are within a quoted string of characters), we have spaced and aligned everything for clarity.

If you'd like to actually execute this program, type it in and run it under Turbo Pascal (feel free to expunge the comments). Alternatively, the source code of every example program in this book in contained in file Manual.Pas

on the Turbo Tutor disk. Simply open this file under Turbo Pascal, locate the program Simple (do a Find operation on the string Simple;), and copy it into a new window.

We've covered a lot of ground in this chapter. So, before you move on, take the time to perform the following exercises. They are designed to reinforce what you have learned so far and prepare you for the material in chapters to come (answers in Appendix B).

Exercises

1. Review the previous sample program. How many identifiers can you find? (Hint: The names of data types, like Integer, are identifiers). How many constants? How many statements?

2. Use Turbo Pascal to load and run this program in its original form.

Change the value of the constant YourName so that the computer writes your name when the program is run (don't cheat by typing in the name of one of the 25 bones in the human foot).

3. Modify the sample program so that it prints not only the sum of the two numbers (A + B), but the difference (A - B) as well. Add a new variable, D, to hold the difference.

4. Modify the program to return the following values:

a. Twice the difference between A and B b. A minus twice B

c. Five times A, minus the quantity three times B d. The product of A and B

e. A modulo B (Watch out if you enter a value of 0 for BD

Check your work by accumulating results for several values of A and B.

5. Try typing a number with a decimal point when asked for one by the program. What happens? Can you explain why?

6. Now, change the variables in the program so that they are all of the type Real. Repeat Exercise 5 with this new program. Can you explain the results? (The compiler will complain if you try Exercise 4(e) with the variables A and B as Real numbers; the mod operator only works on integers.)

The Basics of Pascal 77

Review

Each of the topics presented in this chapter deserves (and gets) more explanation. Our objective here has been to define some of the basic concepts of Pascal, and you should have at least a basic understanding of the terms data type, identifier, reserved word, operator, constant, variable, expression, statement, and comment. If you feel comfortable using these terms, you're ready to go on to the next chapter.

c

H A P T E R

7

Im Dokument Turbo Pascal® Tutor (Seite 96-101)