home *** CD-ROM | disk | FTP | other *** search
-
-
-
-
- Chapter 3
- SIMPLE DATA TYPES
-
-
-
- WHAT IS A DATA TYPE?
- ____________________________________________________________
-
- A type in Pascal, and in several other popular programming
- languages, defines a variable in such a way that it defines
- a range of values which the variable is capable of storing,
- and it also defines a set of operations that are permissible
- to be performed on variables of that type. TURBO Pascal has
- 5 basic data types which are predefined and can be used
- anywhere in a program provided you use them properly. This
- chapter is devoted to illustrating the use of these five data
- types by defining the allowable range of values that can be
- assigned to them, and by illustrating the operations that can
- be done to variables of these types. The five types and a
- very brief description follows;
-
- integer Whole numbers from -32768 to 32767
- byte The integers from 0 to 255
- real Floating point numbers from 1E-38 to 1E+38
- boolean Can only have the value TRUE or FALSE
- char Any character in the ASCII character set
-
- Please note that the byte type of data is not a part of the
- standard Pascal definition but is included as an extension to
- the TURBO Pascal compiler.
-
- TURBO Pascal versions 4.0 and 5.x have three additional types
- available of the integer class, which are not available with
- version 3.0. They are defined as follows;
-
- shortint The integers from -128 to 127
- word The integers from 0 to 65535
- longint The integers from -2147483648 to 2147483647
-
- In addition to the above data types TURBO Pascal version 4.0
- has the following data types available but in order to use
- them, you must have an 80X87 math coprocessor installed in
- your system;
-
- single Real type with 7 significant digits
- double Real type with 15 significant digits
- extended Real type with 19 significant digits
- comp The integers from about -10E18 to 10E18
-
- TURBO Pascal version 5.x has these four types available but
- because they have a software emulator for the floating point
- operations, an 80X87 math coprocessor is not required to use
- these with these versions. Of course, your resulting program
-
- Page 3-1
-
- Simple Data Types
-
- will run much faster if you have the coprocessor available for
- use by the program.
-
- A complete definition of the available types for each compiler
- can be found on pages 41 and 42 of the TURBO Pascal version
- 3.0 reference manual, and on pages 39 through 44 of the
- reference manual for version 4.0. They are defined on pages
- 41 to 46 of the TURBO Pascal 5.x User's guide. It would be
- good to read these pages now for a good definition prior to
- learning how to define and use them in a program. Note that
- all of these will be used in example programs in this chapter.
-
-
-
- OUR FIRST VARIABLES
- ____________________________________________________________
-
- The integers are by far the easiest to ================
- understand so we will start with a simple INTVAR.PAS
- program that uses some integers in a very ================
- simple way. Load INTVAR.PAS into your
- TURBO system and let's take a look at it.
-
- Immediately following the program statement is another
- reserved word, var. This reserved word is used to define a
- variable before it can be used anywhere in the program. There
- is an unbroken rule of Pascal that states "Nothing can be used
- until it is defined." The compiler will complain by
- indicating a compilation error if you try to use a variable
- without properly defining it. It seems a bit bothersome to
- have to define every variable prior to its use, but this rule
- will catch many spelling errors of variables before they cause
- trouble. Some other languages will simply define a new
- variable with the new name and go merrily on its way producing
- some well formatted garbage for you.
-
- Notice that there is only one var, but it is used to define
- three different variables, Count, X, and Y. Once a var is
- recognized, the compiler will continue to recognize variable
- definitions line after line until it finds another reserved
- word. It would be permissible to put a var on the second line
- also but it is not necessary. It would also be permissible
- to put all three variables on one line but your particular
- programming style will dictate where you put the three
- variables. Following the colon on each line is the word
- integer which is a standard identifier, and is therefore
- different from a reserved word. A standard identifier is
- predefined like a reserved word, but you can redefine it,
- thereby losing its original purpose and meaning. For now and
- for a long time, don't do that. Page 38 contains a list of
- standard identifiers in TURBO Pascal 3.0. There is no
- corresponding list in the reference manual for TURBO Pascal
- 4.0 or for TURBO Pascal 5.x.
-
-
- Page 3-2
-
- Simple Data Types
-
-
- OUR FIRST ARITHMETIC
- ____________________________________________________________
-
- Now that we have three variables defined as integer type
- variables, we are free to use them in a program in any way we
- desire as long as we use them properly. If we tried to assign
- a real value to X, the compiler will generate an error, and
- prevent a garbage output. Observe the start of the main body
- of the program. There are three statements assigning values
- to X, Y, and Count. A fine point of mathematics would state
- that Count is only equal to the value of X+Y until one of them
- was modified, therefore the equal sign used in so many other
- languages is not used here. The sign := is used, and can be
- read as "is replaced by the value of," when reading a listing.
- Another quicker way is to use the word "gets". Thus X := X
- + 1 would be read, "X gets the value of X plus 1". We will
- see later that the simple equal sign is reserved for use in
- a different manner.
-
- The first three statements give X the value of 12, Y the value
- of 13, and Count the value of 12 + 13 or 25. If we have a
- requirement to get those values out of the computer, we need
- another extension to the Writeln statement. The first part
- of the data within the parentheses should be very familiar to
- you now, but the second part is new.
-
- Multiple outputs can be handled within one Writeln if the
- fields are separated by a comma. To output a variable, simply
- write the variable's name in the output field. The number
- following the variable in each case is the number of output
- columns to be used by the output data. This number is
- optional and can be omitted allowing the system to use as many
- columns as it needs. For purposes of illustration, they have
- all been assigned different numbers of columns. At this
- point, you can compile and run INTVAR.PAS and examine its
- output.
-
- To illustrate the various ways to output =================
- data, load INTVAR2.PAS and observe that INTVAR2.PAS
- even though the output is identical, it is =================
- output in a completely different manner.
- Observe especially that a Writeln all by
- itself simply moves the cursor to the beginning of a new line
- on the video monitor. Compile and run this program and
- observe its output after you are certain that the two programs
- are actually identical.
-
-
- NOW LET'S USE LOTS OF VARIABLES
- ____________________________________________________________
-
- Load ALLVAR.PAS to observe a short program using all 5 of the
- basic data types. The variables are simply assigned values
-
- Page 3-3
-
- Simple Data Types
-
- and the values are printed. A complete ================
- and detailed description of the options ALLVAR.PAS
- available in the Write statement is given ================
- in the TURBO reference manual version 3.0
- on pages 111 through 113, and on pages 500
- through 502 for version 4.0. Pages 52 and 53 of the User's
- Guide has the corresponding information for TURBO Pascal
- version 5.x. It would be to your advantage to read this
- section at this time since very little explanation will be
- given about Write statements from this point on. We will
- discuss the method by which we can write to disk files or
- other output devices in a later chapter of this tutorial.
-
- Back to the basic types. Pascal does lots of cross checking
- for obvious errors. It is illegal to assign the value of any
- variable with a value that is of the wrong type or outside the
- allowable range of that variable. There are routines to
- convert from one system to another when that is necessary.
- Suppose, for example, that you wished to use the value of an
- integer in a calculation of real numbers. That is possible
- by first converting the integer into a real number of the same
- value and using the new real type variable in the desired
- calculations. The new real type variable must of course be
- defined in a var statement as a real type variable before it
- can be used. Details of how to do several conversions of this
- kind will be given in the example program named CONVERT.PAS
- later in this chapter.
-
- Since we have some variables defined, it ================
- would be nice to use the properties of REALMATH.PAS
- computers for which they are famous, ================
- namely some arithmetic. Two programs are
- available for your observation to
- illustrate the various kinds of math available, REALMATH.PAS
- using real variables, and INTMATH.PAS using integer variables.
- You can edit, compile, and run these on your own with no
- comment from me except the comments
- embedded into the source files. Chapter 6 =================
- on pages 51 to 54 of your version 3.0 INTMATH.PAS
- TURBO reference manual completely defines =================
- the simple mathematics available. The
- corresponding list for version 4.0 is
- found in chapter 3 on pages 46 through 49, and pages 48
- through 51 of the TURBO Pascal User's Guide gives the list for
- version 5.x of the compiler.
-
- A byte type variable is used just like an integer variable but
- with a much smaller allowable range. Only one byte of
- computer memory is used for each variable defined as a byte
- type variable, but 2 are used for each integer type variable.
-
-
-
-
-
- Page 3-4
-
- Simple Data Types
-
- BOOLEAN VARIABLES
- ____________________________________________________________
-
- Let's take a look at a boolean variable, which is only allowed
- to take on two different values, TRUE or FALSE. This variable
- is used for loop controls, end of file indicators or any other
- TRUE or FALSE conditions in the program. Variables can be
- compared to determine a boolean value. A complete list of the
- relational operators available with Pascal is given in the
- following list.
-
- = equal to
- <> not equal to
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
-
- These operators can be used to compare any ================
- of the simple types of data including BOOLMATH.PAS
- integer, char, byte, and real type ================
- variables or constants, and they can be
- used to compare boolean variables. An
- illustration is the best way to learn about the boolean
- variable so load BOOLMATH.PAS and observe it.
-
- In BOOLMATH.PAS we define a few boolean variables and two
- integer type variables for use in the program and begin by
- assigning values to the two integer variables. The expression
- Junk = Who in line 14 is actually a boolean operation that is
- not true since the value of Junk is not equal to the value of
- Who. The result is therefore FALSE and that value is assigned
- to the boolean variable A. The boolean variable B is assigned
- the value of TRUE because the expression Junk = (Who - 1) is
- true. The boolean variables C and D are likewise assigned
- some values in a manner that should not need any comment.
- After assigning a value to the variable with the big name, the
- values are all printed out.
-
-
- WHERE DO WE USE THE BOOLEAN VARIABLES?
- ____________________________________________________________
-
- We will find many uses for the boolean type variable when we
- study the loops and conditional statements soon, but until
- then we can only learn what they are. Often, in a conditional
- statement, you will want to do something if either of two
- things are true, in which case you will use the reserved word
- and with two boolean expressions. If either of the two are
- true, the result will be true. Line 29 is an example of this.
- If the boolean variables B, C, and D, are all true, then the
- result will be true and A will be assigned the value of TRUE.
- If any one of them is false, the result will be false and A
- will be assigned the value of FALSE.
-
- Page 3-5
-
- Simple Data Types
-
-
- In Line 31, where the or operator is illustrated, if any of
- the three boolean variables is true, the result will be true,
- and if all three are false, the result will be false. Another
- boolean operator is the not which is illustrated in line 30.
- Examine line 33 which says the result is true only if the
- variable Junk is one less than Who, or if Junk is equal to
- Who.
-
- Compile and run this program, then add some additional
- printout to see if the boolean variables change the way you
- think they should in the last few statements.
-
-
- SHORT CIRCUIT OR COMPLETE EVALUATION?
- ____________________________________________________________
-
- Suppose you have several boolean expressions "and"ed together,
- and when evaluation starts, the first expression results in
- a FALSE. Since the first expression is FALSE, it is
- impossible for the following expressions to ever allow the
- final result to be TRUE because the first FALSE will force the
- answer to be FALSE. It seems like a waste of execution time
- to continue evaluating terms if the final result is already
- known, but that is exactly what standard Pascal will do
- because of the language definition. This is known as complete
- evaluation of a boolean expression. If the system is smart
- enough to realize that the final result is known, it could
- stop evaluation as soon as the final result is known. This
- is known as short circuit evaluation of a boolean expression,
- and could also be applied if a term of an "or"ed boolean
- expression resulted in a TRUE, since the result would always
- be TRUE.
-
- TURBO Pascal version 3.0 always does complete evaluation of
- boolean expressions but TURBO Pascal versions 4.0 and 5.x
- allows you to choose between complete evaluation or short
- circuit evaluation. The default for both compilers is the
- short circuit form but it can be changed through the Options
- menu when you are using the integrated environment, or through
- use of a compiler directive.
-
-
-
- LET'S LOOK AT THE CHAR TYPE VARIABLE
- ____________________________________________________________
-
- A char type variable is a very useful ================
- variable, but usually not when used alone. CHARDEMO.PAS
- It is very powerful when used in an array ================
- or some other user defined data structure
- which is beyond the scope of this chapter.
- A very simple program, CHARDEMO.PAS is included to give you
- an idea of how a char type variable can be used. Study then
-
- Page 3-6
-
- Simple Data Types
-
- compile and run CHARDEMO.PAS for a very brief idea of what the
- char type variable is used for.
-
- Examine the sample program CONVERT.PAS for =================
- several examples of converting data from CONVERT.PAS
- one simple variable to another. The =================
- program is self explanatory.
-
-
- THIS IS FOR TURBO PASCAL 4.0 OR 5.X USERS
- ____________________________________________________________
-
- If you are using TURBO Pascal version 3.0, you are finished
- with this chapter because the data types illustrated in the
- last two programs are not available with that compiler.
-
- If you are using TURBO Pascal 4.0 or 5.x, =================
- display the program NEWINT4.PAS for an NEWINT4.PAS
- example of using the extended integer =================
- types available with that compiler. Four
- variables are defined and values assigned
- to each, then the results are displayed. When you compile and
- run the program, you will see that the variable Big_int can
- indeed handle a rather large number.
-
- It must be pointed out that the calculation in lines 13 and
- 21 result in a different answer even though they appear to be
- calculating the same thing. An explanation is in order. The
- quantity named MaxInt used in lines 10 and 13 is a constant
- built into the system that represents the largest value that
- an integer type variable can store. On the first page of this
- chapter we defined that as 32767 and when running the program
- you will find that Index displays that value as it should.
- The constant MaxInt has a type that is of a universal_integer
- type as do all of the numeric constants in line 13. The
- result then is calculated to the number of significant digits
- dictated by the left hand side of the assignment statement
- which is of type longint resulting in a very large number.
-
- When we get to line 21, however, the variable Index is of type
- integer so the calculations are done as though the constants
- were of type integer also which causes some of the more
- significant digits to be truncated. The truncated result is
- converted to type longint and assigned to the variable Big_int
- and the truncated value is displayed by line 22.
-
- After that discussion it should be apparent to you that it is
- important what types you use for your variables. It must be
- emphasized that it would not be wise to use all large type
- variables because they use more storage space and slow down
- calculations. Experience will dictate the proper data types
- to use for each application.
-
-
-
- Page 3-7
-
- Simple Data Types
-
- NOW FOR THE NEW REAL TYPES
- ____________________________________________________________
-
- If you are using TURBO Pascal 4.0 or 5.x, ================
- display the program NEWREAL4.PAS for an NEWREAL4.PAS
- example using the new "real" types ================
- available with the newer versions of TURBO
- Pascal. Note that you must have an 80X87
- math coprocessor installed to compile and run this program if
- you are using TURBO Pascal version 4.0. There is a note given
- in the file to aid you in selecting it for use.
-
- If you are using TURBO Pascal version 5.x, you can use the
- 80X87 math coprocessor, once again getting help from the note
- embedded in the file. If you do not have a math coprocessor,
- TURBO Pascal version 5.x has an emulator mode which can be
- used as instructed on page 42 of the User's Guide. Keep in
- mind that, even though the emulator will allow you to use
- these newer data types, the resulting program will execute
- much slower due to the extra calculations required.
-
- This program should be self explanatory so nothing will be
- said except that when you run it you can observe the relative
- accuracy of each of the variable types. Once again, you
- should keep in mind that use of the larger "real" types costs
- you extra storage space and reduced run-time speed, but gives
- you more accuracy.
-
-
- PROGRAMMING EXERCISE
- ____________________________________________________________
-
- 1. Write a program containing several variable definitions
- and do some math on them, printing out the results.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Page 3-8