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> Basic Concepts<br />

while TOTAL.WEIGHT < LIMIT<br />

do<br />

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

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

add FARE to TOTAL.REVENUE<br />

loop<br />

In this example, the value of the variable TOTAL.WEIGHT is incremented for each set of passenger<br />

data read. As long as TOTAL.WEIGHT does not equal or exceed the value LIMIT, the statements in<br />

the do loop are repeated. When the logical condition becomes untrue, that is, TOTAL.WEIGHT<br />

equals or exceeds LIMIT, the repetition terminates and control transfers to the statement following<br />

loop.<br />

An until phrase is similar to a while phrase, but the terminating condition may be expressed as<br />

the logical complement of that used in the while phrase. The phrase:<br />

until TOTAL.WEIGHT >= LIMIT<br />

could be substituted for the while phrase in the above example with an identical effect. In the example<br />

in the previous section, the calculation of an approximate value for PI, the accuracy of the<br />

computation was controlled by an input data value limiting the magnitude of the divisor in included<br />

terms of the series. It might seem more straightforward to specify directly a lower bound on the<br />

size of the terms to be included in the summation. The following example uses an until phrase to<br />

control the do loop:<br />

read LOW.BOUND<br />

let VALUE = 0<br />

let SIGN = +1<br />

let TERM = 1<br />

let DIVISOR = +1<br />

until TERM less than LOW.BOUND<br />

do<br />

add (TERM * SIGN) to VALUE<br />

let DIVISOR = DIVISOR + 2<br />

let SIGN = -SIGN<br />

let TERM = 1/DIVISOR<br />

loop<br />

print 1 line with VALUE*4 as<br />

The Approximate Value of Pi is *.******<br />

stop<br />

Recalling that for control phrases may control the repetition of single statements as well as do<br />

loops, note that while and until statements may be used in the same way. Because a single statement<br />

may not, for the present, appear to offer much scope for altering the logical condition, the usefulness<br />

of this construction may not be immediately appreciated. It is mentioned here for<br />

completeness and will be expanded on in later chapters.<br />

23

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

Saved successfully!

Ooh no, something went wrong!