home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Hall of Fame
/
HallofFameCDROM.cdr
/
prpascal
/
tptutor.lzh
/
TL04.TXT
< prev
next >
Wrap
Text File
|
1986-06-15
|
6KB
|
205 lines
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 16
TURBO-LESSON 4: DECLARATIONS, INPUT
OBJECTIVES - In Lesson 4 you will learn about:
1. The DECLARATIONS part of a program
2. VAR declaration
3. Input using the ReadLn statement
4. Integer variables
1. The DECLARATIONS part of a program.
You learned in the previous lesson that there are two main parts
to any PASCAL program: DECLARATIONS, and MAIN BODY. The various
entries in the DECLARATIONS section define the data items used in
the processing in the MAIN BODY. Not all declaration entries
will occur in every program, but only the ones needed to support
the processing.
The various types of declaration entries will be introduced as
needed in the sample programs. Only the VAR entry will be used
in this program.
2. VAR declaration.
All variables, (spelled A-L-L, no exceptions), must be defined
before they are referenced by processing statements. The VAR
entry is used to define variables. The form of the entry is:
variable-name : type;
The variable-name may be one or several variable-names separated
by commas. The type may be a predefined type, such as Integer,
or a type you have constructed useing the predefined types. The
colon must occur between the variable-name(s) and the type.
Extra spaces are acceptable to allow more readable format. Below
are some VAR entries:
VAR
i,j,k : Integer;
Inkey : Char;
Rate : Real;
Count : Integer;
(The example above includes types not yet discussed, to
illustrate the form of the VAR entry.)
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 17
##### DO:
Look at PROG4.
(You know from earlier lessons how to load the program and use
the editor to look at the program.) A segment of PROG4 is:
VAR
Number : Integer;
The VAR entry above defines a variable called "Number" to be of
type "Integer". This means the computer must set up a memory
location large enough to store an integer, which can be accessed
by referring to the name "Number".
Notice the variable, Number, is later referenced in the
processing statements.
##### DO:
Add an integer variable called "Age" to Prog4. This will be used
later in this lesson. You can either add the new variable to the
declaration of Number
Number, Age : Integer;
or add another declaration
Number : Integer;
Age : Integer;
Compile the program to be sure you haven't made a syntax error.
3. Input using the ReadLn statement.
ReadLn is the statement used to input variables. The form of the
statement is:
ReadLn(var_1, var_2, . . . ,var_n);
When the statement is executed, the computer will wait for you to
type values for the variables, separated by one or more spaces,
followed by depressing the enter key.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 18
Part of PROG4 follows:
BEGIN
Write('Enter a number (no decimals please): ');
ReadLn(Number);
WriteLn; { Display one blank line }
WriteLn('Number: ',Number); { Display the number entered }
END.
The Write statement provides a prompt before the ReadLn accepts
Number. Notice that ReadLn does not provide a "?" or any other
prompt. The programmer must provide any prompting require.
The use of the Write statement for prompting, instead of the
WriteLn, keeps the cursor on the same line so the input will
occur right after the prompt message.
The ReadLn accepts the number you type and stores it in the
memory location which the computer has set aside for the Integer
variable, Number.
##### DO:
Compile and run PROG4. When prompted, type 12 and enter. Run it
again and enter -34.
##### DO:
Using the statements of PROG4 as an example, expand the program
to do the following:
(1) Write a prompt to 'Enter your age'.
(2) Read a value for age to be stored in the integer variable,
Age, which you added to the VAR declarations in the previous
section.
(3) Write a message which prints the age entered, in a manner
similar to the way Number was printed.
Run the program.
4. Integer Variables.
Integers are counting numbers, with no decimal points. They
may be positive or negative. In TURBO the range of Integers
permitted is:
-32768 to +32767
Integers are used for subscripts, indexes, counting, input and
output of such things as counts, limits, menu choices. Decimal
numbers (Real type, discussed later), are needed for such things
as dollar amounts and calculations. When decimal numbers are
not actually needed, Integers should be used since they are
easier to use and take less memory.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 19
##### DO:
Run PROG4 again, this time entering data which will cause errors.
When prompted to enter a number, enter the letter A instead.
What happened?
A message appears:
I/O error 10, PC=287C
Program aborted
Searching
15 lines
Run-time error position found. Press <ESC>
What this means is as follows:
The program aborted because "I/O error 10" occurred at program
code address, 287C. (Your program may produce a different
program code address.) To find out what "I/O error 10" is, look
in your reference manual in the appendix containing "I/O ERROR
MESSAGES". Error 10 is an "Error in numeric format".
TURBO searched 15 lines before finding where the error occurred.
When you press the ESC key, the editor is activated with the
cursor positioned at the end of the statement which caused the
error. (In this case, it is not the statement which caused the
problem, but the type of data entered.)
##### DO:
Run the program again, entering too large a number, 88888. Note
that the same error occurs. Run it again with too small a
number, -55555. You may want to experiment with the end-points
of the acceptable integer range: -32768 to 32767. For example,
try entering the values -32767, -32768, -32769.
NOTE: THERE ARE WAYS TO HANDLE ERRORS TO AVOID ABORTING THE
PROGRAM. MORE ABOUT ERROR-HANDLING IN LATER LESSONS.
î