18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

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.

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

It is common to indent in some regular way the statements that are to be repeated within the control<br />

structure of a do loop (as for the statement groups in an if statement) drawing attention to the departure<br />

from normal sequential flow of control.<br />

The following program uses a number of terms from an infinite series to estimate an approximate<br />

value for the irrational number PI. The value of PI, approximately 3.14159..., can be represented<br />

by the infinite series:<br />

PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 ...<br />

The accuracy of the approximation is controlled by a variable read from input data. This data value<br />

sets a limiting value on the size of the divisor in the terms of the series. The summation is halted<br />

when the values of subsequent terms become too small to affect the desired accuracy of the result.<br />

For example:<br />

read LIMIT<br />

let VALUE = 0<br />

let SIGN = +1<br />

for DIVISOR = 1 to LIMIT by 2<br />

do<br />

add SIGN/DIVISOR to VALUE<br />

let SIGN = -SIGN<br />

loop<br />

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

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

stop<br />

The variable DIVISOR takes the values 1,3,5,... until it exceeds the limiting value. Note the<br />

use of the unary operators to control the sign of successive terms.<br />

1.15 Logical Control Phrases<br />

Normally, a do loop, controlled by a for statement, is executed over the entire range of values of<br />

the control variable generated. An alternative is to control the repetition of statements using some<br />

logical condition that may be evaluated during the execution of the controlled statements.<br />

A logical control phrase contains a logical control operator and a logical expression and may be<br />

used to control a do loop. The logical control phrases are:<br />

while logical expression<br />

and<br />

until logical expression<br />

The following example illustrates the use of a logical control phrase in a program:<br />

22

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

Saved successfully!

Ooh no, something went wrong!