Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-5mhkq Total loading time: 0 Render date: 2024-10-07T07:24:53.200Z Has data issue: false hasContentIssue false

9 - Expression structures

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

This short chapter describes additional forms of expressions introduced in Ada 2012. These are related to the three bracketed sequential control structures of Ada described in the previous chapter. Thus, corresponding to the if statement there is the if expression and corresponding to the case statement there is the case expression. There are also quantified expressions and these are related to loop statements. We also discuss the new forms of membership tests in Ada 2012.

It is perhaps very surprising that Ada did not have conditional forms of expressions right from the beginning. Even Algol 60 had conditional expressions. However, perhaps there was a feeling that conditional expressions could lead one astray by loosening the crisp distinction between statements and expressions.

The incentive to introduce conditional expressions into Ada was triggered by the addition of pre- and postconditions which are described in Chapter 16. Without adding conditional forms of expressions these pre- and postconditions would have been cumbersome and required lots of small functions. However, these additional forms of expressions are applicable in other situations as well and it seems appropriate to describe them here.

Membership tests

Membership tests were introduced in Section 6.9 where we showed how they could be used to check whether a value lies within a specified range or satisfies a constraint implied by a subtype.

Thus given I of type Integer we could write

if I in 1.. 10 then

and given the type Day describing all the days of the week and the subtype Weekday we could test whether a variable D of the type Day had a value within the subtype by writing

if D in Weekday then

These were the only forms of membership test allowed in Ada 2005, but more general forms are permitted in Ada 2012.

Suppose we have an enumeration type for the names of months and perhaps related subtypes thus

type Month_Name is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);

subtype Spring is Month_Name range March .. May;

and wish to check whether it is safe to eat an oyster (there has to be an R in the month). In Ada 2005 we can write

if M in Jan .. April or M in Sep .. Dec then

which means repeating M and then perhaps worrying about whether to use or or or else.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2014

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×