Ada Programming/All Operators

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Augusta Ada King, Countess of Lovelace.
Augusta Ada King, Countess of Lovelace.

Contents

[edit] Operators

[edit] Standard operators

Ada allows operator overloading for all standard operators and so the following summaries can only describe the suggested standard operations for each operator. It is quite possible to misuse any standard operator to perform something unusual.

Each operator is either a keyword or a delimiter -- hence all operator pages are redirects to the appropiate keyword or delimiter.

The list is sorted from lowest precedence to highest precedence.

[edit] Logical operators

and 
and x \land y, (also keyword and)
or 
or x \lor y, (also keyword or)
xor 
exclusive or (x \land \bar{y}) \lor (\bar{x} \land y), (also keyword xor)

[edit] Relational operators

/= 
Not Equal x \ne y, (also special character /=)
= 
Equal x = y, (also special character =)
< 
Less than x < y, (also special character <)
<= 
Less than or equal to (x \le y), (also special character <=)
> 
Greater than (x > y), (also special character >)
>= 
Greater than or equal to (x \ge y), (also special character >=)

[edit] Binary adding operators

+ 
Add x + y, (also special character +)
- 
Subtract x - y, (also special character -)
& 
Concatenate , x & y, (also special character &)

[edit] Unary adding operators

+ 
Plus sign + x, (also special character +)
- 
Minus sign - x, (also special character -)

[edit] Multiplying operator

* 
Multiply, x \times y, (also special character *)
/ 
Divide x / y, (also special character /)
mod 
modulus (also keyword mod)
rem 
remainder (also keyword rem)

[edit] Highest precedence operator

** 
Power xy, (also special character **)
not 
logical not \lnot x, (also keyword not)
abs 
absolute value | x | (also keyword abs)

[edit] Shortcut operators

The shortcut operators cannot be overloaded.

and then 
e.g. if Y /= 0 and then X/Y > Limit then ...
or else 
e.g. if Ptr = null or else Ptr.I = 0 then ...

[edit] Membership tests

The Membership Tests also cannot be overloaded.

in 
element of, var \in type, e.g. if I in Positive then, (also keyword in)
not in 
not element of, var \notin type, e.g. if I not in Positive then, (also keywords not in)

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual

[edit] Ada Quality and Style Guide


Ada Operators
and and then > + abs &
or or else >= - mod
xor = < * rem in
not /= <= ** / not in


[edit] Operators: &

[edit] As operator

[edit] Concatenating arrays

function "&" (Left, Right : T) return T;

Any array type (including fixed Strings) can be concatenated using the & operator. You can also append a single element to an array.

[edit] Common non-standard operations

[edit] Concatenating strings

The & operator is also defined for Bounded_String and Unbounded_String.

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual


[edit] Operators: **

[edit] Operator

[edit] Standard Operations

[edit] Arithmetic Power of

The "**" operator is defined as arithmetic power of for all numeric types.

function "**" (Left : T; Right : Integer) return T;

[edit] Usage
A : constant Float   := 5.0 ** 2;  -- A is now 25.0
B : constant Integer := 5 ** 2;    -- B is also 25

[edit] Working Example
File: operator_power.adb (view, plain text, download page, browse all)
with Ada.Text_IO;

procedure Operator_Power is
   A : constant Float   := 5.0 ** 2;  -- A is now 25.0
   B : constant Integer := 5 ** 2;    -- B is also 25

   package T_IO renames Ada.Text_IO;
   package F_IO is new  Ada.Text_IO.Float_IO (Float);
   package I_IO is new  Ada.Text_IO.Integer_IO (Integer);

begin
   T_IO.Put ("A = ");
   F_IO.Put (
      Item => A,
      Fore => 3,
      Aft  => 1,
      Exp  => 0);
   T_IO.New_Line;
   T_IO.Put ("B = ");
   I_IO.Put (
      Item  => B,
      Width => 3,
      Base  => 10);
   T_IO.New_Line;
end Operator_Power;

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual


[edit] Operators: *

[edit] Operator

[edit] Standard Operations

[edit] Arithmetic Multiplication

The "*" operator is defined as arithmetic multiplication for all numeric types.

function "*" (Left, Right : T) return T;

[edit] Usage
A : constant Float   := 5.0 * 2.0;  -- A is now 10.0
B : constant Integer := 5 * 2;      -- B is also 10

[edit] Working Example
File: operator_multiply.adb (view, plain text, download page, browse all)
with Ada.Text_IO;

procedure Operator_Multiply is
   A : constant Float   := 5.0 * 2.0;  -- A is now 10.0
   B : constant Integer := 5 * 2;      -- B is also 10

   package T_IO renames Ada.Text_IO;
   package F_IO is new  Ada.Text_IO.Float_IO (Float);
   package I_IO is new  Ada.Text_IO.Integer_IO (Integer);

begin
   T_IO.Put ("A = ");
   F_IO.Put (
      Item => A,
      Fore => 3,
      Aft  => 1,
      Exp  => 0);
   T_IO.New_Line;
   T_IO.Put ("B = ");
   I_IO.Put (
      Item  => B,
      Width => 3,
      Base  => 10);
   T_IO.New_Line;
end Operator_Multiply;

[edit] Common Non-Standard Operations

[edit] Character replication

A String is created where a single character is replicated n-times.

function "*" (Left : Natural; Right : Character) return String;

In addition to standard Strings this operator is also defined for Bounded_String and Unbounded_String.

[edit] Usage
A : constant String := 10 * 'X';  -- A is filled with 10 X

[edit] Working Example

The character replication operator is part of the Ada.Strings.Fixed package. You need to with and use the package to make the operator visible.

File: operator_multiply_2.adb (view, plain text, download page, browse all)
with Ada.Text_IO;
with Ada.Strings.Fixed;

procedure Operator_Multiply_2 is
   use Ada.Strings.Fixed;

   A : constant String := 10 * 'X';  -- A is filled with 10 X

   package T_IO renames Ada.Text_IO;

begin
   T_IO.Put_Line ("A = " & A);
end Operator_Multiply_2;

[edit] String replication

A String is created where a source string is replicated n-times.

function "*" (Left : Natural; Right : String) return String;

In addition to standard fixed strings this operator is also defined for Bounded_String and Unbounded_String.

[edit] Usage
A : constant String := 3 * "Hello ";  -- A is filled with 3 Hello

[edit] Working Example

The string replication operator is part of the Ada.Strings.Fixed package. You need to with and use the package to make the operator visible.

File: operator_multiply_3.adb (view, plain text, download page, browse all)
with Ada.Text_IO;
with Ada.Strings.Fixed;

procedure Operator_Multiply_3 is
   use Ada.Strings.Fixed; 

   A : constant String := 3 * "Hello ";  -- A is filled with 3 Hello.

   package T_IO renames Ada.Text_IO;

begin
   T_IO.Put_Line ("A = " & A);
end Operator_Multiply_3;

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual


[edit] Operators: -

[edit] Operator

[edit] Standard Operations

[edit] Arithmetic Subtraction

The "-" operator is defined as arithmetic subtraction for all numeric types.

function "-" (Left, Right : T) return T;

[edit] Usage
A : constant Float   := 5.0 - 2.0;  -- A is now 3.0
B : constant Integer := 5 - 2;      -- B is also 3

[edit] Minus sign

The "-" unary operator is defined as arithmetic negative sign for all numeric types.

function "-" (Left : T) return T;

[edit] Usage
A : constant Float   := -5.0;
B : constant Integer := -5;
C : constant Integer := -B;  -- C is now 5

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual


[edit] Operators: /=

[edit] Operator

The operator /= compares two values on inequality. It is predefined for all non limited types. The operator will also be defined if a suitable operator = is available.

Note that in Ada the representation for this operator was chosen for resembling the mathematical symbol \ne, in the same way than <= resembles \le, or >= resembles \ge.

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual


[edit] Operators: /

[edit] Operator

[edit] Standard operations

[edit] Arithmetic division

The "/" operator is defined as arithmetic division for all numeric types.

function "/" (Left, Right : T) return T;

[edit] Usage
A : constant Float   := 5.0 / 2.0;  -- A is now 2.5
B : constant Integer := 5 / 2;      -- B is also 2

[edit] See also

[edit] Wikibook

[edit] Ada Reference Manual


[edit] Operators: =

[edit] Operator

The operator = compares two values on equality. It is predefined for all non limited types.

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual


[edit] Operators: abs

This keyword is used for the operator that gets the absolute value of an integer number.

y := abs x;

[edit] See also

[edit] Wikibook

[edit] Ada 95 Reference Manual

[edit] Ada 2005 Reference Manual

[