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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introduction to programm<strong>in</strong>g 11<br />

Cost := Price_Per_Kilo * Kilos_Of_Apples;<br />

Put( Kilos_Of_Apples );<br />

Put( " " );<br />

Put( Cost );<br />

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

Kilos_Of_Apples := 0.2;<br />

Cost := Price_Per_Kilo * Kilos_Of_Apples;<br />

Put( Kilos_Of_Apples );<br />

Put( " " );<br />

Put( Cost );<br />

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

etc.<br />

end;<br />

Whilst this is a feasible solution, if we want to calculate the cost of 100 different weights this will <strong>in</strong>volve<br />

considerable effort and code. Even us<strong>in</strong>g copy and paste operations <strong>in</strong> an editor to lessen the typ<strong>in</strong>g effort, will<br />

still <strong>in</strong>volve considerable effort! In addition, the resultant program will be large and consume considerable<br />

resources.<br />

1.13 Introduction to the while statement<br />

In <strong>Ada</strong> <strong>95</strong> a while statement is used to repeat program statements while a condition holds true. A while<br />

statement can be likened to a rail track as illustrated <strong>in</strong> Figure 1.2. While the condition is true the flow of control<br />

is along the true track. Each time around the loop the condition is re-evaluated. Then, when the condition is found<br />

to be false, the false track is taken.<br />

Condition<br />

False<br />

True<br />

Statements<br />

Executed while<br />

condition is true<br />

Figure 1.2 The while statement as a rail track.<br />

In a while loop the condition is always tested first. Due to this requirement if the condition <strong>in</strong>itially evaluates<br />

to false then the code associated with the while loop will never be executed.<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!