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.

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

3.5 Repetition: while<br />

while Count > 0 loop --loop while greater than 0<br />

-- Repeated statements<br />

end loop;<br />

The above construct repeatedly executes the statements between loop and end loop while the condition<br />

count > 0 is true.<br />

Note:<br />

The mandatory end loop term<strong>in</strong>ates the while loop. In <strong>Ada</strong>, most constructs are term<strong>in</strong>ated by<br />

a mandatory term<strong>in</strong>ation keyword(s). This prevents the k<strong>in</strong>d of errors that can occur <strong>in</strong> other<br />

languages when an extra statement is added <strong>in</strong> the belief that it forms part of the construct. It also<br />

allows the compiler to check that the user has constructed a program correctly by match<strong>in</strong>g the start<br />

and end of each construct.<br />

3.6 Selection: if<br />

if Count = 3 then<br />

Put("Ignition"); New_L<strong>in</strong>e;<br />

end if;<br />

--If 3 pr<strong>in</strong>t Ignition<br />

The if statement allows a statement or statements to be executed only if the condition is true. In the above<br />

example, the statements Put("Ignition"); New_L<strong>in</strong>e; will only be executed when count is equal to 3.<br />

Note:<br />

The mandatory end if term<strong>in</strong>ates the if statement.<br />

An else part may also be <strong>in</strong>cluded, <strong>in</strong> which case statement or statements which follow it are only obeyed if<br />

the condition is false. For example:<br />

if Count = 3 then<br />

Put("Count is 3"); New_L<strong>in</strong>e;<br />

else<br />

Put("Count is not 3"); New_L<strong>in</strong>e;<br />

end if;<br />

Note:<br />

The else part of an if statement is optional. However, if it is <strong>in</strong>cluded it must be followed by at least<br />

one statement.<br />

The rather <strong>in</strong>elegant nested if structure below:<br />

if Count = 3 then<br />

Put("Count is 3"); New_L<strong>in</strong>e;<br />

else<br />

if Count = 4 then<br />

Put("Count is 4"); New_L<strong>in</strong>e;<br />

else<br />

Put("Count is not 3 or 4"); New_L<strong>in</strong>e;<br />

end if;<br />

end if;<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!