ZetCode

Ada operators and expressions

last modified November 23, 2026

An expression combines values with operators to produce a new value. Ada groups its operators into arithmetic, relational, logical, and membership families, and the same strong typing that governs declarations also governs expressions: an operator accepts operands of one type and produces a result of a defined type. This article covers the operators of the language, their precedence, and the two Boolean operators that evaluate their right operand only when it is needed.

Arithmetic operators

The binary operators +, -, *, and / work on numeric types. Multiplication and division bind more tightly than addition and subtraction, so a sum of products needs no parentheses. Division of two integers produces an integer, and the fractional part is discarded rather than rounded.

arith.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Arith is
   A : constant Integer := 17;
   B : constant Integer := 5;
begin
   Put_Line ("sum =" & Integer'Image (A + B));
   Put_Line ("difference =" & Integer'Image (A - B));
   Put_Line ("product =" & Integer'Image (A * B));
   Put_Line ("quotient =" & Integer'Image (A / B));
end Arith;

Seventeen plus five is twenty-two, and the product is eighty-five. The quotient is three, not 3.4, because both operands are Integer and the result must be an Integer as well. The same operators are available for Float, where the quotient keeps its fractional part.

$ gnatmake -gnat2022 -gnatwa arith.adb
$ ./arith
sum = 22
difference = 12
product = 85
quotient = 3

Integer division, mod and rem

Ada provides three operators for the quotient of two integers. The operator / truncates toward zero, rem gives the remainder with the sign of the dividend, and mod gives the remainder with the sign of the divisor. The distinction matters as soon as a negative value is involved; for positive operands all three behave as expected.

division.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Division is
   A : constant Integer := -17;
   B : constant Integer := 5;
begin
   Put_Line ("A / B =" & Integer'Image (A / B));
   Put_Line ("A mod B =" & Integer'Image (A mod B));
   Put_Line ("A rem B =" & Integer'Image (A rem B));
   Put_Line ("17 / (-5) =" & Integer'Image (17 / (-5)));
   Put_Line ("17 mod (-5) =" & Integer'Image (17 mod (-5)));
   Put_Line ("17 rem (-5) =" & Integer'Image (17 rem (-5)));
end Division;

A / B is -3, since -3.4 truncates toward zero. A rem B is -2, because rem keeps the sign of the dividend, while A mod B is 3, because mod keeps the sign of the divisor. With a negative divisor the two remainders change roles. In every case the identity A = (A / B) * B + (A rem B) holds, and mod satisfies the same identity with a quotient rounded toward negative infinity.

$ gnatmake -gnat2022 -gnatwa division.adb
$ ./division
A / B =-3
A mod B = 3
A rem B =-2
17 / (-5) =-3
17 mod (-5) =-3
17 rem (-5) = 2

Powers, absolute value, and unary minus

The operator ** raises a number to a power. For an integer base, the exponent must be a non-negative integer of the type Natural. The unary operators abs and - take one operand. Exponentiation binds more tightly than the minus sign, so -2 ** 2 means the negative of two squared, not the square of minus two.

power.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Power is
   Base  : constant Integer := 2;
   Value : constant Integer := Base ** 10;
   Debt  : constant Integer := -7;
begin
   Put_Line ("2 ** 10 =" & Integer'Image (Value));
   Put_Line ("2 ** 0 =" & Integer'Image (Base ** 0));
   Put_Line ("abs (-7) =" & Integer'Image (abs Debt));
   Put_Line ("-2 ** 2 =" & Integer'Image (-2 ** 2));
end Power;

Two to the tenth power is 1024, and any non-zero value raised to zero is 1. abs removes the sign, so the absolute value of -7 is 7. The last line prints -4, which confirms that the power is computed before the unary minus is applied. A minus sign placed directly after a binary operator needs parentheses, so the division example above writes 17 / (-5); without them GNAT reports parentheses required for unary minus.

$ gnatmake -gnat2022 -gnatwa power.adb
$ ./power
2 ** 10 = 1024
2 ** 0 = 1
abs (-7) = 7
-2 ** 2 =-4

Mixed types and precedence

An operator needs both operands to have the same type. When a computation involves an Integer and a Float, one of them must be converted first, and the conversion is written explicitly. A conversion from a real type to an integer type rounds to the nearest integer, so the result may differ from the truncated quotient of two integers.

mixed.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Mixed is
   A     : constant Integer := 7;
   B     : constant Integer := 2;
   Ratio : constant Float := Float (A) / Float (B);
begin
   Put_Line ("integer quotient =" & Integer'Image (A / B));
   Put_Line ("real quotient =" & Float'Image (Ratio));
   Put_Line ("rounded =" & Integer'Image (Integer (Ratio)));
   Put_Line ("precedence =" & Integer'Image (A + B * 2));
end Mixed;

The integer quotient is 3, while the real quotient is 3.5. Converting 3.5 back to Integer gives 4, because the conversion rounds rather than truncates. The last line gives 11: the multiplication runs first and the addition second, exactly as the precedence rules require. Parentheses would change the result to 18, and Ada does not guess which form was intended.

$ gnatmake -gnat2022 -gnatwa mixed.adb
$ ./mixed
integer quotient = 3
real quotient = 3.50000E+00
rounded = 4
precedence = 11

Relational operators

The six relational operators =, /=, <, <=, >, and >= compare two values of the same type and produce a Boolean. They are not restricted to numbers: characters compare by their position in the character set, and strings compare lexicographically. The result can be printed with Boolean'Image, which writes TRUE or FALSE.

relational.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Relational is
   Small : Integer := 3;
   Large : Integer := 5;
begin
   for Round in 1 .. 3 loop
      Small := Round + 2;
      Large := 6 - Round;
      Put_Line ("round" & Integer'Image (Round) &
                ": small =" & Integer'Image (Small) &
                ", large =" & Integer'Image (Large));
      Put_Line ("  =  " & Boolean'Image (Small = Large));
      Put_Line ("  /= " & Boolean'Image (Small /= Large));
      Put_Line ("  <  " & Boolean'Image (Small < Large));
      Put_Line ("  <= " & Boolean'Image (Small <= Large));
      Put_Line ("  >  " & Boolean'Image (Small > Large));
      Put_Line ("  >= " & Boolean'Image (Small >= Large));
   end loop;
end Relational;

In the second round both values are 4, so only =, <=, and >= are true. The first and third rounds show the remaining cases. A comparison never raises an error for values of the same type; it simply answers the question. Notice that /= is written with a slash rather than with an exclamation mark, and that = compares while := assigns.

$ gnatmake -gnat2022 -gnatwa relational.adb
$ ./relational
round 1: small = 3, large = 5
  =  FALSE
  /= TRUE
  <  TRUE
  <= TRUE
  >  FALSE
  >= FALSE
round 2: small = 4, large = 4
  =  TRUE
  /= FALSE
  <  FALSE
  <= TRUE
  >  FALSE
  >= TRUE
round 3: small = 5, large = 3
  =  FALSE
  /= TRUE
  <  FALSE
  <= FALSE
  >  TRUE
  >= TRUE

Boolean operators and short-circuit evaluation

The operators and, or, and xor combine two Boolean values, and not negates one. The forms and then and or else have the same meaning when both operands are evaluated, but they promise to skip the right operand when the left one already decides the result. That promise is what makes a guard such as Step /= 0 and then Count / Step > 2 safe.

logical.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Logical is
   Sunny : constant Boolean := True;
   Warm  : constant Boolean := False;
   Count : Integer := 10;
   Step  : Integer := 0;
begin
   Put_Line ("and -> " & Boolean'Image (Sunny and Warm));
   Put_Line ("or -> " & Boolean'Image (Sunny or Warm));
   Put_Line ("xor -> " & Boolean'Image (Sunny xor Warm));
   Put_Line ("not -> " & Boolean'Image (not Warm));

   for Round in 1 .. 2 loop
      Step := Round mod 2;
      Count := Count + Round;
      if Step /= 0 and then Count / Step > 2 then
         Put_Line ("round" & Integer'Image (Round) & ": quotient computed");
      else
         Put_Line ("round" & Integer'Image (Round) & ": step is zero");
      end if;
      if Step = 0 or else Count / Step > 2 then
         Put_Line ("round" & Integer'Image (Round) & ": guard passed");
      end if;
   end loop;
end Logical;

The first four lines show the truth tables: True and False is false, True or False is true, xor is true when the operands differ, and not reverses its operand. In the loop the second round sets Step to zero; the and then form never evaluates the division, so the program reports that the step is zero instead of failing. The or else form similarly stops at Step = 0. Replacing either form with plain and or or would evaluate the division in that round and raise Constraint_Error.

$ gnatmake -gnat2022 -gnatwa logical.adb
$ ./logical
and -> FALSE
or -> TRUE
xor -> TRUE
not -> TRUE
round 1: quotient computed
round 1: guard passed
round 2: step is zero
round 2: guard passed

Membership tests

The operators in and not in test whether a value lies in a range or in a subtype. The range is written with two dots and its bounds must have the type of the tested value, so the compiler checks the test itself as well as its result. A membership test is an ordinary expression and can be combined with the other Boolean operators.

membership.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Membership is
   Age  : constant Integer := 30;
   Mark : constant Character := 'B';
begin
   Put_Line ("adult -> " & Boolean'Image (Age in 18 .. 64));
   Put_Line ("child -> " & Boolean'Image (Age in 0 .. 17));
   Put_Line ("not adult -> " & Boolean'Image (Age not in 18 .. 64));
   Put_Line ("grade -> " & Boolean'Image (Mark in 'A' .. 'C'));
end Membership;

Thirty lies between 18 and 64, so the first test is true, the second is false, and the negated form is false again. The character 'B' falls in the range from 'A' to 'C', since characters compare by their position. The range bounds here are written directly; later articles use subtypes such as Natural and named ranges, which work with in in the same way.

$ gnatmake -gnat2022 -gnatwa membership.adb
$ ./membership
adult -> TRUE
child -> FALSE
not adult -> FALSE
grade -> TRUE

Next steps

The literals article explains how the values in these expressions are written, including underscores, based numbers, and quoted text. The next article, scalar types and subtypes, shows how to define new integer and enumeration types with their own ranges, which makes the compiler enforce the ranges that were only tested by hand here.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have used the arithmetic operators, distinguished /, mod, and rem, computed powers and absolute values, converted between numeric types, compared values with the relational operators, combined conditions with the Boolean operators and their short-circuit forms, and tested values for membership in a range. The previous article covers the literals that feed these expressions, and the next article defines new scalar types for them.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.