home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP03.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  19.1 KB  |  413 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 3
  5.                                                 SIMPLE DATA TYPES
  6.  
  7.  
  8. WHAT IS A DATA TYPE?
  9. _________________________________________________________________
  10.  
  11. A type in Pascal, and in several other popular programming
  12. languages, defines a variable in such a way that it defines a range
  13. of values which the variable is capable of storing, and it also
  14. defines a set of operations that are permissible to be performed
  15. on variables of that type.  TURBO Pascal has eight basic data types
  16. which are predefined and can be used anywhere in a program provided
  17. you use them properly.  This chapter is devoted to illustrating the
  18. use of these eight data types by defining the allowable range of
  19. values that can be assigned to them, and by illustrating the
  20. operations that can be done to variables of these types.  The eight
  21. types and a very brief description follows;
  22.  
  23.      integer          Whole numbers from -32768 to 32767
  24.      byte             The integers from 0 to 255
  25.      real             Floating point numbers from 1E-38 to 1E+38
  26.      boolean          Can only have the value TRUE or FALSE
  27.      char             Any character in the ASCII character set
  28.      shortint         The integers from -128 to 127
  29.      word             The integers from 0 to 65535
  30.      longint          The integers from -2147483648 to 2147483647
  31.  
  32. Please note that four of these types of data (char, shortint, word,
  33. and longint) are not a part of the standard Pascal definition but
  34. are included as extensions to the TURBO Pascal compiler.
  35.  
  36. In addition to the above data types TURBO Pascal version 5.0 and
  37. later have the following data types available;
  38.  
  39.      single           Real type with 7 significant digits
  40.      double           Real type with 15 significant digits
  41.      extended         Real type with 19 significant digits
  42.      comp             The integers from about -10E18 to 10E18
  43.  
  44. TURBO Pascal version 5.0 and newer have these four types available
  45. which use the 80X87 math coprocessor.  Because TURBO Pascal has a
  46. software emulator for the floating point operations, an 80X87 math
  47. coprocessor is not absolutely required to use these new types with
  48. these versions.  Of course, your resulting program will run much
  49. faster if you have the coprocessor available for use by the
  50. program.
  51.  
  52. A complete definition of the available types for each compiler can
  53. be found in the TURBO Pascal reference manual.  It would be good
  54. to read these pages now for a good definition prior to learning how
  55. to define and use them in a program.  Note that all of these will
  56. be used in example programs in this chapter.
  57.  
  58.  
  59.                                                          Page 3-1
  60.  
  61.                                     Chapter 3 - Simple Data Types
  62.  
  63. OUR FIRST VARIABLES
  64. _________________________________________________________________
  65.  
  66. The integers are by far the easiest to             ==============
  67. understand so we will start with a simple            INTVAR.PAS
  68. program that uses some integers in a very simple   ==============
  69. way.  Load INTVAR.PAS into your TURBO system and
  70. let's take a look at it.
  71.  
  72. Immediately following the program statement is another reserved
  73. word, var.  This reserved word is used to define a variable before
  74. it can be used anywhere in the program.  There is an unbroken rule
  75. of Pascal that states "Nothing can be used until it is defined." 
  76. The compiler will complain by indicating a compilation error if
  77. you try to use a variable without properly defining it.  It seems
  78. a bit bothersome to have to define every variable prior to its use,
  79. but this rule will catch many spelling errors of variables before
  80. they cause trouble.  Some other languages will simply define a new
  81. variable with the new name and go merrily on its way producing some
  82. well formatted garbage for you.
  83.  
  84. Notice that there is only one use of the reserved word var, but it
  85. is used to define three different variables, Count, X, and Y.  Once
  86. the word var is recognized, the compiler will continue to recognize
  87. variable definitions line after line until it finds another
  88. reserved word.  It would be permissible to put a var on the second
  89. line also but it is not necessary.  It would also be permissible
  90. to put all three variables on one line but your particular
  91. programming style will dictate where you put the three variables. 
  92. Following the colon on each line is the word integer which is a
  93. standard identifier, and is therefore different from a reserved
  94. word.  A standard identifier is predefined like a reserved word,
  95. but you can redefine it, thereby losing its original purpose and
  96. meaning.  For now and for a long time, don't do that.
  97.  
  98.  
  99. OUR FIRST ARITHMETIC
  100. _________________________________________________________________
  101.  
  102. Now that we have three variables defined as integer type variables,
  103. we are free to use them in a program in any way we desire as long
  104. as we use them properly.  If we tried to assign a real value to X,
  105. the compiler will generate an error, and prevent a garbage output. 
  106. Observe the start of the main body of the program.  There are three
  107. statements assigning values to X, Y, and Count.  A fine point of
  108. mathematics would state that Count is only equal to the value of
  109. X+Y until one of them is modified, therefore the equal sign used
  110. in so many other languages is not used here.  The sign := is used,
  111. and can be read as "is replaced by the value of," when reading a
  112. listing.  Another quicker way is to use the word "gets".  Thus X
  113. := X + 1 would be read, "X gets the value of X plus 1".  We will
  114. see later that the simple equal sign is reserved for another use
  115. in Pascal.
  116.  
  117.                                                          Page 3-2
  118.  
  119.                                     Chapter 3 - Simple Data Types
  120.  
  121. The first three statements give X the value of 12, Y the value of
  122. 13, and Count the value of 12 + 13 or 25.  If we have a requirement
  123. to get those values out of the computer, we need another extension
  124. to the Writeln statement.  The first part of the data within the
  125. parentheses should be very familiar to you now, but the second part
  126. is new.
  127.  
  128. Multiple outputs can be handled within one Writeln if the fields
  129. are separated by a comma.  To output a variable, simply write the
  130. variable's name in the output field.  The number following the
  131. variable in each case is the number of output columns to be used
  132. by the output data.  This number is optional and can be omitted
  133. allowing the system to use as many columns as it needs.  For
  134. purposes of illustration, they have all been assigned different
  135. numbers of columns.  At this point, you can compile and run
  136. INTVAR.PAS and examine its output.  
  137.  
  138.  
  139. To illustrate the various ways to output data,    ===============
  140. load INTVAR2.PAS and observe that even though       INTVAR2.PAS
  141. the output is identical, it is output in a        ===============
  142. completely different manner.  The Writeln
  143. statements are broken into pieces and the
  144. individual pieces are output with Write and Writeln statements. 
  145. Observe especially that a Writeln all by itself simply moves the
  146. cursor to the beginning of a new line on the video monitor.
  147.  
  148. Compile and run this program and observe its output after you are
  149. certain that the two programs are actually identical.
  150.  
  151.  
  152. NOW LET'S USE LOTS OF VARIABLES
  153. _________________________________________________________________
  154.  
  155. Load ALLVAR.PAS to observe a short program using   ==============
  156. five of the basic data types.  The variables are     ALLVAR.PAS
  157. simply assigned values and the values are          ==============
  158. printed.  A complete and detailed description of
  159. the options available in the Write statement is
  160. given in the TURBO Pascal reference manual.  Check the index to
  161. find this information for the version you are using.  It would be
  162. to your advantage to read this section at this time since very
  163. little explanation will be given about Write statements from this
  164. point on.  We will discuss the method by which we can write to disk
  165. files or other output devices in a later chapter of this tutorial.
  166.  
  167. Back to the basic types.  Pascal does lots of cross checking for
  168. obvious errors.  It is illegal to assign the value of any variable
  169. with a value that is of the wrong type or outside the allowable
  170. range of that variable.  There are routines to convert from one
  171. system to another when that is necessary.  Suppose, for example,
  172. that you wished to use the value of an integer in a calculation of
  173. real numbers.  That is possible by first converting the integer
  174. into a real number of the same value and using the new real type
  175.  
  176.                                                          Page 3-3
  177.  
  178.                                     Chapter 3 - Simple Data Types
  179.  
  180. variable in the desired calculations.  The new real type variable
  181. must of course be defined in a var statement as a real type
  182. variable before it can be used.  Details of how to do several
  183. conversions of this kind will be given in the example program named
  184. CONVERT.PAS later in this chapter.
  185.  
  186. Since we have some variables defined, it would   ================
  187. be nice to use the properties of computers for     REALMATH.PAS
  188. which they are famous, namely some arithmetic.   ================
  189. Two programs are available for your observation
  190. to illustrate the various kinds of math
  191. available, REALMATH.PAS using real variables, and INTMATH.PAS using
  192. integer variables.  You can edit, compile, and run these on your
  193. own with no comment from me except the comments embedded into the
  194. source files.  You should output some of the results using the
  195. method of outputting illustrated in line 24 of the previous example
  196. program.  Read the definition of how to do this in your TURBO
  197. Pascal User's Guide.
  198.  
  199. The example program named INTMATH.PAS             ===============
  200. illustrates some of the math capabilities of        INTMATH.PAS
  201. Pascal when using integer class variables.  A     ===============
  202. byte type variable is used just like an integer
  203. variable but with a much smaller allowable
  204. range.  Only one byte of computer memory is used for each variable
  205. defined as a byte type variable, but 2 are used for each integer
  206. type variable.
  207.  
  208.  
  209. BOOLEAN VARIABLES
  210. _________________________________________________________________
  211.  
  212. Let's take a look at a boolean variable, which is only allowed to
  213. take on two different values, TRUE or FALSE.  This variable is used
  214. for loop controls, end of file indicators or any other TRUE or
  215. FALSE conditions in the program.  Variables can be compared to
  216. determine a boolean value.  A complete list of the relational
  217. operators available with Pascal is given in the following list.
  218.  
  219.      =         equal to
  220.      <>        not equal to
  221.      >         greater than
  222.      <         less than
  223.      >=        greater than or equal to
  224.      <=        less than or equal to
  225.  
  226. These operators can be used to compare any of    ================
  227. the simple types of data including integer,        BOOLMATH.PAS
  228. char, byte, and real type variables or           ================
  229. constants, and they can be used to compare
  230. boolean variables.  An illustration is the best
  231. way to learn about the boolean variable so load BOOLMATH.PAS and
  232. observe it.
  233.  
  234.  
  235.                                                          Page 3-4
  236.  
  237.                                     Chapter 3 - Simple Data Types
  238.  
  239. In BOOLMATH.PAS we define a few boolean variables and two integer
  240. type variables for use in the program and begin by assigning values
  241. to the two integer variables.  The expression Junk = Who in line
  242. 14 is actually a boolean operation that is not true since the value
  243. of Junk is not equal to the value of Who,  The result is therefore
  244. FALSE and that value is assigned to the boolean variable A.  The
  245. boolean variable B is assigned the value of TRUE because the
  246. boolean expression Junk = (Who - 1) is true.  The boolean variables
  247. C and D are likewise assigned some values in a manner that should
  248. not need any comment.  After assigning a value to the variable with
  249. the big name, the values are all printed out.  Note that if either
  250. A or B is TRUE, the result is TRUE in line 18.
  251.  
  252.  
  253. WHERE DO WE USE THE BOOLEAN VARIABLES?
  254. _________________________________________________________________
  255.  
  256. We will find many uses for the boolean type variable when we study
  257. the loops and conditional statements soon, but until then we can
  258. only learn what they are.  Often, in a conditional statement, you
  259. will want to do something if both of two things are true, in which
  260. case you will use the reserved word and with two boolean
  261. expressions.  If both are true, the result will be true.  Line 29
  262. is an example of this.  If the boolean variables B, C, and D, are
  263. all true, then the result will be true and A will be assigned the
  264. value of TRUE.  If any one of them is false, the result will be
  265. false and A will be assigned the value of FALSE.
  266.  
  267. In Line 31, where the or operator is illustrated, if any of the
  268. three boolean variables is true, the result will be true, and if
  269. all three are false, the result will be false.  Another boolean
  270. operator is the not which is illustrated in line 30.  Examine line
  271. 33 which says the result is true only if the variable Junk is one
  272. less than Who, or if Junk is equal to Who.  This should indicate
  273. the level of flexibility available with a boolean variable.
  274.  
  275. Compile and run this program, then add some additional printout to
  276. see if the boolean variables change in the manner you think they
  277. should in the last few statements.
  278.  
  279.  
  280. SHORT CIRCUIT OR COMPLETE EVALUATION?
  281. _________________________________________________________________
  282.  
  283. Suppose you have several boolean expressions "and"ed together, and
  284. when evaluation starts, the first expression results in a FALSE. 
  285. Since the first expression is FALSE, it is impossible for the
  286. following expressions to ever allow the final result to be TRUE
  287. because the first FALSE will force the answer to be FALSE.  It
  288. seems like a waste of execution time to continue evaluating terms
  289. if the final result is already known, but that is exactly what
  290. standard Pascal will do because of the language definition.  This
  291. is known as complete evaluation of a boolean expression.  If the
  292. system is smart enough to realize that the final result is known,
  293.  
  294.                                                          Page 3-5
  295.  
  296.                                     Chapter 3 - Simple Data Types
  297.  
  298. it could stop evaluation as soon as the final result is known. 
  299. This is known as short circuit evaluation of a boolean expression,
  300. and could also be applied if a term of an "or"ed boolean expression
  301. resulted in a TRUE, since the result would always be TRUE.
  302.  
  303. TURBO Pascal versions 5.0 and later, allows you to choose between
  304. complete evaluation or short circuit evaluation.  The default for
  305. these compilers is the short circuit form but it can be changed
  306. through the Options menu when you are using the integrated
  307. environment, or through use of a compiler directive.
  308.  
  309.  
  310. LET'S LOOK AT THE CHAR TYPE VARIABLE
  311. _________________________________________________________________
  312.  
  313. A char type variable is a very useful variable,  ================
  314. but usually not when used alone.  It is very       CHARDEMO.PAS
  315. powerful when used in an array or some other     ================
  316. user defined data structure which is beyond the
  317. scope of this chapter.  A very simple program,
  318. CHARDEMO.PAS is included to give you an idea of how a char type
  319. variable can be used.  Study, then compile and run CHARDEMO.PAS for
  320. a very brief idea of what the char type variable is used for.
  321.  
  322. Examine the sample program CONVERT.PAS for        ===============
  323. several examples of converting data from one        CONVERT.PAS
  324. simple variable to another.  The comments make    ===============
  325. the program self explanatory.
  326.  
  327.  
  328. EXTENDED INTEGER TYPES
  329. _________________________________________________________________
  330.  
  331. Display the program EXTINT.PAS for an example of   ==============
  332. using the extended integer types available with      EXTINT.PAS
  333. the Pascal compiler.  Four variables are defined   ==============
  334. and values assigned to each, then the results
  335. are displayed.  When you compile and run the
  336. program, you will see that the variable Big_int can indeed handle
  337. a rather large number.
  338.  
  339. It must be pointed out that the calculation in lines 13 and 21
  340. result in a different answer even though they appear to be
  341. calculating the same thing.  An explanation is in order.  The
  342. quantity named MaxInt used in lines 10 and 13 is a constant built
  343. into the system that represents the largest value that an integer
  344. type variable can store.  On the first page of this chapter we
  345. defined that as 32767 and when running the program you will find
  346. that Index displays that value as it should.  The constant MaxInt
  347. has a type that is of a universal_integer type as do all of the
  348. numeric constants in line 13.  The result then is calculated to the
  349. number of significant digits dictated by the left hand side of the
  350. assignment statement which is of type longint resulting in a very
  351. large number.
  352.  
  353.                                                          Page 3-6
  354.  
  355.                                     Chapter 3 - Simple Data Types
  356.  
  357.  
  358. When we get to line 21, however, the variable Index is of type
  359. integer so the calculations are done as though the constants were
  360. of type integer also which causes some of the more significant
  361. digits to be truncated.  The truncated result is converted to type
  362. longint and assigned to the variable Big_int and the truncated
  363. value is displayed by line 22.
  364.  
  365. After that discussion it should be apparent to you that it is
  366. important what types you use for your variables.  It must be
  367. emphasized that it would not be wise to use all large type
  368. variables because they use more storage space and slow down
  369. calculations.  Experience will dictate the proper data types to use
  370. for each application.
  371.  
  372.  
  373. EXTENDED REAL TYPES
  374. _________________________________________________________________
  375.  
  376. Display the program EXTREAL.PAS for an example    ===============
  377. using the new "real" types available with the       EXTREAL.PAS
  378. newer versions of TURBO Pascal.                   ===============
  379.  
  380. If you are using a version of TURBO Pascal which
  381. is 5.0 or newer, you can use the 80X87 math coprocessor.  If you
  382. do not have a math coprocessor, TURBO Pascal version 5.0 and newer
  383. has an emulator mode which can be used as instructed in the User's
  384. Guide.  Keep in mind that, even though the emulator will allow you
  385. to use these newer data types, the resulting program will execute
  386. much slower due to the extra calculations required.
  387.  
  388. This program should be self explanatory so nothing will be said
  389. except that when you run it, you can observe the relative accuracy
  390. of each of the variable types.  Once again, you should keep in mind
  391. that use of the larger "real" types costs you extra storage space
  392. and reduced run-time speed, but gives you more accuracy.
  393.  
  394.  
  395. PROGRAMMING EXERCISE
  396. _________________________________________________________________
  397.  
  398. 1.   Write a program containing several variable definitions and
  399.      do some math on them, printing out the results.
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.                                                          Page 3-7
  413.