    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Ada Overview - Part 1</title>
        <link>https://members.accu.org/index.php/articles/1086</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 13, #1 - Feb 2001</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c122/">131</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-122/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+122/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Ada Overview - Part 1</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 February 2001 13:15:42 +00:00 or Sat, 03 February 2001 13:15:42 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>New computer languages appear all the time, but few achieve a
sufficient following that projects are written in them, and so
cause project managers to actively advertise for persons skilled in
their use. One of the few to achieve this status in the past ten
years is Ada.</p>
<p>And as knowledge of other tools helps you understand the
capabilities and limitations of the tools you already have, I
thought I would write this time about Ada. So that you can quickly
see what a simple Ada program looks like, let me immediately show
the usual example:</p>
<pre class="programlisting">
with ada.text_io; use ada.text_io;
procedure the_usual_example is
begin
  put_line (&quot;Hello, world.&quot;);
end the_usual_example;
</pre>
<p>This is the way I have structured this piece. I start with a bit
of the history of the language, and its purpose. I then move on to
constants and variable names, which will lead me into the
elementary types and aggregate types. From there, I discuss
statements: firstly assignment statements and then control
statements. I then talk about procedures, functions and packages.
Finally, Ada specifies a number of run time library packages, some
of which I will describe.</p>
<p>If all you know is C, please keep reading. I will point out the
similarities and dissimilarities between C and Ada as I go. I do
this because I find it easier to get my programs to work if I use
features that are the same in the various languages, rather than
those which are different. I do not want to get surprises when
debugging my programs.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e30" id="d0e30"></a>Ada's
Origins</h2>
</div>
<p>Ada is named after Augusta Ada Byron, Countess of Lovelace, who,
through her assistance to Sir Charles Babbage on his differential
engine, is regarded by many as the world's first programmer. To
celebrate this, the United States Department of Defense (DoD) chose
to name the computer language they started to design in 1974 after
her.</p>
<p>The DoD design aim was to provide a modern algorithmic language
for writing programs of a wide range of sizes running on everything
from microcomputers to mainframes. Applications were seen as
including all technical programming requirements from modelling and
simulation to real-time process control. The language had to be
simple, so as to encourage its use, but robust enough for safety
critical applications. Although designed for military, aerospace
and safety critical applications, its use in other areas was hoped
for. That this has not happened is perhaps a reflection of
programming standards generally.</p>
<p>Efficiency was a further important consideration, in terms of
reliable compilation with the then current compiler technology,
together with ease of code maintenance. Indeed, the importance of
maintenance of a large project was placed above ease of writing as
this is where the majority of software costs on a large programme
can occur.</p>
<p>The ideas of a number of languages were drawn upon for Ada's
design. Principal among these was Pascal together with a number of
unnamed research languages, which probably included C. (Initial
ideas were not drawn from C++ because Bjarne Stroustrup did not
publish his book until 1986.) The result is a block structured
language with strict typing which looks and feels more like Pascal
or Modula than C, but with operator and procedure overloading in
there too (based, I suspect, on Algol 68).</p>
<p>There have been two Ada standards published to date. The
original release, in 1983, is usually called Ada 83 and the current
standard, adopted in 1995, is Ada 95. There are some (on occasions,
significant) differences between these, and I shall mention some of
them as I proceed.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e43" id="d0e43"></a>Ada
Features</h2>
</div>
<p>Let me describe some of the features of Ada to give you a better
idea of it.</p>
<p>Perhaps the first things I should comment on are comments. Ada
uses the double minus sign, --, to introduce a comment, which is
terminated only by the end of the line. By being unable to
terminate a comment early (by */ in C), code cannot hide in a
forest of comments and comments cannot hide in a sea of code. (How
many times have you discovered a bug was due to a line not being
compiled because */ had been omitted from the comment on the
preceding line?)</p>
<p>Something Ada does not have in any form is a pre-processor (the
thing that handles the <tt class="literal">#defines</tt> in C).
Although a nice idea, the C pre-processor is generally regarded as
causing a number of problems whilst providing a small number of
facilities that might be better supplied by other means.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e55" id="d0e55"></a>Names or
Identifiers</h3>
</div>
<p>As in C, identifiers (names) are sequences of alphanumeric and
underscore characters. Unlike C, identifiers must not start with an
underscore, and upper and lower case versions of the same name are
considered to be the same. Ada 83 prohibited successive underscores
in names, but Ada 95 has removed this restriction. Identifiers may
be of any length.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e60" id="d0e60"></a>Literals</h3>
</div>
<p>Literals are the constants that appear in expressions along with
variables. Decimal literals are generally the same as in C, and
might look like 1. The underscore is also allowed in numbers, but
ignored: its purpose is to act as a digit separator. So you might
write the integer literal one million as 1_000_000 as well as
1000000 or 10_00000 (some prefer to collect digits into groups of
five...).</p>
<p>Based literals, which means numbers to bases other than 10, up
to a maximum base of 16, are provided. So <tt class=
"literal">16#e#</tt> is <tt class="literal">e</tt> in base sixteen
which is equivalent to fourteen in base ten.</p>
<p>Floating point constants are supported, together with,
interestingly, based floating-point constants. C does not have the
latter.</p>
<p>The other principal literals are characters and strings. These
look the same as in C, with a character literal being a single
character delimited by single quote marks, and a string being a
possibly zero length sequence of characters delimited by double
quote marks.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e77" id="d0e77"></a>Elementary
Types</h3>
</div>
<p>From the above, you will correctly guess that integer and
floating-point types are available, and a couple of simple
declarations might be:</p>
<pre class="programlisting">
i       : integer;
f, d    : float;
</pre>
<p>Ada tackles the problem of providing various sizes of integer
and floating-point variables differently from C. C says you get
three sorts of integers: <tt class="type">short</tt>, <tt class=
"type">int</tt> and <tt class="type">long</tt> and three
floating-point types: <tt class="type">float</tt>, <tt class=
"type">double</tt> and <tt class="type">long double</tt>.</p>
<p>Ada says that there is an integer type and a floating-point
type, and if the compiler wishes to offer various lengths of these
it may do so by prefixing <tt class="literal">long_</tt> or
<tt class="literal">short_</tt> to the elementary type name as
often as necessary to name the additional types. So you find types
like <tt class="type">short_integer</tt>, or <tt class=
"type">long_long_float</tt> depending on whose compiler you use.
So:</p>
<pre class="programlisting">
li      : long_integer;
sf      : short_float;
lf      : long_float;
</pre>
<p>What these actually are you can find out by inspecting the
attributes of the type. I discuss attributes later on.</p>
<p>Ada also offers fixed-point numbers. These are a cross between
floating point and integer types. Like an integer, the difference
between two successive numbers is always the same, but, like a
floating-point number, that difference does not have to be exactly
one. Unlike a floating-point number, the difference does not depend
on the magnitude of the number (which is the effect the exponent
has on floating point types).</p>
<p>The advantage of fixed-point numbers is speed of calculation,
for whilst fixed-point numbers look like floating point numbers to
the user, the compiler will implement them as integers, and hide
all the messy business of scaling factors from their user.</p>
<p>Fixed-point numbers would typically be used in real time
calculations for measurements like temperature, or engine speed.
Both of these range between well defined limits and would be
measured to known, fixed, resolutions.</p>
<p>Fixed-point constants look exactly like floating point
constants: it is only how the compiler generates code for them that
is different.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e130" id="d0e130"></a>Characters and
Strings</h3>
</div>
<p>Both character and string types are present, with the string
type being predeclared, rather than the &quot;do-it-yourself&quot; job of C.
Ada 83 specified the character type as being the 7-bit ASCII
character set. However, the subsequent general move towards an
8-bit character set resulted in the Ada Joint Program Office
&quot;interpreting&quot; the original standard as allowing this as well. Ada
95 was formally specified from the start as using an 8-bit
character set, and bowed to the inevitable and introduced the
16-bit <tt class="type">wide_character</tt> type at the same
time.</p>
<p>Both the character and the <tt class="type">wide_character</tt>
types are actually enumerated types in Ada, but this does not
impose any restrictions on their use.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e143" id="d0e143"></a>Other Types</h3>
</div>
<p>Enumerated types are present in Ada and the <tt class=
"type">boolean</tt> type is supplied as a predefined enumerated
type:</p>
<pre class="programlisting">
type boolean is (false, true);
</pre>
<p>What C calls pointers are catered for by the provision of an
access type so you can build dynamically allocated structures. You
can copy an access type, assign to it the value obtained from the
<tt class="literal">new</tt> reserved word which I discuss later,
assign to it the value <tt class="literal">null</tt> to indicate
that its value is otherwise invalid and (in Ada 95) assign to it
the access attribute of an existing object.</p>
<p>This list may seem a little brief to the C programmer used to
incrementing pointers to select the next element of an array, or
subtracting them to find the number of elements in an array. But
Ada would say that as such uses are unsafe and their use should be
prohibited.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e163" id="d0e163"></a>Aggregates and
Arrays</h3>
</div>
<p>An aggregate is merely a collection of objects of which the
array is perhaps the simplest example. An array is simply a
collection of objects which are all the same and exactly which
object in the collection you get is specified by the array
index.</p>
<p>Arrays in Ada may look a little confusing at first to the C
programmer. Firstly, parentheses, rather than square brackets
surround array indices. Then you can have not only multidimensional
arrays of objects, but also arrays of arrays of objects. This means
that you write things like <tt class="literal">a(1, 2)</tt> for an
element in a multidimensional array and <tt class=
"literal">b(1)(2)</tt> for an element in an array of arrays. C
supports only singly dimensioned arrays of arrays, though often
generates code as though the use is of a multidimensional
array.</p>
<p>Another feature of arrays is that, unlike C, array indices can
start at any value, rather then the value of 0 always assumed by C,
or 1 if you believe in Fortran 66. Further, an index may be a type,
in which case an array element is associated with each possible
value of the indexing type. Of course, you can only realistically
do this for types that are not very big, such as <tt class=
"type">boolean</tt>, or <tt class="type">character</tt>. But an
array <tt class="varname">c</tt>, defined as below, would be useful
for accumulating the number of times each character appeared in a
piece of text. Possible declarations for <tt class=
"varname">a</tt>, <tt class="varname">b</tt> and <tt class=
"varname">c</tt> are:</p>
<pre class="programlisting">
a       : array (0 .. 12, 0 .. 12) of boolean;
type sfb_t is array (-27 .. 10) of short_float;
b       : array (6 .. 100) of sfb_t;
c       : array (character) of long_integer;
</pre>
<p>And here you see the reason for arrays of arrays. If you gave a
reference like <tt class="literal">b(7)</tt>, the object selected
would be an array of type <tt class="type">sfb_t</tt> that contains
38 <tt class="type">short_float</tt>s.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e209" id="d0e209"></a>Aggregates:
Records</h3>
</div>
<p>The other aggregate type in Ada is the record, which is a
collection of objects that are usually of different types. C would
call these structures.</p>
<p>There is little difference between these in Ada and C and an
example of a record intended to build a binary tree is:</p>
<pre class="programlisting">
type node_type;
type node_ptr_type is access node_type;
type node_type is record
        value           : integer;
        left, right     : node_ptr_type;
end record;
node    : node_type;
this_node,
root    : node_ptr_type;
</pre>
<p>This introduces the new type <tt class="type">node_type</tt>
with an incomplete type declaration so that you can define an
access type for <tt class="type">node_type</tt>. (Do not worry if
that sounds confused, this is Ada avoiding a circular argument by
ensuring that everything is sufficiently defined before it is
used.) Once you have the types of all the variables in the record,
you can complete the declaration of the type. For completeness, I
create a variable <tt class="varname">node</tt> of the type
<tt class="type">node_type</tt>, also the variable <tt class=
"varname">root</tt>, which might access the top of a binary tree of
these nodes and <tt class="varname">this_node</tt> which could be
used to walk the tree (visit each node in the tree in turn).</p>
<p>Accessing the components of a record is similar to the approach
of C by using the &quot;.&quot; notation.</p>
<pre class="programlisting">
node.value := 7;
</pre>
<p>will set the <i class="structfield"><tt>value</tt></i> field of
<span class="structname">node</span> to 7.</p>
<p>But how are the components referenced using <tt class=
"varname">this_node</tt>, which is of the access type for
<tt class="type">node_type</tt>? C ploughs in with the <tt class=
"literal">&amp;</tt>, <tt class="literal">*</tt> and <tt class=
"literal">-&gt;</tt> operators. Ada says that the compiler knows
whether the type of the prefix, i.e. the name before the &quot;.&quot;, is
that of a record, or the access type of a record, so can insert the
appropriate de-reference instruction when needed. And so</p>
<pre class="programlisting">
this_node := this_node.left;
</pre>
<p>will move to the left sub-tree of the current node.</p>
<p>This does, however, fall down if you want to use an access type
as an actual parameter to a procedure: do you want to pass the
access type, or the object it points to? To sort this out, Ada
introduces the reserved word <tt class="literal">all</tt>, as in
<tt class="literal">this_node.all</tt>, which means pass the
object. Then <tt class="varname">this_node</tt> as a parameter
means pass the pointer to the object. This also means that a record
cannot have a component called <tt class="literal">all</tt>.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e285" id="d0e285"></a>Exceptions</h3>
</div>
<p>Ada has the concept of exceptions, which are in C++ but not C.
These provide a means for the programmer to control what happens in
the presence of hardware detected problems such as division by zero
or run time problems such as array subscript errors. Alternatively
perhaps the programmer has used a conditional statement to decide
that something has gone wrong with his code, raises an exception
himself and uses the exception mechanism to handle the problem. C++
throws and catches exceptions, Ada raises and handles them.</p>
<p>In C, a programmer making heavy use of the trigonometric
functions should frequently check the value of the <tt class=
"literal">errno</tt> macro to see if anything has detected an
error. This is somewhat tedious. In Ada, you would merely place an
exception handler at the end of the block to be protected. Then,
should one of the functions in the block detect an error, it must
raise an exception and control would transfer directly to the
handler dealing with that particular exception.</p>
<p>As a short example, suppose it is suspected that the result of
the division of <tt class="varname">y</tt> by <tt class=
"varname">z</tt> will occasionally be out of range. For <tt class=
"varname">y</tt>, <tt class="varname">z</tt> and <tt class=
"varname">r</tt> all well defined, one might write:</p>
<pre class="programlisting">
begin
    r := y / z;
exception
when constraint_error =&gt;
    put_line (&quot;Failed to calculate 'r'.&quot;);
    raise;
end;
</pre>
<p>This code forms the ratio of <tt class="varname">y</tt> and
<tt class="varname">z</tt> and places it in <tt class=
"varname">r</tt>. Ada says that should the result not fit in
<tt class="varname">r</tt>, a <tt class=
"exceptionname">constraint_error</tt> exception must be raised.
(How it does that is a problem for the compiler and the run-time
library.) If this exception is raised, the handler is taken, which
in this example prints a diagnostic message and re-raises the
exception (by the <tt class="literal">raise;</tt> statement) so
that an attempt may be made to repair the problem at a higher level
in the program.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e334" id="d0e334"></a>Attributes</h3>
</div>
<p>Another feature of Ada without direct parallel in C is that of
attributes. An attribute provides information about the type, or
object, of which it is requested. It looks like the name of the
type, or object, of interest, a single quote, and the name of the
attribute required. So <tt class="literal">integer'size</tt> is
replaced by the number of bits required by an <tt class=
"type">integer</tt> variable, which is roughly what the C
<tt class="literal">sizeof</tt> operator does. Or <tt class=
"literal">integer'last</tt> gives the largest possible value an
<tt class="type">integer</tt> can hold. That piece of information C
will tell you as <tt class="constant">INT_MAX</tt> in <tt class=
"filename">&lt;limits.h&gt;</tt>.</p>
<p>The attributes <tt class="varname">succ</tt> and <tt class=
"varname">pred</tt> allow you to step to the succeeding and
preceding values in an enumerated type.</p>
<p>Arrays always carry all their dimensions with them in Ada, even
when handed to a procedure or function as an actual argument. The
attributes <tt class="literal">first</tt> and <tt class=
"literal">last</tt> can be used to find the limits of each
dimension of an array, so there is no excuse in Ada for using the
wrong array dimension. And as looping over all values of an array
dimension is a common operation in any language, Ada also provides
an attribute <tt class="literal">range</tt> for use with the for
loop which selects each value of the dimension in turn.</p>
<p>An attribute of use to embedded systems programmers is
<tt class="literal">address</tt> which yields the address of the
object to which it is applied. Its purpose is to allow hardware
registers, such as in Direct Memory Access (DMA) controllers, to be
loaded with objects' addresses. But be warned, it is not meant as a
substitute for the access type when you want a pointer to
something.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e384" id="d0e384"></a>Overloading</h3>
</div>
<p>Overloading&quot; is where an identifier, or an operator, is given
several meanings, and the compiler has to sort out from the context
which of the possible meanings is the one to use. For example in
the expression <tt class="literal">a + b</tt> the compiler will
generally look at the types of <tt class="varname">a</tt> and
<tt class="varname">b</tt> to decide whether to emit an <tt class=
"type">integer</tt> or floating point add instruction, and even
whether one of the operands should first be converted from an
integer to a floating point type.</p>
<p>This is built in to many languages, but it is only when the
programmer can write his own definition of what <tt class=
"literal">+</tt> does that the idea becomes known as &quot;overloading&quot;.
The concept is usually extended to allow several different
procedures to have the same name. Again, the compiler has to look
at the context to decide which meaning to use. In the case of an
overloaded procedure, this would entail looking at the number and
type of all the parameters.</p>
<p>Ada has this concept, as does C++.</p>
<p>Wisely used, the concept can be a great aid to clarity. You do
not have to think up a new procedure name to do the same thing on a
new type: just use the old procedure name again. Misused, for
instance by helpfully making <tt class="literal">*</tt> do addition
and <tt class="literal">+</tt> do multiplication, causes chaos and
provides further evidence for the Project Managers who prefer to
ban it in their coding standards.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e416" id="d0e416"></a>Assignments and
Expressions</h3>
</div>
<p>Let me move on to statements, and firstly the assignment
statement.</p>
<p>Ada has only the simple assignment operator which is <tt class=
"literal">:=</tt>, so the compound assignment operators of C, like
<tt class="literal">+=</tt>, are not allowed. But the usual
arithmetic, relational and boolean operations are supplied.</p>
<p><tt class="literal">**</tt>, the exponentiation operator, comes
from Fortran and raises its left operand to the power given by the
right operand. As in Fortran, the right hand operand must be an
integer (the left hand operand may be an integer or a floating
point type) so you cannot raise a number to a fractional power.
However, you can overload the <tt class="literal">**</tt> operator,
and the interface to a suitable declaration to do this is included
in the standard library package <tt class=
"literal">ada.numerics.generic_elementary_functions</tt>.</p>
<p>Ada provides two operators to return the remainder of a division
(<tt class="literal">%</tt> in C): <tt class="literal">mod</tt> and
<tt class="literal">rem</tt>. Which one you want depends on whether
you think the remainder after dividing -11 by 5 should be +4 (use
<tt class="literal">mod</tt>) or -1 (use <tt class=
"literal">rem</tt>). (C says this is implementation defined.)</p>
<p>Ada uses <tt class="literal">&amp;</tt> to signify array
concatenation. Remembering that strings are character arrays, one
frequent use is to join two strings together: <tt class=
"literal">&quot;hello&quot; &amp; &quot;, world.&quot;</tt>. C would use the standard
library functions <tt class="function">strcat</tt> or <tt class=
"function">memcpy</tt> to do this.</p>
<p>The inequality relational operators are as you would expect:
<tt class="literal">&lt;</tt>, <tt class="literal">&lt;=</tt>,
<tt class="literal">&gt;=</tt> and <tt class="literal">&gt;</tt>.
To test for equality, Ada uses <tt class="literal">=</tt> (in C,
when did you last find an <tt class="literal">if</tt> statement
control expression was always true because you had typed = instead
of ==?) The inequality operator is <tt class="literal">/=</tt>
rather than the <tt class="literal">!=</tt> of C.</p>
<p>The boolean operators are spelt out as <tt class=
"literal">and</tt>, <tt class="literal">or</tt> and <tt class=
"literal">xor</tt>. <tt class="literal">not</tt> is used for
boolean negation (not <tt class="literal">!</tt>).</p>
<p>There are no bit operators (~, &amp; and | in C), but using the
equivalent logical operator on a packed array of <tt class=
"type">boolean</tt>s will achieve the same result.</p>
<p>Unlike C, Ada does not automatically convert between types
before evaluating operators. So in the expression <tt class=
"literal">1 + 2.3</tt>, whilst C would convert <tt class=
"literal">1</tt> to the double precision value <tt class=
"literal">1.0</tt>, Ada would generate an error during compilation.
This is to increase code reliability by forcing the programmer to
consider why he is adding an <tt class="type">integer</tt> to a
<tt class="type">float</tt>.</p>
<p>To do the necessary type conversion (casting in C), Ada provides
a function for every type, with the same name as that type, which
will convert its argument to that type. So the programmer should
re-write the above example as <tt class="literal">1 + integer
(2.3)</tt> to get the <tt class="type">integer</tt> result 3. A
floating point type is always rounded when converted to an integer,
whereas C would truncate towards zero.</p>
<p>Of course, some conversions are not possible, such as the
conversion between a <tt class="type">character</tt> and an
<tt class="type">integer</tt>, but the compiler will tell you about
that quickly enough. Because the character type is actually an
enumerated type, you need the attribute functions <tt class=
"literal">character'val</tt> to convert an <tt class=
"type">integer</tt> to the equivalent <tt class=
"type">character</tt> and <tt class="literal">character'pos</tt> to
convert a <tt class="type">character</tt> to an <tt class=
"type">integer</tt>. So <tt class="literal">character'pos
('A')</tt> will give the <tt class="type">integer</tt> value
65.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e575" id="d0e575"></a>Statements</h3>
</div>
<p>Most of the statement types available in C are present in Ada.
The most significant difference is that whilst the control
statements in C control only a simple statement, resulting in a
proliferation of braces as you struggle to make an <tt class=
"literal">if</tt> statement control several statements at once,
every control statement in Ada operates on a sequence of
statements. The consequence is that Ada needs things like
<tt class="literal">end if</tt> to signify the end of the
statements controlled by an earlier <tt class="literal">if</tt>.
But this also makes it easier for the compiler to detect where you
have omitted an <tt class="literal">end</tt>.</p>
<p>If you want a block statement, <tt class="literal">begin ..
end</tt> are available and, like C, you can commence with a list of
variable declarations which will exist only within that block. The
block may be optionally named, and that name repeated after the
<tt class="literal">end</tt>, to assist the compiler in producing
meaningful error messages should the number of <tt class=
"literal">begin</tt>s not match the number of <tt class=
"literal">end</tt>s. This feature may also be used on the loop
statement.</p>
<p>As I indicated above, an if statement is present, looking
like:</p>
<pre class="programlisting">
if control_expression then
    statement_1;
    statement_2;
else
    statement_3;
    etc;
end if;
</pre>
<p>Note that a <tt class="literal">then</tt> is needed after the
<tt class="literal">control_expression</tt> so that the compiler
knows <tt class="literal">control_expression</tt> has ended. And,
of course, the <tt class="literal">else</tt> and following
statements are optional. If you want the <tt class=
"literal">else</tt> as a place marker for further development, you
can always use the null statement of <tt class=
"literal">null;</tt>.</p>
<p>At first sight, the syntax of Ada appears to allow only one type
of loop statement, and closer scrutiny is needed to find that this
may be either of the more familiar <tt class="literal">for</tt> or
<tt class="literal">while</tt> statements. There is no equivalent
of the C <tt class="literal">do ... while</tt> construct.</p>
<p>The <tt class="literal">for</tt> statement in Ada is a little
strange in that the control variable exists only within the scope
of the <tt class="literal">for</tt> statement. So using a
<tt class="literal">for</tt> statement to sequentially search an
array for a value requires explicit thought as to how the index of
the wanted value is extracted. One ends up with something like</p>
<pre class="programlisting">
l       : integer;
x       : sfb_t;
test    : short_float;
l := x'last + 1;
for i in x'range loop
    if x(i) = test then
        l := i;
        exit;
    end if;
end loop;
</pre>
<p>If <tt class="varname">test</tt> is the value to be found, then
the <tt class="literal">if</tt> statement checks the current
element of array <tt class="varname">x</tt> and, when found, places
the index of the wanted value in <tt class="varname">l</tt> and
exits the loop. (<tt class="literal">exit</tt> does what <tt class=
"literal">break</tt> does in C, and there is no equivalent in Ada
of C's <tt class="literal">continue</tt>.) The <tt class=
"literal">for</tt> statement uses the range attribute on <tt class=
"varname">x</tt> to determine the loop limits, and also the type of
<tt class="varname">i</tt>. And immediately before the <tt class=
"literal">for</tt> statement I assign an illegal value to
<tt class="varname">l</tt> (of one more than the highest legal
index into <tt class="varname">x</tt>) so that I can subsequently
determine if the <tt class="varname">test</tt> value was ever found
in the array,</p>
<p>The multiple selection <tt class="literal">switch</tt> statement
in C is the <tt class="literal">case</tt> statement in Ada. When
the sequence of statements selected by the control variable is
completed, the Ada <tt class="literal">case</tt> statement is also
completed. Unlike C, Ada does not then start executing the
statements for the next value of the control variable, so there is
no need to worry about a missing <tt class="literal">break</tt>.
Ada also requires that statements appear which match every possible
value of the control variable. This means in practise that the
default choice of <tt class="literal">others</tt> becomes a
compulsory, rather than an optional, feature of the <tt class=
"literal">case</tt> statement.</p>
<p>To move across a chess board using commands like <tt class=
"literal">north</tt> or <tt class="literal">south</tt> by updating
the <tt class="literal">(x, y)</tt> coordinates of the selected
cell you might end up with:</p>
<pre class="programlisting">
type direction_type is (north, south, east, west, left, right);
d       : direction_type;
x, y    : integer;
case d is
when north =&gt; y := y - 1;
when south =&gt; y := y + 1;
when east  =&gt; x := x + 1;
when others =&gt; null;
end case;
</pre>
<p>which as well as using the <tt class="literal">others</tt>
choice to ensure that actions for all possible values of <tt class=
"varname">d</tt> are provided for, introduces the feature that you
cannot move west. A computer language can make it difficult to
write a bug by prohibiting known dubious practises, but it can
never prevent a determined programmer achieving a dubious goal.</p>
<p>And finally, Ada allows every statement to be named by preceding
it with the name in double angle brackets:</p>
<pre class="programlisting">
&lt;&lt;label_1&gt;&gt; a := b;
</pre>
<p>so that, when all else fails, a <tt class="literal">goto</tt>
statement may be used:</p>
<pre class="programlisting">
goto label_1;
</pre>
<p>(There are several reasons why a modern, block structured,
language must have a <tt class="literal">goto</tt> statement, but
few occasions when it may be used.)</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
