Ada Programming/Expressions

Expressions
The LRM defines an expression as a formula that defines the computation or retrieval of a value. . There are numerous forms of expression, ranging from primaries such as literals or names, to quantified expression.

An expression is typically used in an assignment, or as part of a bigger expression. The value of an expression usually involves computation. However, some expressions' values are determined at compile time; these are called static expressions. A so-called simple expression (which happens to be a term) is seen in

Area := Length * Height;

The text  has the form of an expression, and is used on the right hand side of an assignment statement. Computing the value of the expression means multiplying the value named  by the one named. Using the same expression as part of a bigger expression is demonstrated in the following example:

Cost (Paint, Area => Length * Height) > 900 * Dollar ...

The bigger expression starts with  and ends with. It features another form of expression, a relation, and places a function call and another multiplicative expression to the left and right, respectively, of the relation's relational operator. The two are called operands and the result is a Boolean expression.

Kinds of Expressions
Among basic expressions are literals, for example, decimal (real), enumeration, string, and access value literals:

2.5e+3 False "и"

Involving many of those, one can write aggregates (a primary),

(X => 0.0,   Y => 1.0,    Z => 0.0)

but arbitrarily complex sub-components are possible, too, creating an aggregate from component expressions,

(Height => 1.89 * Meter,     Age    => Guess (Picture => Images.Load (Suspects, "P2012-Aug.PNG"), Tiles  => Grid'(1 .. 3 => Scan, 4 => Skip)),    Name   =>  Nickname'("Herbert"))

is associated with the value of a nested function call. The actual parameter for  has type name   qualify the array aggregate following it; the component   is associated with an allocator.

The well known ‘mathematical’ expressions have closely corresponding simple expressions in Ada syntax, for example, or the relation

Area = π*r**2

Other expressions test for membership in a range, or in a type:

X 1 .. 10 | 12  Shape  Polygon'Class

Conditional Expressions, Quantified Expressions
New in Ada 2012, there are forms for making the value of an expression depend on the value of another expression, conditionally. They have a pair of parentheses around them and they start with a keyword, which clearly distinguishes them from other kinds of expression. For example:

Area := ( Is_Circular Pi * Radius**2  Length * Height);

In this example, the value of  determines the part of the expression that is used for computing the value of the entire expression. A similar construct exists for and also one for. These kinds of expressions are frequently used in assertions, like in the conditions of contract based programming.