Ada is a multi-paradigm language with support for object orientation and some elements of functional programming, but its core is a simple, coherent procedural/imperative language akin to C or Pascal.
In other languages
One important distinction between Ada and a language like C is that statements and expressions are very clearly distinguished. In Ada, if you try to use an expression where a statement is required then your program will fail to compile. This rule supports a useful stylistic principle: expressions are intended to deliver values, not to have side effects. It can also prevent some programming errors, such as mistakenly using the equality operator "=" instead of the assignment operation ":=" in an assignment statement.
Here's a very simple imperative Ada program:
which we'll assume is in the source file
If you compile that source with the GNAT compiler and run the executable, you will get an unsurprising result.
$ gprbuild greet.adb using project file [...]_default.gpr Compile [Ada] greet.adb Bind [gprbind] greet.bexch [Ada] greet.ali Link [link] greet.adb $ ./greet Hello, World! $
There are several noteworthy things in the above program:
A subprogram in Ada can be either a procedure or a function. A procedure, as illustrated above, does not return a value when called.
withis used to reference external modules that are needed in the procedure. This is similar to
importin various languages or roughly similar to
#includein C and C++. We'll see later how they work in detail. Here, we are requesting a standard library module, the
Ada.Text_IOpackage, which contains a procedure to print text on the screen:
Greetis a procedure, and the main entry point for our first program. Unlike in C or C++, it can be named anything you prefer. The builder will determine the entry point. In our simple example, gprbuild, GNAT's builder, will use the file you passed as parameter.
Put_Lineis a procedure, just like
Greet, except it is declared in the
Ada.Text_IOmodule. It is the Ada equivalent of C's
Comments start with
--and go to the end of the line. There is no multi-line comment syntax, that is, it is not possible to start a comment in one line and continue it in the next line. The only way to create multiple lines of comments in Ada is by using
--on each line. For example:
-- We start a comment in this line... -- and we continue on the second line...
In other languages
Procedures are similar to functions in C or C++ that return
We'll see later how to declare functions in Ada.
Here is a minor variant of the "Hello, World" example:
This version utilizes an Ada feature known as a
use clause, which has
use package-name. As illustrated by the call on
Put_Line, the effect is that entities from the named package can be
referenced directly, without the package-name. prefix.
Imperative language - If/Then/Else¶
This section describes Ada's
if statement and introduces several other
fundamental language facilities including integer I/O, data declarations,
and subprogram parameter modes.
if statement is pretty unsurprising in form and function:
if statement minimally consists of the reserved word
condition (which must be a Boolean value), the reserved word
then and a
non-empty sequence of statements (the
then part) which is executed if the
condition evaluates to True, and a terminating
This example declares an integer variable N, prompts the user for an integer, checks if the value is positive and, if so, displays the integer's value followed by the string " is a positive number". If the value is not positive, the procedure does not display any output.
The type Integer is a predefined signed type, and its range depends on the computer architecture. On typical current processors Integer is 32-bit signed.
The example illustrates some of the basic functionality for integer input-output.
The relevant subprograms are in the predefined package
Ada.Integer_Text_IO and include the
Get procedure (which reads in
a number from the keyboard) and the
Put procedure (which displays an
Here's a slight variation on the example, which illustrates an
In this example, if the input value is not positive then the program displays the value followed by the String " is not a positive number".
Our final variation illustrates an
if statement with
This example expects the user to input an integer between 0 and 360
inclusive, and displays which quadrant or axis the value corresponds
in operator in Ada tests whether a scalar value is
within a specified range and returns a Boolean result.
The effect of the program should be self-explanatory; later we'll see an
alternative and more efficient style to accomplish the same effect,
elsif keyword differs from C or
C++, where nested
else .. if blocks would be used instead.
And another difference is the presence of the
end if in Ada,
which avoids the problem known as the "dangling else".
Imperative language - Loops¶
The first kind of loop is the
for loop, which allows iteration through a
Executing this procedure yields the following output:
Hello, World! 1 Hello, World! 2 Hello, World! 3 Hello, World! 4 Hello, World! 5
A few things to note:
1 .. 5is a discrete range, from
The loop parameter
I(the name is arbitrary) in the body of the loop has a value within this range.
Iis local to the loop, so you cannot refer to
Ioutside the loop.
Although the value of
Iis incremented at each iteration, from the program's perspective it is constant. An attempt to modify its value is illegal; the compiler would reject the program.
Integer'Image is a function that takes an Integer and converts it to a String. It is an example of a language construct known as an attribute, indicated by the
'syntax, which will be covered in more detail later.
&symbol is the concatenation operator for String values
end loopmarks the end of the loop
The "step" of the loop is limited to 1 (forward direction) and -1 (backward).
To iterate backwards over a range, use the
Executing this procedure yields the following output:
Hello, World! 5 Hello, World! 4 Hello, World! 3 Hello, World! 2 Hello, World! 1
The bounds of a
for loop may be computed at run-time; they
are evaluated once, before the loop body is executed. If the value of the
upper bound is less than the value of the lower bound, then the
loop is not executed at all. This is the case also for
Thus no output is produced in the following example:
for loop is more general than what we illustrated here;
more on that later.
The simplest loop in Ada is the bare loop, which forms the foundation of the other kinds of Ada loops.
This example has the same effect as
Greet_5a shown earlier.
It illustrates several concepts:
We have declared a variable named
begin. This constitutes a declarative region. Ada clearly separates the declarative region from the statement part of a subprogram. A declaration can appear in a declarative region but is not allowed as a statement.
The bare loop statement is introduced by the keyword
loopon its own and, like every kind of loop statement, is terminated by the combination of keywords
end loop. On its own, it is an infinite loop. You can break out of it with an
The syntax for assignment is
:=, and the one for equality is
=. There is no way to confuse them, because as previously noted, in Ada, statements and expressions are distinct, and expressions are not valid statements.
The last kind of loop in Ada is the
The condition is evaluated before each iteration. If the result is false, then the loop is terminated.
This program has the same effect as the previous examples.
In other languages
Note that Ada has different semantics than C-based languages with respect
to the condition in a while loop. In Ada the condition has to be a Boolean
value or the compiler will reject the program; the condition is not an
integer that is treated as either
False depending on
whether it is non-zero or zero.
Imperative language - Case statement¶
case statement is similar to the C and C++
but with some important differences.
Here's an example, a variation of a program that was shown earlier
This program repeatedly prompts for an integer value and then, if the value is in the range 0..360, displays the associated quadrant or axis. If the value is an Integer outside this range, the loop (and the program) terminate after outputting a farewell message.
The effect of the case statement is similar to the if statement in an earlier example, but the case statement can be more efficient because it does not involve multiple range tests.
Notable points about Ada's case statement:
The case expression (here the variable N) must be of a discrete type, i.e. either an integer type or an enumeration type. Discrete types will be covered in more detail later discrete types.
Every possible value for the case expression needs to be covered by a unique branch of the case statement. This will be checked at compile time.
A branch can specify a single value, such as
0; a range of values, such as
1 .. 89; or any combination of the two (separated by a |).
As a special case, an optional final branch can specify
others, which covers all values not included in the earlier branches.
Execution consists of the evaluation of the case expression and then a transfer of control to the statement sequence in the unique branch that covers that value.
When execution of the statements in the selected branch has completed, control resumes after the
end case. Unlike C, execution does not fall through to the next branch. So Ada doesn't need (and doesn't have) a
Imperative language - Declarative regions¶
As mentioned earlier, Ada draws a clear syntactic separation between declarations, which introduce names for entities that will be used in the program, and statements, which perform the processing. The areas in the program where declarations may appear are known as declarative regions.
In any subprogram, the section between the
is and the
begin is a
declarative region. You can have variables, constants, types, inner subprograms,
and other entities there.
We've briefly mentioned variable declarations in previous subsection. Let's look
at a simple example, where we declare an integer variable
X in the
declarative region and perform an initialization and an addition on it:
Let's look at an example of a nested procedure:
A declaration cannot appear as a statement. If you need to declare a local variable amidst the statements, you can introduce a new declarative region with a block statement:
The Get_Line function allows you to receive input from the user, and get
the result as a string. It is more or less equivalent to the
It returns a String, which, as we will see later, is an
Unconstrained array type. For now we
simply note that, if you wish to declare a
String variable and do
not know its size in advance, then you need to initialize the variable
during its declaration.
Imperative language - conditional expressions¶
Ada 2012 introduced an expression analog for conditional statements
Here's an alternative version of an example we saw earlier; the
statement has been replaced by an
if expression evaluates to one of the two Strings depending
on N, and assigns that value to the local variable S.
if expressions are similar to
if statements. However,
there are a few differences that stem from the fact that it is an expression:
All branches' expressions must be of the same type
It must be surrounded by parentheses if the surrounding expression does not already contain them
elsebranch is mandatory unless the expression following
thenhas a Boolean value. In that case an
elsebranch is optional and, if not present, defaults to
Here's another example:
This program produces 10 lines of output, alternating between "Odd" and "Even".
if expressions, Ada also has
They work just as you would expect.
This program has the same effect as the preceding example.
The syntax differs from
case statements, with branches separated