home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap03.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  29.7 KB  |  707 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                         Chapter 3
  6.                                         THE INTEGER TYPE VARIABLE
  7.  
  8.  
  9. OUR FIRST INTEGER VARIABLE
  10. _________________________________________________________________
  11.  
  12. Examine the program named ONEINT.ADA for our     ================
  13. first example program with a variable.  Some        ONEINT.ADA
  14. programming languages do not require you to      ================
  15. predefine a variable before you use it.
  16. Instead, you simply begin using it and the
  17. system has some mechanism by which it creates the variable and
  18. makes it ready for your use.  Ada requires you to specifically
  19. define every variable before you can use it.  You must give the
  20. variable a name, which is any valid identifier, and you tell the
  21. compiler how you plan to use the variable by assigning a type to
  22. the variable.
  23.  
  24.  
  25. WHAT IS A TYPE?
  26. _________________________________________________________________
  27.  
  28. The type defines a set of values which the variable can have
  29. assigned to it, and it also defines a set of operations that can
  30. be performed on the variable.  Ada is a strongly typed language
  31. since it has very strict rules limiting how a variable can be used.
  32. The compiler will not give you a usable program unless you follow
  33. all of the rules very carefully.  A major part of the study of Ada
  34. involves the study of types and how to use typing as a programming
  35. aid.
  36.  
  37.  
  38. AN UNBROKEN RULE OF ADA, NEVER BROKEN
  39. _________________________________________________________________
  40.  
  41. Ada requires that anything you use must have been previously
  42. defined.  This includes variables as well as constants, procedures,
  43. functions, and all other entities.  At least one language, Pascal,
  44. makes this claim but breaks it in a few instances.  Ada never
  45. breaks this rule.
  46.  
  47.  
  48. THE with STATEMENT
  49. _________________________________________________________________
  50.  
  51. When you include the with statement, as is done in line 2 of this
  52. program, it tells the system that there are a few functions,
  53. procedures, or packages that will be required by the system in
  54. order to run this program.  The system opens up a window into the
  55. package named Text_IO which permits some checking to be done during
  56. compilation to see that you are using the functions and procedures
  57.  
  58.                                                          Page 3-1
  59.  
  60.                             Chapter 3 - The Integer Type Variable
  61.  
  62. correctly.  Because it establishes the context, it is called a
  63. context clause.  Much more will be said about this later.  For now,
  64. it would be best if you simply include the line as shown.
  65.  
  66.  
  67. THE use STATEMENT
  68. _________________________________________________________________
  69.  
  70. Even though the functions and procedures from Text_IO are available
  71. for use, the system requires that we explicitly declare the package
  72. Text_IO with each use of one of its procedures unless we include
  73. the use clause.  For now, we will simply say that including the use
  74. clause as we have done in line 3 will simplify the rest of the
  75. program.  We will completely define this operation in a later
  76. lesson.
  77.  
  78.  
  79.  
  80. THE package STATEMENT
  81. _________________________________________________________________
  82.  
  83. The statement in line 7 tells the system to instantiate a copy of
  84. the generic package named Integer_IO which is a part of the package
  85. Text_IO, use it to output INTEGER type variables, and call it
  86. Int_IO.  That is quite a mouthful and far too advanced for us to
  87. attempt to understand at this point.  Simply accept the fact that
  88. line 7 gives the system the ability to display INTEGER type
  89. variables, and line 8 makes it easier for us to use them.  Once
  90. again, we will return to these statements in a later chapter and
  91. define them completely.
  92.  
  93.  
  94.  
  95. WHAT IS AN INSTANTIATION?
  96. _________________________________________________________________
  97.  
  98. Since the word "instantiate" represents a new concept, we need a
  99. brief definition of what the word means.  A generic package as
  100. defined in Ada is not actually a usable package, but only a
  101. template for a usable package.  When we instantiate a copy of a
  102. generic package, we get a copy of the template with the undefined
  103. entities filled in and the resulting "instance" of the package is
  104. therefore usable in the program.  Much more will be said about this
  105. later.  Note that the word "instantiate" is not a reserved word,
  106. but only a word referring to an Ada operation.
  107.  
  108.  
  109.  
  110. HOW DO WE DECLARE A VARIABLE?
  111. _________________________________________________________________
  112.  
  113. Back to the program named ONEINT.ADA.  To understand the definition
  114. of a variable, examine the program at hand and specifically line
  115. 10.  This declares a variable, which we will call Index, to be of
  116.  
  117.                                                          Page 3-2
  118.  
  119.                             Chapter 3 - The Integer Type Variable
  120.  
  121. type INTEGER, therefore defining an allowable range of values which
  122. can be assigned to it.  Most microcomputers allow an INTEGER type
  123. variable to cover a range of -32768 to 32767 but the definition of
  124. Ada allows for flexibility here.  We will see a way to determine
  125. exactly what the limits are for your compiler later in this
  126. chapter.  The type also defines a number of operations which can
  127. be performed on the variable.  More will be said of that later.
  128. The type INTEGER is used to declare a scalar variable, which is a
  129. variable that can contain a single value.
  130.  
  131. The word INTEGER is not a reserved word but a predefined word which
  132. we can redefine if we so choose.  We will not be doing that for a
  133. long time since it could cause untold problems in a program.  You
  134. should know that it is possible for you to redefine this word, and
  135. many other similarly predefined words, to mean something entirely
  136. different from their predefined meanings.
  137.  
  138. In the last chapter, we said that the declarative part of the
  139. program goes between the reserved words is and begin, and you will
  140. notice that we did indeed declare the variable named Index in that
  141. area of the program.  The end result of line 10 is that we have a
  142. variable named Index which is of type INTEGER.  As yet however, it
  143. does not contain a useful value.
  144.  
  145.  
  146.  
  147. HOW DO WE USE THE VARIABLE IN A PROGRAM?
  148. _________________________________________________________________
  149.  
  150. In the last chapter we also said that the executable statements
  151. went between the begin and the end and you will see that we have
  152. some executable statements within that range, in lines 14 through
  153. 21 of this program.  In line 14 we assign the value of 23 to the
  154. variable Index which is valid to do because 23 is within the range
  155. of allowable values that can be assigned to an INTEGER type
  156. variable.
  157.  
  158.  
  159.  
  160. THE ASSIGNMENT OPERATOR
  161. _________________________________________________________________
  162.  
  163. The combination := can be read as "gets the value of".  Line 14 can
  164. then be read as, "Index gets the value of 23."  The equal sign
  165. alone is reserved for another use.  Actually, if we say that Index
  166. = 23, it is only mathematically correct if Index is never changed,
  167. but since it is a variable and will be changed, the equality is not
  168. true.
  169.  
  170. We have succeeded in assigning a value of 23 to the variable named
  171. Index and can use it in many different ways in an Ada program, but
  172. we will illustrate only very simple uses at this point.
  173.  
  174.  
  175.  
  176.                                                          Page 3-3
  177.  
  178.                             Chapter 3 - The Integer Type Variable
  179.  
  180. Line 15 instructs the system to output a line of text to the
  181. monitor and leave the cursor at the end of the line.  In line 16,
  182. we use the predefined procedure named Put to tell the system to
  183. display the value of Index, which has the value of 23, on the
  184. monitor.  The system will right justify the output in a default
  185. field width of 6 columns, because of its definition.  This Put is
  186. part of the Int_IO package we declared earlier.  The Put in line
  187. 15 is from Text_IO.  Later in this tutorial you will understand
  188. which Put is from which package and why.  Line 17 returns the
  189. cursor to the beginning of the next line.
  190.  
  191.  
  192.  
  193. OUR FIRST ARITHMETIC
  194. _________________________________________________________________
  195.  
  196. Line 18 contains our first arithmetic statement and it will do
  197. exactly what it appears to do, "Index gets the value of Index with
  198. 12 added to it."  The variable Index should now have a stored value
  199. of 23 + 12, or 35, which we verify by telling the system to display
  200. the new value of Index.  The numeral 8 in line 20 tells the system
  201. to display the value right justified in a field 8 columns wide.
  202. We will discuss the Put procedure in detail later in this tutorial.
  203. If you remember that the statements are executed sequentially, you
  204. should have no difficulty following this program.
  205.  
  206. Compile and execute this program being careful to observe that the
  207. two values of Index are displayed in fields of different widths.
  208.  
  209.  
  210.  
  211. LET'S USE LOTS OF INTEGERS NOW
  212. _________________________________________________________________
  213.  
  214. Examine the program named MOREINTS.ADA and you   ================
  215. will see an example of using many variables of     MOREINTS.ADA
  216. type INTEGER in a program.  Lines 10 and 11      ================
  217. illustrate that you can define one or more
  218. variables on a single line.  All four variables
  219. are of type INTEGER and can be assigned values within the range of
  220. integer variables as defined for your particular compiler.  Each
  221. of these variables have no value associated with them since they
  222. were created without an initial value.  The executable part of the
  223. program can assign a value to each of them.  The variable named Cat
  224. is also an INTEGER type variable but after being created, it is
  225. assigned an initial value of 12.  Likewise Dog is created and
  226. initialized to a value of -5.  It should not come as a surprise to
  227. you that the three variables in line 14 are created, and each is
  228. assigned an initial value of 1000.
  229.  
  230. According to the Ada definition, line 14 is merely a shorthand for
  231. three different lines with a single variable declaration on each
  232. line as far as the Ada compiler is concerned.  The same is true of
  233. line 11.  This is a very subtle point, and has no consequence on
  234.  
  235.                                                          Page 3-4
  236.  
  237.                             Chapter 3 - The Integer Type Variable
  238.  
  239. the program at hand, but will make a difference later when we
  240. commence the study of arrays.
  241.  
  242. NOW TO EXERCISE SOME OF THOSE VARIABLES
  243. _________________________________________________________________
  244.  
  245. Examining the executable part of the program we find the four
  246. arithmetic operations in lines 18 through 21 which should be self
  247. explanatory.  Note that integer division in line 21 results in
  248. truncation, not rounding.  The four values are displayed on the
  249. monitor in lines 22 through 25 in a format utilizing several
  250. statements per line which is simply a matter of style.
  251.  
  252. Continuing with lines 27 and 28, we have examples of more complex
  253. mathematical calculations which should be clear to you.  The order
  254. of precedence of mathematical operators is given in detail in
  255. section 4.5 of the LRM.  The order of precedence is similar to
  256. other languages and follows common sense.  A discussion of the
  257. order of precedence will be given at the end of the next chapter
  258. of this tutorial.  If you feel you need more detail now, refer to
  259. the LRM.
  260.  
  261. Lines 29 and 30 illustrate use of the mod and rem operators, each
  262. of which return the remainder which would be obtained following an
  263. integer divide operation.  They differ only in the sign when
  264. negative numbers are involved and since negative numbers are rare
  265. when using these operators, little will be said except to give a
  266. brief statement of the differences.
  267.  
  268.      mod - gets the sign of the second operator.
  269.      rem - gets the sign of the first operator.
  270.  
  271.  
  272.  
  273. TWO MORE OPERATIONS
  274. _________________________________________________________________
  275.  
  276. After displaying the results, four more values are calculated, the
  277. first being the absolute value of the variable Dog in line 36.
  278. This is not a function, it is an operation defined with the
  279. reserved word abs.  The fact that it is an operator will be very
  280. significant when we come to the portion of this tutorial that deals
  281. with overloading operators.  The abs operator returns the absolute
  282. value of the variable given to it as a parameter.
  283.  
  284. The operation in line 37 is an illustration of exponentiation of
  285. integer numbers.  Since Cat has the value of 12, this line says
  286. that Index_2 gets the value of 12 raised to the 3rd power.  The
  287. only rules for using exponentiation with integer values are, the
  288. exponent must be an integer type value, and it cannot be negative.
  289. Note that a zero value for an exponent is legal.  Line 38 is a
  290. combination of several of the previous operations, and line 39 is
  291. an illustration of unary negation.  Be sure to compile and execute
  292. this program and study the results.
  293.  
  294.                                                          Page 3-5
  295.  
  296.                             Chapter 3 - The Integer Type Variable
  297.  
  298.  
  299. HOW DO WE DECLARE A CONSTANT?
  300. _________________________________________________________________
  301.  
  302. Examine INITEX.ADA for an example of a program   ================
  303. with some INTEGER type variables and some           INITEX.ADA
  304. INTEGER type constants declared and used in it.  ================
  305. Lines 10 and 11 should be familiar to you now,
  306. but when we get to lines 13 through 16 we have
  307. a few new things to observe.  DOZEN and GROSS are INTEGER type
  308. constants because of the reserved word constant in their
  309. declaration.  The only difference between a constant and a variable
  310. is that a constant cannot be changed during execution of the
  311. program and in the present example, it would probably be silly to
  312. redefine how many elements are in a dozen.  This is one of the
  313. things Ada can do to help you if you analyze your program right,
  314. because if you ever tried to change the value of DOZEN during
  315. program execution, Ada would give you an error message and you
  316. would eliminate one bug immediately.
  317.  
  318. Notice that GROSS is defined in terms of DOZEN since the constant
  319. DOZEN is available when GROSS is initialized.  Likewise, the
  320. constant TWO is defined in terms of BIG_NO in the next two lines.
  321. It should be obvious that a constant must have an initialization
  322. value assigned to it.
  323.  
  324.  
  325.  
  326. TWO MORE DEFINITIONS
  327. _________________________________________________________________
  328.  
  329. Lines 15 and 16 contain underlines in the numeric values that the
  330. Ada compiler will simply ignore.  You can put them in wherever you
  331. please to make the numeric literals more readable for you, but you
  332. cannot put more than one underline between each digit.  The poor
  333. choice of underline locations in this example do not add to the
  334. ease of reading the numbers but illustrate the places where they
  335. can be located.  The word INTEGER has been omitted from lines 15
  336. and 16, which makes the type of these constants slightly different
  337. from the other two but we will have to learn a bit more before we
  338. can understand or appreciate the value of doing this.
  339.  
  340. Lines 20 through 28, in the executable part of the program, should
  341. be easy for you to understand on your own, so they will be left to
  342. your study.
  343.  
  344.  
  345.  
  346. DECLARING LITERAL CONSTANTS
  347. _________________________________________________________________
  348.  
  349. Lines 30 through 32 give examples of declaration of literal values
  350. for constants using the exponential notation.  The exponent can be
  351. indicated with either case of "E" and the number following it must
  352.  
  353.                                                          Page 3-6
  354.  
  355.                             Chapter 3 - The Integer Type Variable
  356.  
  357. be positive for an integer literal.  Lines 33 through 36 give
  358. examples of the use of a base other than 10.  The radix of the
  359. number is given prior to the first "#" sign, and can be any value
  360. from 2 through 16, the radix itself being given in decimal
  361. notation.  The value is given between "#" signs and can be followed
  362. by an optional exponent, the exponent being given in the defined
  363. base.  If no radix is given, base 10 is assumed as in lines 30
  364. through 32 of this example program.
  365.  
  366. The executable part of this program should be very clear to you so
  367. you can study it on your own before you compile and execute it.
  368.  
  369.  
  370.  
  371. TYPES AND SUBTYPES
  372. _________________________________________________________________
  373.  
  374. Examine the program SUBTYPES.ADA for your first  ================
  375. look at a user defined type.  We mentioned         SUBTYPES.ADA
  376. earlier that the range of a variable of type     ================
  377. INTEGER could be different on different
  378. computers or with different compilers on the
  379. same computer.  Ada gives us the ability to define our own type in
  380. such a way that it will be identical on every computer and with
  381. every Ada compiler.
  382.  
  383.  
  384.  
  385. A DIFFERENT TYPE IS INCOMPATIBLE
  386. _________________________________________________________________
  387.  
  388. Line 10 defines a new integer type which will cover the range of
  389. -10,000 to 20,000 because of the use of the reserved word range to
  390. limit the available range of values that can be assigned to a
  391. variable of this type.  Notice carefully that we called this an
  392. integer type, not a type INTEGER.  Since it is an integer type, it
  393. has all of the properties defined earlier in this chapter but a
  394. variable of this type can only be assigned a value from -10,000 to
  395. 20,000.  The actual range is given by specifying the lower and
  396. upper limits separated by two decimal points.  The limits can be
  397. of arbitrary complexity as illustrated in lines 23 and 24.
  398.  
  399. In addition, we have actually defined an entirely new type, and
  400. since Ada does very strong type checking, it will not allow you to
  401. assign a value directly from a variable of type INTEGER to a
  402. variable of our new type, or vice versa.  The variable My_Int is
  403. defined with the new type in line 11.  We will return to
  404. consideration of this variable later.
  405.  
  406. The package declaration in line 13 is similar to the package
  407. declaration in line 7 except that it is used to tell the system how
  408. to output variables of type MY_INT_TYPE to the monitor.  We will
  409. discuss this in detail later.
  410.  
  411.  
  412.                                                          Page 3-7
  413.  
  414.                             Chapter 3 - The Integer Type Variable
  415.  
  416.  
  417. A NEW SUBTYPE IS COMPATIBLE WITH ITS PARENT
  418. _________________________________________________________________
  419.  
  420. In line 16, we define a new subtype which is of the parent type
  421. INTEGER except that it covers a limited range, then we declare a
  422. variable named Thing of the new subtype in line 17.  Any attempt
  423. to assign a value to the variable Thing which is outside of its
  424. assigned range, will result in an error.  If you were running a
  425. small company with 23 employees and you assigned them each a unique
  426. number from 1 to 23, you would be interested if the payroll program
  427. tried to generate a paycheck for employee numbered 36, for example,
  428. because there would definitely be something wrong.  A limited
  429. subrange could save you a lot of money in a case such as that.
  430.  
  431.  
  432. ASSIGNMENT MUST BE TYPE COMPATIBLE
  433. _________________________________________________________________
  434.  
  435. Anytime a value is assigned to a variable, the type assigned to it
  436. must be of its declared type or a compiler error will be generated.
  437. This will always be true in Ada and is one of the most important
  438. concepts behind its design.  This is to prevent us from making the
  439. silly little mistakes which we humans are so good at making.
  440.  
  441. The variable Count is defined as an INTEGER type variable and Stuff
  442. is defined as a limited range INTEGER also.  Remember that Thing
  443. is declared to be a subtype of INTEGER, so it is also a limited
  444. range INTEGER type variable.  Because of the way these three
  445. variables are defined, they can be freely assigned to each other
  446. as long as the values assigned are within their respective ranges.
  447. This is because they are all of their parent type of INTEGER, and
  448. various assignments among them are illustrated in lines 28 through
  449. 30.
  450.  
  451. If we tried to assign the value of Thing to My_Int, the computer
  452. would give a compile error because they are of different types, but
  453. an explicit type conversion can be used to do the assignment as
  454. illustrated in line 33.  By including the variable to convert to
  455. a new type in parentheses and preceding the parentheses with the
  456. desired type name, the system will convert the type as illustrated.
  457. The addition to Thing is completed and the entire expression inside
  458. of the parentheses is changed in type, by the explicit type
  459. conversion, to the desired type of the left side of the assignment
  460. statement.  In line 34, the type is changed to the new type before
  461. the value of 17 is added to it, which should lead you to ask a
  462. question about the type of the constant 17.
  463.  
  464.  
  465. HOW CAN 17 BE ADDED TO EITHER TYPE?
  466. _________________________________________________________________
  467.  
  468. The constant 17 is of a very special type defined by Ada as type
  469. "universal_integer" which can be combined with any of the integer
  470.  
  471.                                                          Page 3-8
  472.  
  473.                             Chapter 3 - The Integer Type Variable
  474.  
  475. types without specific conversion.  This term will be used in many
  476. ways in future lessons.  The type universal_integer is compatible
  477. with all integer types and has a range with no limits.  The range
  478. is effectively minus infinity to plus infinity.  The type
  479. universal_integer is used for all literal values, but it is not
  480. available for your use in declaring a variable.
  481.  
  482.  
  483. NOW FOR A SUBRANGE ERROR
  484. _________________________________________________________________
  485.  
  486. Lines 33 and 34 were essentially the same because they resulted in
  487. the same answer, and lines 36 and 37 would appear to be the same,
  488. but they are not.  In line 36, the value of Thing, which is 18, is
  489. converted to type MY_INT_TYPE and 10 is subtracted from it.  Both
  490. 18 and 8 are within the specified range of MY_INT_TYPE so there is
  491. no error.  In line 37 however, the result of the subtraction has
  492. the type of Thing and even the intermediate result is required to
  493. be within the range of 12 to 144.  The result of 8 is outside of
  494. the required range so a run-time error is signalled in a special
  495. way called raising an exception.  We will discuss the exception
  496. shortly but first notice that if we could get past the error, we
  497. could change the type of the result to MY_INT_TYPE and everything
  498. would work as desired.
  499.  
  500.  
  501. INTERMEDIATE RESULTS OF CALCULATIONS
  502. _________________________________________________________________
  503.  
  504. The LRM allows the limits of intermediate results to be checked
  505. against the limits of the parent type rather than the limits of the
  506. subtype.  Line 37 therefore, may not give an error indicating that
  507. the intermediate result is out of the allowable range.  This will
  508. depend on your compiler.
  509.  
  510.  
  511. WHAT IS AN EXCEPTION?
  512. _________________________________________________________________
  513.  
  514. We will have a lot to say about exceptions as we progress through
  515. this tutorial but a very brief description is needed at this time.
  516. When an Ada program is running, and a potentially disastrous error
  517. is detected, it would be good for you, the programmer, to be able
  518. to tell the system what to do with the error rather than cause a
  519. complete termination of the program.  Ada gives you that capability
  520. through the use of exception handlers that you write and include
  521. in your program.  In the above case, when the program detected the
  522. value of 8 as being out of the allowable range of that type of
  523. variable, it would signal your program that the error occurred,
  524. and if you did nothing, the Ada system would terminate the program.
  525. The proper Ada terminology for signalling you is called "raising
  526. an exception", and in the case of an out-of-bounds value, the
  527. exception named Constraint_Error would be raised.  You could then
  528. trap this error and handle it any way you choose to.
  529.  
  530.                                                          Page 3-9
  531.  
  532.                             Chapter 3 - The Integer Type Variable
  533.  
  534.  
  535. There are several different exceptions that the system can raise
  536. and you can define your own exceptions that your program can raise
  537. and respond to.  We will have a lot more to say about exceptions
  538. later in this tutorial.
  539.  
  540. A little study on your part should reveal why line 41 also has a
  541. run time error that will raise the exception Constraint_Error, if
  542. execution continues to that statement.
  543.  
  544.  
  545. ANOTHER LOOK AT THE universal_integer
  546. _________________________________________________________________
  547.  
  548. Before we leave this program we must look at lines 21 and 22 where
  549. we first define START and STOP as constants with no type
  550. indication.  These constants are therefore of type
  551. "universal_integer" and can be used in the range definitions of the
  552. next two lines even though the two lines are of different parent
  553. types.  If we had included the word INTEGER in the definitions in
  554. lines 21 and 22, they could not be used to define the limits of
  555. Example2 because it is of a different parent type.  The example
  556. program named INITEX.ADA had examples of INTEGER constants (DOZEN
  557. and GROSS) and universal_integer type constants (BIG_NO and TWO).
  558. The type universal_integer is actually a hidden type that is type
  559. compatible with all integer types.
  560.  
  561. It should also be observed that the limits of the range in these
  562. definitions are based on previously defined entities and can be of
  563. arbitrary complexity as long as they evaluate to the right types.
  564. The limits must also be within the range of the parent type or a
  565. compile error will result.
  566.  
  567.  
  568. HOW TO DEFINE TYPES AND SUBTYPES
  569. _________________________________________________________________
  570.  
  571. To declare types or subtypes, here are two simple formulas which
  572. can be followed.
  573.  
  574.      type    <type_name>    is <type_definition>;
  575.      subtype <subtype_name> is <subtype_definition>;
  576.  
  577. Note that each declaration starts with a reserved word and includes
  578. the reserved word is between the name and the definition.  It
  579. should be pointed out that Ada permits new types, subtypes, and
  580. variable declarations to be done in any order as long as everything
  581. is defined before it is used.
  582.  
  583. Compile and execute SUBTYPES.ADA and observe the exception error
  584. as reported by your runtime system.  The exception may be raised
  585. at line 37, depending on your compiler, where the value of 8 is
  586. outside of the allowable range of 12 through 144.  The error
  587. message will vary with different compilers.
  588.  
  589.                                                         Page 3-10
  590.  
  591.                             Chapter 3 - The Integer Type Variable
  592.  
  593.  
  594. Much more will be said about types and subtypes in chapter 7 of
  595. this tutorial.
  596.  
  597.  
  598. WHAT ARE ATTRIBUTES?
  599. _________________________________________________________________
  600.  
  601. Ada has a rather large list of attributes        ================
  602. available for you as a programming aid.  For an    INTATTRS.ADA
  603. example of a program that contains, and          ================
  604. therefore illustrates the use of attributes,
  605. examine the program named INTATTRS.ADA.
  606.  
  607. A new type is defined in line 10 with a limited range to illustrate
  608. that attributes are available even for user defined types.
  609.  
  610. Two additional integer types are introduced in lines 13 and 14, the
  611. NATURAL and POSITIVE types.  The POSITIVE type includes all
  612. integers greater than or equal to 1, and the NATURAL type includes
  613. all integers greater than or equal to 0.  Both of these are
  614. available with your Ada compiler, and both are subtypes of INTEGER,
  615. so all variables of these three types can be freely mixed with no
  616. type errors.
  617.  
  618. Attributes are used to gain access to various limits within the
  619. program.  For example it may be necessary to know the upper limit
  620. of a variable of some type because it is of some strange subtype.
  621. An attribute can be used to find this limit, specifically the
  622. attribute LAST as illustrated in line 31 of this program.  By
  623. combining the type name and the attribute in question with a "tick"
  624. or apostrophe, the upper limit of the range of the type is
  625. returned.  The attribute FIRST is used to find the lowest value
  626. allowed by the subrange.  The attribute SIZE gives the storage size
  627. of the type in bits of memory.  Other attributes are available for
  628. integer types.  A complete list of available attributes is given
  629. in Appendix A of the LRM.  You should spend a few minutes reviewing
  630. the list at this time even though you will understand little of
  631. what is presented there.  In the near future, you will understand
  632. most of the material included there.
  633.  
  634.  
  635. TYPE CONVERSIONS
  636. _________________________________________________________________
  637.  
  638. Note the type conversions in lines 66, 70, 72, and 75.  The values
  639. could be output directly with the BUG_RANGE type by instantiating
  640. another new copy of Text_IO.Integer_IO in a manner similar to that
  641. done in the example program named SUBTYPES.ADA.
  642.  
  643. Compile and run this program and you will get a listing of the
  644. number of bits required by your compiler to store each of the four
  645. types along with the range covered by each of the four types.
  646.  
  647.  
  648.                                                         Page 3-11
  649.  
  650.                             Chapter 3 - The Integer Type Variable
  651.  
  652.  
  653. PROGRAMMING EXERCISES
  654. _________________________________________________________________
  655.  
  656. 1.   Write a program with some types containing some constrained
  657.      limits and see what errors occur when you exceed their limits.
  658.  
  659. 2.   Try to assign some wrong type of data to some variables and
  660.      try to mix up some types in arithmetic statements.  Study the
  661.      compiler error messages.
  662.  
  663. 3.   Modify INTATTRS.ADA to output the BUG_RANGE attributes
  664.      directly by instantiating a new copy of the generic package
  665.      Text_IO.Integer_IO.  Don't spend too much time on this
  666.      exercise before you look at the answer.  It contains a new
  667.      construct, at least new to you.
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.                                                         Page 3-12