Section 4.1 - Type Integer

The next few sections will describe how to create subprograms, but we really can't talk about creating subprograms until we know a little about the Ada type Integer.

The Ada type Integer is used to store integer values if you don't care what its minimum and maximum range is -- Ada will then select whatever range is ``most natural'' for your machine. Ada guarantees that an Integer can store numbers between -32767 and 32767 (inclusive); an Integer is likely to have a wider range. In other words, an Integer type must use at least 16 bits, but the actual number of bits used will depend on the compiler and machine.

If you do care what the range of a value is, Integer isn't the right type to use. In fact, as we'll discover later, Ada has a rich collection of types and typing mechanisms to specify what you do want. Many real Ada programs don't use Integer very much - but what they do use will have to wait for a later lesson.

Normal Integer operations are available: + means add, - means subtract, * means multiply, / means (integer) division, and ** means exponentiate. The normal mathematical rules of evaluation are used, so exponentiation is done first, then multiplications and division, then addition and subtraction. Parentheses can be used to change the order or to make it clearer. Thus ``2+3*5'' is 17, and ``(2+3)*5'' is 25.

A key difference between Ada and some other languages (such as C and C++) is what happens when an evaluation cannot be completed. If a division by zero is attempted, or an expression result is too large, Ada will normally raise an exception. Exceptions can be handled, but if they aren't, the program will halt (with some debugging output to help identify the kind and location of the problem). This means that instead of silently giving wrong answers, Ada programs normally will halt when a computation cannot be completed. This simplifies debugging.

Normal Integer comparisons (which return true or false) are also available: = means ``is equal to'', > means ``greater than'', >= means ``greater than or equal to'', and so on. The ``not equal to'' operation is written as ``/='' (which looks like the mathematical symbol for `not equal'). Comparisons are considered after arithmetic operations, so ``3 + 4 > 6'' is evaluated as ``7 > 6'' (which is True).

Unlike C or C++, but like Pascal and many other languages, Integers are not considered the same as True or False. A zero and False aren't the same thing (in Ada terms they are different types). If you want to determine if a number is zero, compare it (using =) to zero. This helps to catch errors early.


Quiz:

Which of the following expressions is true?

  1. (2+3)*4 = 2+(3*4)
  2. 6/3 > 12-2
  3. 2+8 /= 28

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 4 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s4s1.htm".