home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / m2t-1.zip / CHAP03.TXT < prev    next >
Text File  |  1989-01-18  |  36KB  |  764 lines

  1.  
  2.                                                      Chapter 3
  3.  
  4.                                 THE SIMPLE MODULA-2 DATA TYPES
  5.  
  6.  
  7.  
  8. TYPES ARE IMPORTANT
  9. ______________________________________________________________
  10.  
  11. The material in this chapter is extremely important to you as
  12. you strive to become a good Modula-2 programmer, but you may
  13. also find it to be somewhat tedious because it contains so
  14. many facts.  This material is needed in order to develop the
  15. topics in the next few chapters, but all of the details are
  16. not necessarily required.  For that reason, you may wish to
  17. go through it rather rapidly picking up the high points and
  18. come back to this chapter for the details later when they will
  19. be much more meaningful.  Do not completely pass over this
  20. material at this time or the next few chapters will be
  21. meaningless.  If you are already highly experienced in other
  22. programming languages, you may be able to survive the next few
  23. chapters without this material, but keep in mind that Modula-
  24. 2 has a few unique constructs.
  25.  
  26. The most important basic principle in this chapter is the
  27. introduction of the data type.  The type of a variable defines
  28. a range of values the variable can have assigned to it and it
  29. also defines a set of operations that are available for use
  30. with the variable.  By the proper application of data typing,
  31. the system checks your usage of values to see that you use
  32. them in a reasonable manner.
  33.  
  34.  
  35. MODULA-2 IS CHANGING
  36. ______________________________________________________________
  37.  
  38. Because Modula-2 is changing based on experience with use,
  39. some of these programs may not compile correctly.  Every
  40. effort was made to make the example programs as error free as
  41. possible but due to slight changes in the language, you may
  42. have a few compilation problems.  We will say more about this
  43. after we develop a few topics so we can talk more
  44. intelligently about it.
  45.  
  46. Of course, due to slight variations in compilers you may have
  47. a few problems also.  Modula-2 is a well designed and defined
  48. language, but there is room for slight differences in
  49. compilers and the way they interpret various constructs.
  50.  
  51.  
  52. A PROGRAM WITH VARIABLES
  53. ______________________________________________________________
  54.  
  55. The program named INTVAR.MOD is our first program with some
  56. variables in use.  This program begins in the usual way since
  57. it has a module header and the import list.  Next we come to
  58.  
  59.                                                            3-1
  60.  
  61.                              Chapter 3 - The Simple Data Types
  62.  
  63. a new reserved word, VAR.  This word is       ================
  64. used to indicate to the compiler that we         INTVAR.MOD
  65. wish to define one or more variables.  In     ================
  66. Modula-2, there is a rule that says you
  67. can use nothing until it is defined.  If we wish to use a
  68. variable in the program, we must first define that it will
  69. exist, and what kind of a variable it is, or to be more
  70. specific, what type it is.  After that, it can be used in the
  71. program to do what needs to be done.
  72.  
  73. Following the reserved word VAR in line 16, we have the
  74. variable named Count defined.  The reserved word INTEGER
  75. following the colon states that the variable named Count will
  76. be of type INTEGER.  This means that it can store any whole
  77. number between -32768 to 32767 on most microcomputers, and a
  78. considerably larger range on larger computers.  Don't worry
  79. too much about this yet, the next program will completely
  80. define what an integer type variable is.  It is important to
  81. recognize that after we have defined the variable named Count,
  82. it still doesn't have a value stored in it, that will come
  83. later.
  84.  
  85. Line 7 has two more variables defined, namely x, and y.  Once
  86. the reserved word VAR is mentioned, as many variables as
  87. desired can be defined.  They are also integer type variables
  88. and do not have a value stored in them yet.  You can think of
  89. the three variables as three empty boxes, each capable of
  90. storing a number but with no number in them yet.  It would be
  91. perfectly permissible to put all three variables on one line,
  92. or to have separated them such that each was on a separate
  93. line.  At this point, the program doesn't know that there is
  94. any difference between them, because there isn't any.  The
  95. fact that one will contain the sum of the other two has no
  96. meaning yet, the comments are only for us, not the computer.
  97.  
  98. Note that Modula-2 gives you no way to initialize a variable
  99. when it is declared.  This is permissible in many other
  100. languages.
  101.  
  102.  
  103. USING VARIABLES IN A PROGRAM
  104. ______________________________________________________________
  105.  
  106. Now we will examine the program itself.  Line 11 sets the
  107. variable named x to the value 12, in effect putting the number
  108. 12 in the box mentioned earlier.  The sign := is the Modula-2
  109. symbol for assignment.  It is most meaningful to read the
  110. symbol "gets the value of" since it is not really stating a
  111. mathematical equality but is saying in effect, "assign the
  112. value of this to the variable at the left."  The entire line
  113. can be read as "x gets the value of 12."  There is now a value
  114. assigned to the variable x declared in the header.  The next
  115. statement assigns the value of 13 to the variable named y.
  116. Finally the value of the data stored in the variable named x
  117.  
  118.                                                            3-2
  119.  
  120.                              Chapter 3 - The Simple Data Types
  121.  
  122. is added to the value of the data stored in the variable named
  123. y, and the sum is stored in the variable named Count.  We have
  124. therefore done our first calculation in Modula-2, but we will
  125. do many more before this tutorial is completed.  The observant
  126. student will notice that each statement is terminated with a
  127. semicolon, a Modula-2 requirement.
  128.  
  129. The three variables are then displayed on the monitor with
  130. appropriate prose to identify them in lines 17 through 25.
  131. The only new statement here is the WriteInt procedure that
  132. needs a little explanation.  This procedure is used to output
  133. an integer type variable to the monitor or whatever device is
  134. being used.  By definition, it contains two quantities within
  135. the parentheses, the first being the variable name and the
  136. second being the number of columns the output should fill.
  137. If there are not enough columns specified to output the data,
  138. more will be used so that no digits will be truncated.  If all
  139. are not needed, leading blanks will be output.  If the
  140. variable named x had the value of 1234 when we came to program
  141. line 18, all four digits would be output in spite of the
  142. request for three.  Since the variable x has the value of 12,
  143. only two columns will be used and one leading blank will be
  144. output.  In like manner, the value of the variable named y is
  145. allotted 4 columns and the value of Count is to be output in
  146. 6 columns.
  147.  
  148. Lines 27 and 28 of the program assign new values to two of the
  149. variables.  The variable x is assigned the value of FF
  150. hexadecimal which is 255 decimal, and y is assigned the value
  151. of 177 octal which is 127 decimal.  Note that the hexadecimal
  152. number must have a leading digit.  In this case, a zero had
  153. to be prepended.  This is only done as an illustration to you
  154. of how it is done.  If you don't understand these two
  155. numbering systems, simply ignore this until you have a need
  156. for it.
  157.  
  158. Compile and run this program to see if it does what you expect
  159. it to do.  The important thing to notice in this program is
  160. the variable definition in the definition part of the module
  161. and the variable assignment in the program part.  It should
  162. be obvious, but it would be well to mention that the
  163. definition part of the module extends from the module name to
  164. the reserved word BEGIN and is where all definitions are put.
  165. Likewise, the program part of the module includes all
  166. statements from the begin statement to the end statement.
  167.  
  168.  
  169. SIMPLE VARIABLE TYPES
  170. ______________________________________________________________
  171.  
  172. Modula-2 has several predefined data types that you can use
  173. in your programs.  You also have the ability to define any
  174. number of complex types built up from the simple types but we
  175. will not discuss this until we get to chapter 6 of this
  176.  
  177.                                                            3-3
  178.  
  179.                              Chapter 3 - The Simple Data Types
  180.  
  181. tutorial.  The simple types are INTEGER, CARDINAL, REAL,
  182. BOOLEAN, and CHAR.  Each has its own purpose and its own
  183. peculiarities and we will cover each type one at a time.
  184.  
  185. A few other types may be available with your particular
  186. compiler, but they are not universal, so you must check your
  187. documentation to see if they are available with your system.
  188. The types LONGINT, LONGCARD, and LONGREAL are additional types
  189. that represent long integer, long cardinal, and long real
  190. types respectively.  In each case more storage is required,
  191. but numbers covering a larger range can be stored in variables
  192. of these types.  After you understand the basic types, these
  193. new types will be easy for you to understand, so they will not
  194. be illustrated in this tutorial.
  195.  
  196.  
  197. THE SIMPLE VARIABLE - INTEGER
  198. ______________________________________________________________
  199.  
  200. Examine the program named INTMATH.MOD for      ===============
  201. a few examples of integer math operations.       INTMATH.MOD
  202. In the declaration part of the program         ===============
  203. (the part prior to the BEGIN) we have 7
  204. integer type variables defined for use in the program which
  205. we will use to illustrate integer arithmetic in the Modula-2
  206. programming language.
  207.  
  208. An integer variable, can store any whole number between -32768
  209. and 32767 on most microcomputers.  An attempt to store any
  210. other value in an integer type variable should produce an
  211. error by your compiler but it may produce some other result.
  212. Some compilers may store a -32769, which is one count too low,
  213. as a 32767 which is at the top end of the range.  This is due
  214. to the two's complement arithmetic that you don't need to
  215. understand at this point.  It will be left to you to determine
  216. what your compiler does in such a case.
  217.  
  218. Line 10 of this program is nothing new to you, it simply
  219. assigns the variable named A the value of 9.  Line 11 adds 4
  220. to the value stored in the variable A and the result, 13, is
  221. stored in the variable named B.  Next, the values stored in
  222. the variables named A and B are added together and the sum,
  223. which is 13, is stored in the variable named IntSum.
  224. Continuing in the same manner, the difference and the product
  225. are calculated and stored.  When we come to integer division,
  226. we are breaking new ground because the result is truncated to
  227. the largest whole number resulting from the division.  Thus
  228. 13 DIV 9 results in 1 because the remainder is simply dropped.
  229.  
  230. The next construct, B MOD A results in the remainder of the
  231. division, which is 4 in this case.  Note that the modulo (MOD)
  232. operator can only be used with a positive denominator in
  233. Modula-2.  Any attempt to use the modulo operator with a
  234. negative denominator will result in a fatal run time error.
  235.  
  236.                                                            3-4
  237.  
  238.                              Chapter 3 - The simple Data Types
  239.  
  240. You will find these operations very useful as you progress as
  241. a Modula-2 programmer.
  242.  
  243. The intent of the next line is to illustrate that you can do
  244. several math operations in a statement if you are careful to
  245. put the parentheses in the proper places.  The rules about
  246. operator precedence are similar to those for other programming
  247. languages, and they are well defined, but I recommend that you
  248. use lots of parentheses to remove all doubt as to what the
  249. results will be.
  250.  
  251.  
  252. THE INC AND DEC OPERATIONS
  253. ______________________________________________________________
  254.  
  255. The results of the operations are each displayed in 6 columns
  256. and we move on to several new operations.  The first new
  257. operation is the INC which is short for increment.  This
  258. simply increments the variable contained within the
  259. parentheses and if a second argument is given, the variable
  260. is incremented by the value of that variable.  In like manner,
  261. the DEC procedure decrements the variable in the parentheses
  262. by one unless a second argument is given in which case the
  263. variable is decremented by the value of that variable.
  264.  
  265. It may not be clear at this point, but the second variable
  266. itself may be another variable name or even a composite of
  267. several as long as it results in an integer type variable.
  268. This is illustrated in the program in line 33.
  269.  
  270.  
  271.  
  272. THE MIN AND MAX OPERATIONS
  273. ______________________________________________________________
  274.  
  275. Finally, we come to the last two procedures in this program,
  276. the MIN and the MAX.  These two function procedures are built
  277. into the language and therefore do not need to be imported.
  278. They are properly called function procedures because each
  279. returns a value, but it is still proper to refer to them
  280. simply as procedures.  These two procedures will return the
  281. value of the smallest possible integer, -32768 and the largest
  282. possible integer, 32767 for most microcomputers.  These are
  283. the values returned for a 16 bit computer.  It is possible
  284. that you are using a computer and compiler combination that
  285. uses more than 16 bits.  If this is the case, MIN and MAX will
  286. return larger numbers.
  287.  
  288. Compile and execute this program and observe the output.  If
  289. your compiler results in errors, you may have to make some
  290. changes in order to compile it.  Some of the more popular
  291. Modula-2 compilers do not implement the MIN and MAX
  292. procedures, so you may need to remove or comment out lines 35
  293. and 36 of this program prior to compilation.  The min and max
  294.  
  295.                                                            3-5
  296.  
  297.                              Chapter 3 - The Simple Data Types
  298.  
  299. procedures are relatively new additions to Modula-2 which
  300. explains why they are not yet available with all compilers.
  301.  
  302.  
  303. THE SIMPLE VARIABLE - CARDINAL
  304. ______________________________________________________________
  305.  
  306. Examine the file named CARDMATH.MOD for a     ================
  307. few examples of cardinal mathematics and        CARDMATH.MOD
  308. output.  In this file, 7 variables are        ================
  309. defined as type CARDINAL and one more as
  310. type INTEGER.  A cardinal variable can store any whole number
  311. from 0 to 65535 in a 16 bit microcomputer, although the range
  312. may be different if you are using a computer and compiler
  313. combination that uses more bits.
  314.  
  315. The first few lines are the same as the last program so very
  316. little needs to be said about them except for the subtraction
  317. example.  In this case, the result of the subtraction would
  318. be negative if it were carried out as in the last program so
  319. A is subtracted from B.  It is an error to attempt to store
  320. a negative number in a cardinal type variable.  For that
  321. reason, a cardinal should not be used if there is any chance
  322. that it will be required to go negative.  Programming
  323. experience will be the best teacher when it comes to deciding
  324. what variable types to use in each situation.
  325.  
  326. In this program the variables are once again displayed, but
  327. now the procedure named WriteCard is used for output because
  328. the variables to be output are of the cardinal type.
  329.  
  330. The next two statements indicate that integer and cardinal
  331. variables are "assignment compatible", meaning that they can
  332. be assigned to each other with the := operator.  They cannot
  333. however, be mixed in calculations.  Constants in an expression
  334. are assumed to be of the same type as the variables in the
  335. expression and they must agree.  For that reason, the
  336. expression in line 36 is invalid because (-112) is a negative
  337. constant and therefore not cardinal.  In the prior line it is
  338. permissible to subtract the positive number 112 from the value
  339. of A as long as the result is still positive.  As an exercise,
  340. change line 34 such that a number less than 112 is assigned
  341. to the variable named A.  The program will compile without
  342. error but when you run it, you should get a runtime error
  343. because the cardinal assignment is out of range.  Notice that
  344. the constant value of -112 is permissible for use as an
  345. integer constant.
  346.  
  347. The remaining statements in the program are the same as the
  348. last program so additional explanation is unneeded.  It would
  349. be good to point out that in the case of cardinal, the MIN and
  350. MAX procedures will return values of 0 and 65535 for most 16
  351. bit implementations.  Computers using additional bits will
  352. result in a much larger value for MAX.  Compile and run this
  353.  
  354.                                                            3-6
  355.  
  356.                              Chapter 3 - The Simple Data Types
  357.  
  358. program remembering that it may be necessary to comment out
  359. the MIN and MAX statements to get a successful compilation,
  360. since they may not be available with your compiler.
  361.  
  362.  
  363. HOW NEW IS YOUR COMPILER?
  364. ______________________________________________________________
  365.  
  366. Modula-2, as mentioned near the beginning of this chapter, is
  367. a dynamic language that is changing as the base of experience
  368. dictates weaknesses.  This is true of any good modern
  369. programming language.  As originally defined, Modula-2 used
  370. the cardinal type for most operations, assuming cardinal if
  371. the type was not specifically stated and if the type could be
  372. either integer or cardinal.  In his Fourth edition of
  373. "Programming in Modula-2", Niklaus Wirth has changed this to
  374. favor the integer in some situations.  The language will now
  375. be more similar to a few other modern languages, many of which
  376. do not have a cardinal representation.
  377.  
  378. This change makes this tutorial difficult to write, since all
  379. compilers will not comply with this change for several years.
  380. I will try to point out the places where the changes are
  381. taking place, but I will not be able to find them all.  You
  382. may be required to do a little searching in your compiler
  383. documentation for some of the changes as they come up in the
  384. example programs.
  385.  
  386.  
  387.  
  388. THE SIMPLE VARIABLE - REAL
  389. ______________________________________________________________
  390.  
  391. Examine the program named REALMATH.MOD for    ================
  392. a demonstration of the use of the data          REALMATH.MOD
  393. type REAL.  The definition part of this       ================
  394. program is similar to the last with some
  395. additions to the import list.  Your compiler may use different
  396. names for some of the procedures here, so if you get a compile
  397. error you will need to modify these.  We will study the import
  398. (and export) list in detail later, so be patient.
  399.  
  400. Several real variables and one each of the integer and
  401. cardinal types are defined for use in the program.  The real
  402. type variable can contain numbers in a wide range and with
  403. fractional parts included.  The exact range, and the accuracy
  404. will vary widely depending on your implementation.  It will
  405. be up to you to check your reference manual for the limits on
  406. your computer and compiler.  A real type number is defined as
  407. one with a decimal point, and in Modula-2, there must be at
  408. least one digit prior to the decimal point, but none are
  409. required following it.  The mathematics are the same as with
  410. the other two except that the division symbol is the slash
  411. (/).  There is no MOD for real type numbers because there is
  412.  
  413.                                                            3-7
  414.  
  415.                              Chapter 3 - The Simple Data Types
  416.  
  417. theoretically no remainder, since a fractional part is
  418. computed as part of the calculation.
  419.  
  420. The four results are displayed on the monitor with 12 columns
  421. allowed for each result and two extra blanks displayed between
  422. each number.  Unfortunately, we have no control over how many
  423. digits will be displayed following the decimal point.  This
  424. would be nice for outputting data in a financial model where
  425. we would like to have two digits following the decimal point.
  426. When we get to the advanced part of this tutorial, we will
  427. write our own procedure for outputting real values in such a
  428. way that we can call it from any program just like we call
  429. these output procedures.
  430.  
  431.  
  432. CONVERSION BETWEEN DATA TYPES
  433. ______________________________________________________________
  434.  
  435. Beginning in line 33, we assign the integer and cardinal
  436. variables some values and convert the values to type real by
  437. using the procedure named FLOAT.  We then convert the variable
  438. Sum to integer and cardinal by use of the procedure named
  439. TRUNC.  The fractional part, if any, will simply be thrown
  440. away.  These procedures will be very useful in many of your
  441.  
  442. programs.
  443.  
  444. Lines 40 and 41 once again use the MIN and MAX functions to
  445. return the value of the largest positive real number and the
  446. smallest positive real number for your implementation.  Once
  447. again, your compiler may not support these two functions and
  448. they may have to be commented out in order to compile.  Be
  449. sure to compile and execute this program.
  450.  
  451.  
  452. THE REAL MATHEMATICS LIBRARY
  453. ______________________________________________________________
  454.  
  455. Besides the standard arithmetic               ================
  456. operations, Modula-2 provides a library of      REALTRIG.MOD
  457. standard geometric and trigonometric          ================
  458. functions in the library module MathLib0.
  459. These must be imported in order to use them as illustrated in
  460. the example program REALTRIG.MOD which you should examine at
  461. this time.
  462.  
  463. The program itself should be self explanatory with the
  464. exception of lines 24 and 25 which illustrate the real and
  465. entier functions which convert data between the real and
  466. integer types.  The word entier is French for integer.  Note
  467. that if the real value will not fit into the integer range of
  468. values, the result is undefined by the definition of Modula-
  469. 2, but your compiler documentation should define the result
  470. in this case.
  471.  
  472.                                                            3-8
  473.  
  474.                              Chapter 3 - The Simple Data Types
  475.  
  476. THE SIMPLE VARIABLE - BOOLEAN
  477. ______________________________________________________________
  478.  
  479. Examine the file named BOOLMATH.MOD for an    ================
  480. example of boolean variables and some           BOOLMATH.MOD
  481. boolean operations.  A boolean variable       ================
  482. can only have one of two possible values,
  483. TRUE or FALSE.  The values TRUE and FALSE are standard
  484. identifiers which are defined by the Modula-2 system and
  485. available for your use.  These variables cannot be printed
  486. directly but can be used to control other print statements to
  487. print out a representation of their value.  We will see how
  488. later.
  489.  
  490. We define 3 boolean variables and 3 integer variables and
  491. assign values to the 3 integer variables in the program for
  492. use in these illustrations.  In line 13 the boolean expression
  493. "A = 22" is true, therefore the boolean variable named IsIt
  494. is assigned the value TRUE.  The variable IsIt could be used
  495. later in the program to make a decision, by a yet undefined
  496. method, to do something or bypass it.  In like manner, the
  497. next statement assigns IsIt the value FALSE because A is not
  498. equal to 23.  The remainder of the allowed boolean expressions
  499. are defined in the next few lines and are left for your
  500. inspection and study.
  501.  
  502. Lines 20 and 21 illustrate forms of the "not equal" operator.
  503. According to the latest definition of Modula-2, the <> is no
  504. longer legal for use and must be changed to the # form.  You
  505. may need to comment out line 21 to compile this program.
  506.  
  507. Beginning in line 25, composite boolean expressions are
  508. illustrated.  As many boolean expressions as desired can be
  509. combined with AND and OR operators.  If two or more boolean
  510. expressions are combined with the AND, the result is true if
  511. all expressions are true.  If two or more boolean expressions
  512. are combined with the OR, the result is true if any of them
  513. are true.  The NOT operator inverts the sense of what it
  514. modifies, it turns a TRUE to FALSE and vice-versa.  Finally,
  515. a couple of composite boolean expressions are given for
  516. illustration of the amount of complexity that is allowed,
  517. although there is no real limit as to how far you can go with
  518. the complexity.  Good programming practice would dictate that
  519. you keep it simple and understandable.
  520.  
  521. The ampersand (&) can be used in place of the AND operator,
  522. and the tilde (~) can be used in place of the OR operator, but
  523. the use of the words is considerably clearer.  Their use is
  524. illustrated in lines 35 and 36.
  525.  
  526.  
  527.  
  528.  
  529.                                                            3-9
  530.  
  531.                              Chapter 3 - The Simple Data Types
  532.  
  533. TWO RULES FOR BOOLEAN EVALUATION
  534. ______________________________________________________________
  535.  
  536. First it is important that you use the same type of variables
  537. within a boolean expression.  Real type variables can be
  538. compared to other reals and integers to integers, but reals
  539. cannot be compared to integers.  Cardinal and char types can
  540. also be compared to their own types, but none of the four can
  541. be compared directly to each other.
  542.  
  543. Secondly, Modula-2 uses a short circuit evaluation technique
  544. for boolean evaluation.  Evaluation proceeds from left to
  545. right and if it finds a result which will positively determine
  546. the outcome, evaluation stops.  For example, if it is
  547. evaluating a string of 5 comparisons all combined with an AND,
  548. and it finds that the second term is false, evaluation stops
  549. there.  Since all terms must be true for the result to be
  550. true, it makes no difference what values the last three are,
  551. the result will be false because of the second term.
  552.  
  553. Be sure to compile and execute this program even though it has
  554. no output.  You should verify for your own information that
  555. this program will compile and execute correctly with your
  556. system.
  557.  
  558.  
  559. THE SIMPLE VARIABLE - CHAR
  560. ______________________________________________________________
  561.  
  562. Examine the program named CHARDEMO.MOD for    ================
  563. an illustration of use of the last simple       CHARDEMO.MOD
  564. variable type, CHAR.  Text data is stored     ================
  565. in a computer in a format utilizing the
  566. char data type.  Although there are exceptions, such as when
  567. text is stored in some form of a packed mode, this is nearly
  568. always true.  This tutorial was written with a word processor
  569. that uses a char type for text storage, and few word
  570. processors use any other method.
  571.  
  572. Although there are many different ways to store text, only two
  573. are used to any level of significance, EBCDIC and ASCII.  This
  574. merely refers to the way the characters of the alphabet and
  575. all other characters are represented in the computer.  The
  576. ASCII standard defines that the value of 65 will be the letter
  577. A, 66 will be the letter B, etc.  If everyone uses the same
  578. standard, transfer of data from one computer to another is
  579. greatly simplified.
  580.  
  581. The program named CHARDEMO.MOD has the usual header, imports
  582. the procedure named Write for use in displaying the char type
  583. data, then defines 4 char type variables for use in the
  584. program.  An integer type variable is also defined.  In the
  585. program itself, we begin in lines 11 and 12 by assigning two
  586. of the variables some char data.  Since a char variable is
  587.  
  588.                                                           3-10
  589.  
  590.                              Chapter 3 - THe Simple Data Types
  591.  
  592. capable of storing one letter, numeral, or special character,
  593. each variable is assigned one letter.  The single or double
  594. quotes are used as an indication to the compiler that you
  595. intend for it to use the letter as a char type variable rather
  596. than as another variable name.  Of course if you wanted to use
  597. A as a variable name, you would have to define it in the
  598. definition part of the module.
  599.  
  600.  
  601. TWO SPECIAL CHAR PROCEDURES
  602. ______________________________________________________________
  603.  
  604. The next instruction gets the ordinal value of the letter A,
  605. adds 2 to it, and assigns that value to the variable named
  606. Index, which must be an integer (although it could have been
  607. defined as a cardinal type variable).  Refer to the
  608. documentation that came with your computer and you will find
  609. an ASCII table that will define the letter A as 65.  Finally,
  610. the char type variable Dog3 is assigned the character value
  611. of Index.  Your ASCII table should define 67 as the letter C.
  612. It is important to understand that the char variable Dog3
  613. contains the character representation of the letter C, and the
  614. integer variable Index contains the numerical value of the
  615. ASCII representation of the letter C.  It would be perfectly
  616. alright to use the variable Index for any desired numerical
  617. calculations, but not to display the letter C.  On the other
  618. hand, it would be alright to use the variable Dog3 to display
  619. the letter C on the monitor but it could not be used for any
  620. calculations.  The purpose therefore, of the two procedures
  621. named ORD and CHR, is to translate from one type to the other.
  622.  
  623. The variable Cat4 is assigned the double quote by enclosing
  624. it in the single quotes, and the characters are output in a
  625. funny order to spell "CAT".  The variable Char1 is assigned
  626. the value of S, and the word is completed resulting in the
  627. full word "CATS" on the monitor after the program is compiled
  628. and run.  If this were the only way to use the char type
  629. variable, it would be very tedious and frustrating, but there
  630. are other methods to use the char type that are far more
  631. useful as you will see later in this tutorial.
  632.  
  633. Next, an additional means of assigning a char type variable
  634. is given.  By assigning the char variable the value 65C, it
  635. is the same as writing CHR(65), resulting in the  variable
  636. having the internal value A.  A number less than 256 followed
  637. by a C is defined by Modula-2 as a char type constant.
  638.  
  639. Finally, the variable Char1 is assigned the letter a and it
  640. is converted to upper case A with the procedure named CAP.
  641. This procedure will convert the argument to its upper case
  642. equivalent if it is a lower case letter.  If its argument is
  643. an upper case letter or any other character, it will do
  644. nothing to it.  Be sure to compile and execute CHARDEMO.MOD
  645. and observe the output.
  646.  
  647.                                                           3-11
  648.  
  649.                              Chapter 3 - The Simple Data Types
  650.  
  651. One final point should be made about the char type variable.
  652. There are no arithmetic operations that can be done on a char
  653. type variable.  To increment a char type variable for example,
  654. it must be converted to a numerical type, incremented, then
  655. converted back to char.  Note that the actual increment is
  656. performed on the numerical type, not the char type.
  657.  
  658.  
  659. USING THE TRANSFER PROCEDURES
  660. ______________________________________________________________
  661.  
  662. Examine the file named TRANSFER.MOD for       ================
  663. several examples of transferring data           TRANSFER.MOD
  664. between the various simple data types.        ================
  665. The transfer functions given here may not
  666. seem too important at this time, but some time spent here will
  667. help to reduce the frustration later when you get seemingly
  668. wrong errors that say you are using incompatible types in a
  669. statement.  All of the program will not be discussed, only
  670. those statements that use some of the more unusual
  671. capabilities of Modula-2.
  672.  
  673. In line 13, the calculations are done in integer format, but
  674. due to the assignment compatibility of integer and cardinal,
  675. the result is converted to cardinal across the assignment
  676. operator.  In lines 14 and 15, the integer type Int1 is
  677. converted to cardinal and the mathematics are completed in the
  678. cardinal type.  This is an example of type coercion, which
  679. will be explained in a couple of paragraphs.  Line 16 is an
  680. illustration of mixed mathematics using the transfer procedure
  681. INTEGER.  Line 20 is the first example of nested transfer
  682. procedures which must be done because FLOAT can only be used
  683. with cardinal for an argument.  (Keep in mind that Modula-2
  684. is changing such that integer is the preferred type, and some
  685. of this may change.)
  686.  
  687. The expression in line 22 is an error because the procedure
  688. named TRUNC results in a cardinal which cannot be added to an
  689. integer type variable.  Either of the next two lines fix the
  690. problem by making the addition type-compatible then making use
  691. of the assignment compatibility between integer and cardinal
  692. for line number 23.  The same error occurs in line 26 and is
  693. fixed the same way in either of the next two lines.  Once
  694. again, in line 31, the incompatible type error occurs and is
  695. fixed in either of two ways in the next two lines.
  696.  
  697. Lines 35 and 36 illustrate converting char data to first
  698. cardinal then real which requires nested transfer procedure
  699. calls.  The last line of the program is a nest of procedures
  700. which converts a character from char to cardinal, then to
  701. real, back to cardinal, and finally back to the original char
  702. variable.  It does nothing except act as a good illustration
  703. to you of what can be done.
  704.  
  705.                                                           3-12
  706.  
  707.                              Chapter 3 - The Simple Data Types
  708.  
  709. TYPE COERCION OR TYPE CONVERSION
  710. ______________________________________________________________
  711.  
  712. All examples in this example program use type conversion
  713. except for the INTEGER and CARDINAL conversions, which are
  714. actually type coercions.  A type conversion is a call to a
  715. function which actually modifies the bit pattern to conform
  716. to the new type, whereas a type transformation, also called
  717. a type coercion, simply copies the bit pattern from one type
  718. to the other with no regard for the meaning of the bits.
  719. Check your compiler documentation for a complete list of type
  720. conversions.  The definition of Modula-2 requires that CHR,
  721. FLOAT, ORD, TRUNC, and VAL be available.
  722.  
  723. A type transformation requires that the two types involved use
  724. the same amount of storage.  This implies that you must have
  725. some knowledge of the machine representation of the types.
  726. The only type transformations you should make at this point
  727. are between integer and cardinal types, since it would be
  728. difficult for a compiler writer to use a different bit pattern
  729. for each of these types.  More will be said about this later.
  730. Until then, you should use type transformations cautiously,
  731. if at all.
  732.  
  733. Conversion between types is very important.  You will use
  734. these techniques often so it is important to know how they
  735. work.  A very simple yet helpful memory aid is to remember
  736. that any simple type can be converted to cardinal and cardinal
  737. can be converted to any type.  Most other conversions require
  738. two steps to get from one to the other.  Remember that the
  739. latest update to Modula-2 is changing this.  Don't worry about
  740. this change too much, since you may not even notice the change
  741. as you program.  Compiler writers will try to keep updates
  742. compatible with older versions and will inform you of any
  743. changes.
  744.  
  745. Chapter 14 will readdress this topic with even more extensive
  746. type transfer procedures.
  747.  
  748.  
  749. PROGRAMMING EXERCISES
  750. ______________________________________________________________
  751.  
  752. 1.   Write a program in which you define, using the char type
  753.      variable, the letters "a" and "z", and the numbers "0"
  754.      and "9".  Convert them to cardinal, and display the four
  755.      characters and their ASCII (numerical) values.
  756.  
  757. 2.   Write a program that you can easily modify to experiment
  758.      with conversions between the various types that result
  759.      in incorrect conversions to see the results on your
  760.      compiler.  For example, convert a -1 as an integer to a
  761.      cardinal.
  762.                                                           3-13
  763.  
  764.