Summary
This chapter lays the foundations for the small-scale aspects of Ada. We start by considering the declaration of objects and the assignment of values to them and briefly discuss the ideas of scope and visibility. We then introduce the important concepts of type, subtype and constraints. As examples of types, the remainder of the chapter discusses the numeric types Integer and Float, enumeration types in general, the type Boolean in particular, and the operations on them.
Object declarations and assignments
Values can be stored in objects which are declared to be of a specific type. Objects are either variables, in which case their value may change (or vary) as the program executes, or they may be constants, in which case they keep their same initial value throughout their life.
A variable is introduced into the program by a declaration which consists of the name (that is, the identifier) of the variable followed by a colon and then the name of the type. This can then optionally be followed by the := symbol and an expression giving the initial value. The declaration terminates with a semicolon. Thus we might write
J: Integer;
P: Integer := 38;
The first declaration introduces the variable J of type Integer but gives it no particular initial value. The second introduces the variable P and gives it the specific initial value of 38.
We can introduce several variables at the same time in one declaration by separating them by commas thus
I, J, K: Integer;
P, Q, R: Integer := 38;
In the second case all of P, Q and R are given the initial value of 38.
If a variable is declared and not given an initial value then great care must be taken not to use the undefined value of the variable until one has been properly given to it. If a program does use the undefined value in an uninitialized variable, its behaviour will be unpredictable; the program is said to have a bounded error as described in Section 2.6.
A common way to give a value to a variable is by using an assignment statement. In this, the name of the variable is followed by := and then some expression giving the new value. The statement terminates with a semicolon. Thus
J := 36;
and
P := Q + R;
- Type
- Chapter
- Information
- Programming in Ada 2012 , pp. 73 - 100Publisher: Cambridge University PressPrint publication year: 2014