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>Programming</strong> <strong>Language</strong> Concepts<br />

2.8 Nested DO Loops<br />

As shown above, a number of for statements, for instance, may be nested to provide complex control<br />

over the repetition of a do...loop. Do...loops may also be nested within one another.<br />

This proves convenient when processing variables with multiple subscripts. Consider a program<br />

fragment that computes the row and column sums of a two-dimensional matrix. The sums are accumulated<br />

in the appropriate elements of two one-dimensional arrays:<br />

for I = 1 to NROWS<br />

do<br />

for J = 1 to NCOLS<br />

do<br />

add MATRIX(I,J) to ROWSUM(I)<br />

add MATRIX(I,J) to COLSUM(J)<br />

loop<br />

loop<br />

The do...loop controlled by the phrase for I = 1 to NROWS is termed the outer loop. Recall<br />

that all the statements between the bounds of the do and the matching loop statements are repeated<br />

for each value of the control variable. Thus, the inner do loop is repeated as each row of the matrix<br />

is indexed. Clearly, the inner loop indexes, for any one row, each matrix element within that row.<br />

When do...loops are nested, each do should be paired with a matching loop as shown. Adopting<br />

a convention of indenting the controlled statements makes this clear. Where nested loops terminate<br />

on successive loop statements, however, a special construct, similar to the then if<br />

construct, may be used. When also is prefixed to a for phrase, <strong>SIMSCRIPT</strong> <strong>II.5</strong> automatically<br />

pairs the do that follows with the loop that matches the do of the preceding for statement. Using<br />

this statement, the above example can be written as:<br />

for I = 1 to NROWS<br />

do<br />

also for J = 1 to NCOLS<br />

do<br />

add MATRIX(I,J) to ROWSUM(I)<br />

add MATRIX(I,J) to COLSUM(J)<br />

loop<br />

The do statements themselves need not be immediately adjacent, but they must terminate on adjacent<br />

loop statements. Statements to be repeated only under the control of the outer loop must appear<br />

after the first do and preceding the also. Care should be taken, when using the also for<br />

construct, not to obscure the logical intent of the repetition. To this end, the example above retains<br />

the indenting of the earlier example.<br />

2.9 The Structure of a <strong>SIMSCRIPT</strong> <strong>II.5</strong> Program<br />

In the previous discussion, a program has been understood to contain a number of instruction statements,<br />

possibly preceded by some variable declaration statements in the preamble section of the<br />

51

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

Saved successfully!

Ooh no, something went wrong!