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 />

for I = 1 to N<br />

read LIST(I)<br />

If N were the reserved dimension of the array, this statement has the same effect as the statement<br />

read LIST.<br />

A more complex example demonstrates the use of nested control phrases to govern the repetition of<br />

a do loop. The loop reads and assigns data values to only the lower triangular elements (i.e., those<br />

that lie below the diagonal) of a square two-dimensional array or matrix. For example:<br />

reserve MATRIX(*,*) as N by N<br />

for I = 1 to N<br />

for J = 1 to N<br />

do<br />

if I gt J<br />

read MATRIX(I,J)<br />

always<br />

loop<br />

While the subscripts, in turn, take on the values to address each element, the read statement is<br />

executed only when the if condition is satisfied. The data values supplied would be the values for<br />

MATRIX(2,1), MATRIX(3,1), MATRIX(3,2), MATRIX(4,1), MATRIX(4,2),<br />

MATRIX(4,3), etc.<br />

2.7 Using Subscripted Variables In Expressions<br />

Recall that parentheses are also used to clarify the desired evaluation order in complex arithmetic<br />

expressions. Subscripted variables may appear in such expressions. In these cases, the evaluation<br />

of subscripts and the selection of the array elements may be considered to take precedence over other<br />

arithmetic evaluations. Note that subscripts may themselves involve expression evaluations.<br />

Thus, in the statement:<br />

let Z = X(I + 1) * Y(J + 1)<br />

the subscript expressions I + 1 and J + 1 are evaluated first, thus selecting the two elements of<br />

arrays X and Y that are to be multiplied. The subscript expressions may even involve subscripted<br />

variables. The same procedure, applied to these expressions, ensures that these are evaluated first:<br />

let Z = X(Y(I + 1) + 1) * 2<br />

The subscript I + 1 is evaluated to select an element from Y; 1 is added to the value of this element<br />

to form the indexing value for the array X; and the selected element of X is then multiplied before<br />

assignment to Z. After conversion to integer mode (if necessary), the subscript expression should<br />

always evaluate in the range of 1 to the size given in the reserve statement. Out-of-range subscripts<br />

will produce execution errors that either terminate the program or introduce undesirable<br />

side-effects, depending on the implementation.<br />

50

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

Saved successfully!

Ooh no, something went wrong!