18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

<strong>SIMSCRIPT</strong> <strong>II.5</strong> <strong>Programming</strong> <strong>Language</strong><br />

those values in the sequence, however, for which the logical expression in the with phrase is true<br />

(i.e., all values but zero) are passed on to the statements in the do loop.<br />

Sequences of with, unless, while, and until phrases can be attached to for phrases in any<br />

combination. More than one of each type of phrase is permitted. While and until control phrases<br />

may also be modified by with and unless phrases, and may be nested with other independent<br />

while and until phrases to control statement repetition. Again, the use of such combinations of<br />

control phrases will become more apparent in later examples.<br />

1.16 Altering the Flow of Control Within a Loop<br />

There are occasions when it may be necessary to exercise more explicit direction on the flow of control<br />

than may readily be specified using the control structures discussed so far. Consider the previous<br />

example:<br />

for COUNT = 1 to NUMBER.OF.SEATS,<br />

while TOTAL.WEIGHT < LIMIT<br />

do<br />

read PASSENGER.NO, FARE, and BAGGAGE.WEIGHT<br />

add BAGGAGE.WEIGHT to TOTAL.WEIGHT<br />

loop<br />

Examination of the statements within the loop shows that if the loop is terminated by the while<br />

phrase, the weight of the last passenger's baggage must have overflowed the LIMIT value. This<br />

may not be quite what was intended. The problem arises because the test of the condition is made<br />

only at the beginning of the loop, and all the statements in the loop are executed at each iteration.<br />

There are occasions when the test should logically be made within the loop. The following example<br />

uses a leave statement to transfer control out of the loop when the LIMIT is about to be exceeded:<br />

for COUNT = 1 to NUMBER.OF.SEATS<br />

do<br />

read PASSENGER.NO, FARE, and BAGGAGE.WEIGHT<br />

if TOTAL.WEIGHT + BAGGAGE.WEIGHT > LIMIT<br />

leave<br />

otherwise<br />

add BAGGAGE.WEIGHT to TOTAL.WEIGHT<br />

loop<br />

let LOAD.FACTOR = (COUNT-1)/NUMBER.OF.SEATS<br />

.<br />

.<br />

The leave statement, which may only be used within a do loop, causes the flow of control to pass<br />

to the statement immediately following the loop statement that delimits the do loop. This addition<br />

to the construction of a do loop proves useful in those cases where the terminating condition is dependent<br />

on some of the actions performed within the loop. Note the use of otherwise in this example.<br />

This usage is discussed in the next paragraph. Note also that the value of the control phrase<br />

26

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

Saved successfully!

Ooh no, something went wrong!