Ada Programming/All Operators
From Wikibooks, the open-content textbooks collection
[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
[edit] Relational operators
- /=
- Not Equal
, (also special character /=) - =
- Equal x = y, (also special character =)
- <
- Less than x < y, (also special character <)
- <=
- Less than or equal to (
), (also special character <=) - >
- Greater than (x > y), (also special character >)
- >=
- Greater than or equal to (
), (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
[edit] Multiplying operator
- *
- Multiply,
, (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
, (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,
, e.g. if I in Positive then, (also keyword in) - not in
- not element of,
, 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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
[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
- 4.4 Expressions (Annotated)
- 4.5.3 Binary Adding Operators (Annotated)
- A.4.4 Bounded-Length String Handling (Annotated)
- A.4.5 Unbounded-Length String Handling (Annotated)
[edit] Ada 2005 Reference Manual
- 4.4 Expressions (Annotated)
- 4.5.3 Binary Adding Operators (Annotated)
- A.4.4 Bounded-Length String Handling (Annotated)
- A.4.5 Unbounded-Length String Handling (Annotated)
[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
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
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.
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.
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
- 4.4 Expressions (Annotated)
- 4.5.5 Multiplying Operators (Annotated)
- A.4.3 Fixed-Length String Handling (Annotated)
- A.4.4 Bounded-Length String Handling (Annotated)
- A.4.5 Unbounded-Length String Handling (Annotated)
[edit] Ada 2005 Reference Manual
- 4.4 Expressions (Annotated)
- 4.5.5 Multiplying Operators (Annotated)
- A.4.3 Fixed-Length String Handling (Annotated)
- A.4.4 Bounded-Length String Handling (Annotated)
- A.4.5 Unbounded-Length String Handling (Annotated)
[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
- Ada Programming
- Ada Programming/Delimiters
- Ada Programming/Operators
- Ada Programming/Mathematical calculations
[edit] Ada 95 Reference Manual
- 4.4 Expressions (Annotated)
- 4.5.3 Binary Adding Operators (Annotated)
- 4.5.4 Unary Adding Operators (Annotated)
[edit] Ada 2005 Reference Manual
- 4.4 Expressions (Annotated)
- 4.5.3 Binary Adding Operators (Annotated)
- 4.5.4 Unary Adding Operators (Annotated)
[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
, in the same way than <= resembles
, or >= resembles
.
[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
- Ada Programming
- Ada Programming/Delimiters
- Ada Programming/Operators
- Ada Programming/Mathematical calculations
[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
- 2.9 Reserved Words (Annotated)
- 4.4 Expressions (Annotated)
- 4.5.6 Highest Precedence Operators (Annotated)
- Annex P (informative) Syntax Summary (Annotated)
[edit] Ada 2005 Reference Manual
- 2.9 Reserved Words (Annotated)
- 4.4 Expressions (Annotated)
- 4.5.6 Highest Precedence Operators (Annotated)
- Annex P (informative) Syntax Summary (Annotated)
, (also keyword
, (also keyword
, (also keyword