Skip to main content Accessibility help
×
Hostname: page-component-7bb8b95d7b-495rp Total loading time: 0 Render date: 2024-10-07T05:22:28.715Z Has data issue: false hasContentIssue false

7 - Control Structures

Published online by Cambridge University Press:  05 July 2014

Get access

Summary

This chapter describes the three bracketed sequential control structures of Ada. These are the if statement, the case statement and the loop statement. These three statements enable programs to be written with a clear flow of control without using goto statements and labels. However, for pragmatic reasons, Ada does have a goto statement and this is also described in this chapter.

The three control structures exhibit a similar bracketing style. There is an opening reserved word if, case or loop and this is matched at the end of the structure by the same reserved word preceded by end. The whole is, as usual, terminated by a semicolon. So we have

if case loop

… … …

end if; end case; end loop;

In the case of the loop statement the word loop can be preceded by an iteration scheme commencing with for or while.

Corresponding structures for expressions such as if expressions and case expressions which are introduced in Ada 2012 are described in Chapter 9.

If statements

The simplest form of if statement starts with the reserved word if followed by a Boolean expression and the reserved word then. This is then followed by a sequence of statements which will be executed if the Boolean expression turns out to be True. The end of the sequence is indicated by the closing end if. The Boolean expression can, of course, be of arbitrary complexity and the sequence of statements can be of arbitrary length.

A simple example is

if Hungry then

Cook;

Eat;

Wash_Up;

end if;

In this, Hungry is a Boolean variable and Cook, Eat and Wash_Up are various subprograms describing the details of the activities. The statement Eat; merely calls the corresponding subprogram (subprograms are dealt with in detail in Chapter 10).

The effect of this if statement is that if variable Hungry is True then we call the subprograms Cook, Eat and Wash_Up in sequence and otherwise we do nothing. In either case we then obey the statement following the if statement.

Note how we indent the statements to show the flow structure of the program. This is most important since it enables the program to be understood so much more easily. The end if should be underneath the corresponding if and the then is best placed on the same line as the if.

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.

  • Control Structures
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.012
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.

  • Control Structures
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.012
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.

  • Control Structures
  • John Barnes
  • Book: Programming in Ada 2012
  • Online publication: 05 July 2014
  • Chapter DOI: https://doi.org/10.1017/CBO9781139696616.012
Available formats
×