18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

32 <strong>Ada</strong> <strong>in</strong>troduction: Part 1<br />

A character variable may be declared which can hold a character from the <strong>Ada</strong> character set. The follow<strong>in</strong>g<br />

case statement would pr<strong>in</strong>t out a classification of the character held <strong>in</strong> the object Ch.<br />

Ch := 'a';<br />

case Ch is<br />

when '0' | '1' | '2' | '3' | '4' |<br />

'5' | '6' | '7' | '8' | '9' =><br />

Put("Character is a digit");<br />

when 'A' .. 'Z' =><br />

Put("Character is upper case English letter");<br />

when 'a' .. 'z' =><br />

Put("Character is lower case English letter");<br />

when others =><br />

Put("Not an English letter or digit");<br />

end case;<br />

New_L<strong>in</strong>e;<br />

In this case statement two ways of comb<strong>in</strong><strong>in</strong>g case labels are <strong>in</strong>troduced.<br />

Case component Description Explanation<br />

| Or For example, '0' | '1' will match the<br />

character '0' or the character '1'<br />

.. Range For example, 'A' .. 'Z' will match any<br />

character <strong>in</strong> the range Capital A to Capital Z.<br />

The fragment of program code comb<strong>in</strong>ed with appropriate declarations and compiled would produce when<br />

run:<br />

Character is lower case English letter<br />

3.9 Input and output<br />

In <strong>Ada</strong>, <strong>in</strong>put and output are performed by a variety of standard packages. The full implications of the package<br />

construct are discussed fully <strong>in</strong> Chapters 5 and 17 which describe <strong>in</strong> detail the I/O packages. For the moment, the<br />

discussion about <strong>in</strong>put and output will concentrate solely on character data.<br />

Text is output to the term<strong>in</strong>al us<strong>in</strong>g the put procedure. This procedure may take a formal parameter which is<br />

either a character or a character str<strong>in</strong>g. For example, to output hello the user could write either:<br />

Put( "Hello" );<br />

or<br />

Put('h'); Put('e'); Put('l'); Put('l'); Put('o');<br />

Note:<br />

A str<strong>in</strong>g is use to represent a sequence of characters. A str<strong>in</strong>g is enclosed <strong>in</strong> " " whilst a character is<br />

enclosed <strong>in</strong> ' '. In this way the compiler can dist<strong>in</strong>guish between a character 'A'and a str<strong>in</strong>g of a s<strong>in</strong>gle<br />

character "A".<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!