Ada Programming/Expressions< Ada Programming
||A Wikibookian has nominated this page for cleanup.
You can help make it better. Please review any relevant discussion.
(cleanup in progress)
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;
Length * Height 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
Length by the one named
Height. Using the same expression as part of a bigger expression is demonstrated in the following example:
The bigger expression starts with
Cost and ends with
Dollar. 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 ExpressionsEdit
Among basic expressions are literals, for example, decimal (real), enumeration, string, and access value literals:
2.5e+3 False "и" null
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 => new Nickname'("Herbert"))
Age is associated with the value of a nested function call. The actual parameter for
Tiles has type name
Grid qualify the array aggregate following it; the component
Name is associated with an allocator.
The well known ‘mathematical’ expressions have closely corresponding simple expressions in Ada syntax, for example
2.0*π*r, or the relation
Area = π*r**2
Other expressions test for membership in a range, or in a type:
Conditional Expressions, Quantified ExpressionsEdit
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:
In this example, the value of
Is_Circular determines the part of the expression that is used for computing the value of the entire expression. A similar construct exists for case and also one for for. These kinds of expressions are frequently used in assertions, like in the conditions of contract based programming.