Ada Programming/Keywords/and

Logical operator

Boolean operator

X : Boolean := A > 10 and A < 20;

Boolean shortcut operator

Shortcut operators are used to make the evaluation of parts of boolean expressions conditional: and then, or else. This should never be done to speed up the evaluation (with modern optimizing compilers, it will possibly not have that effect). The correct use is to prevent the evaluation of expressions known to raise an exception.

if Dog /= null and then G (Dog) then
   Walk (Dog);
end if;

In the example above, G (Dog) is only called when the pointer Dog is not null, i.e. it actually points to something.

Actually and then and or else are not operators in the sense of the reference manual, they are called 'Short-circuit Control Forms'. The difference is that (true) operators can be redefined (i.e. overloaded), whereas these cannot. They are however defined for any boolean type.

Since Ada allows parallel evaluation of the arguments for an expression, shortcut operators are not the standard way of evaluating boolean expressions. In case the final result of the evaluation is guaranteed to be the same, the compiler is allowed to use a shortcut evaluation.

Boolean operator on arrays

The and operator is applied to each pair of boolean elements from the left and right arrays. The result has the same bounds than the left operand.

type Day_Of_Month is range 1 .. 31;            
type Month_Array is array (Day_Of_Month) of Boolean;

X : Month_Array := Function_1;
Y : Month_Array := Function_2;
Z : Month_Array := X and Y;

Bitwise operator

The operator and could be used with modular types to perform bitwise operations.

↑Jump back a section

Adding interfaces to tagged types

This language feature is only available in Ada 2005.

type Programmer is new Person 
                   and Printable
with 
   record
     Skilled_In : Language_List;
   end record;
↑Jump back a section
Last modified on 5 January 2009, at 23:37