Ada basics
last modified November 23, 2026
This article covers the everyday parts of Ada: importing a package, declaring
values and variables, printing them, writing comments, repeating work,
making decisions, and declaring the first procedures. The examples were
compiled with GNAT 14.3.0 and are run with
gnatmake -gnat2022 -gnatwa.
The first program with use
The previous article called Ada.Text_IO.Put_Line with its full
name. A use clause makes the names declared in a package directly
visible, so the package name can be left out. The clause goes after the
with clause and before the procedure.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello is
begin
Put_Line ("Hello there!");
end Hello;
with makes the package available; use makes its
visible declarations available without a prefix. The call now reads simply
Put_Line. The procedure writes one string and a line break, and it
returns after the last statement, where end Hello; closes the
unit.
$ gnatmake -gnat2022 -gnatwa hello.adb $ ./hello Hello there!
Declarations and assignment
The declarative part between is and begin introduces
the names that the statements use. A declaration gives the type first and the
name second, and := supplies an initial value. Without a value the
variable exists but has no defined content until the program assigns one.
Assignment uses the same :=; the single = is the
equality test.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Values is
Count : Integer := 3;
Total : Integer;
begin
Total := Count * 2;
Count := Count + 1;
Put_Line ("count =" & Integer'Image (Count));
Put_Line ("total =" & Integer'Image (Total));
end Values;
Total receives the product before it is read, so the missing
initial value is harmless. Count becomes 4 after the increment,
and the two calls print the final values. Integer'Image converts
the number to a string; for a non-negative number the string begins with a
space, which is why the output has one space after the equals sign. The
ampersand & joins two strings.
$ gnatmake -gnat2022 -gnatwa values.adb $ ./values count = 4 total = 6
Comments
A comment starts with two hyphens and continues to the end of the line. It may
stand on its own line, follow a declaration, or sit between statements.
Comments do not nest, and the compiler ignores everything after
--.
-- This is a single-line comment.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Comments is
-- Declarations follow the is keyword.
Year : Integer := 1980;
begin
Year := Year + 3;
Put_Line ("Ada was standardised in" & Integer'Image (Year));
-- Put_Line ("this line is disabled");
end Comments;
The first comment describes the whole file, and the second explains the declaration that follows it. The last comment disables a call without deleting it. The assignment sets the year that the call prints. Keywords are case-insensitive, but this series writes them in lower case so that they stand apart from the identifiers.
$ gnatmake -gnat2022 -gnatwa comments.adb $ ./comments Ada was standardised in 1983
Loops
A counted loop is written for I in 1 .. 5 loop and ends with
end loop;. The two dots form a range, and the loop parameter
takes every value in it. A conditional loop uses while instead of
a range and tests its condition before each pass. The loop parameter is
read-only, and it is not visible after the loop ends.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Loops is
Sum : Integer := 0;
Next : Integer := 3;
Product : Integer := 1;
begin
for I in 1 .. 5 loop
Sum := Sum + I;
end loop;
Put_Line ("sum =" & Integer'Image (Sum));
while Next > 0 loop
Product := Product * Next;
Next := Next - 1;
end loop;
Put_Line ("product =" & Integer'Image (Product));
end Loops;
The counted loop adds 1 through 5 and produces 15. The while loop
multiplies 3, 2, and 1 and produces 6; it stops when Next reaches
zero. A semicolon follows end loop because the loop is a statement,
but no semicolon separates loop from its body.
$ gnatmake -gnat2022 -gnatwa loops.adb $ ./loops sum = 15 product = 6
Conditionals
An if statement tests a Boolean condition and runs one of several
branches. elsif tests another condition when the first is false,
else catches the remaining cases, and end if; closes
the statement. The comparison operators are =, /=,
<, <=, >, and
>=.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Decide is
begin
for Celsius in 15 .. 26 loop
if Celsius >= 24 then
Put_Line (Integer'Image (Celsius) & " C: hot");
elsif Celsius >= 20 then
Put_Line (Integer'Image (Celsius) & " C: warm");
else
Put_Line (Integer'Image (Celsius) & " C: cool");
end if;
end loop;
end Decide;
The loop supplies twelve temperatures, and the branches split them into three
groups. A temperature of 24 or more is hot, 20 to 23 is warm, and the rest is
cool. Each branch prints the temperature with Integer'Image; the
leading space in the image separates the number from the left margin.
$ gnatmake -gnat2022 -gnatwa decide.adb $ ./decide 15 C: cool 16 C: cool 17 C: cool 18 C: cool 19 C: cool 20 C: warm 21 C: warm 22 C: warm 23 C: warm 24 C: hot 25 C: hot 26 C: hot
Procedures
A procedure groups statements under a name. It can be declared in the
declarative part of another procedure, before the outer begin,
and it runs only when it is called. A parameterless procedure is called by its
name followed by a semicolon.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Procedures is
procedure Banner is
begin
Put_Line ("==================");
Put_Line (" Ada basics demo");
Put_Line ("==================");
end Banner;
begin
Banner;
Put_Line ("A procedure groups statements.");
Banner;
end Procedures;
Banner is declared inside Procedures, so it is
visible only there. The outer statements call it twice and print a sentence
between the calls. Each call writes the same three lines, and the procedure
returns to the statement after the call.
$ gnatmake -gnat2022 -gnatwa procedures.adb $ ./procedures ================== Ada basics demo ================== A procedure groups statements. ================== Ada basics demo ==================
Next steps
The program structure article explains
the declarative part in more detail, including nested declare
blocks and scope. The types article describes the
predefined types, and the variables and
constants article shows how declarations hold values over time.
Source
In this article we have imported a package with with and
use, declared and assigned variables, printed values, written
comments, looped, made decisions, and declared the first procedures. The
next article looks at the structure that
holds these pieces together.
Author
List all Ada tutorials.