Ada Programming/Expressions

(cleanup in progress)

## Expressions

The LRM defines an expression as a formula that defines the computation or retrieval of a value..[1] 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 `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:

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

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 Expressions

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:

```  X in 1 .. 10 | 12
Shape in 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 := (if Is_Circular then Pi * Radius**2 else Length * Height);
```

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.

### References

This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

Manage research, learning and skills at IT1me. Create an account using LinkedIn to manage and organize your IT knowledge. IT1me works like a shopping cart for information -- helping you to save, discuss and share.