31.12.2013 Views

TOPS-20 PASCAL Language Manual - Bitsavers

TOPS-20 PASCAL Language Manual - Bitsavers

TOPS-20 PASCAL Language Manual - Bitsavers

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>PASCAL</strong> STATEMENTS<br />

In the DOWNTO form,<br />

than or equal to<br />

control variable is<br />

variable is less<br />

complete.<br />

if the value of the control variable is greater<br />

the final value, the loop is executed, and the<br />

decremented. When the value of the control<br />

than the final value, execution of the loop is<br />

Because completion tests are performed before the statement is<br />

executed, some loops are never executed, for example:<br />

FOR Control := N TO N + Q DO<br />

Wpek r: N ] :::~ We (::)k [N] + Newpa~:I'<br />

If the value of N + Q is less than the value of N (that is,<br />

negative), the loop is never execut,ed.<br />

if Q<br />

is<br />

When incrementing and decrementing the control variable, <strong>PASCAL</strong> uses<br />

units of the appropriate type. For numeric values, it adds or<br />

subtracts one upon each iteration. For values of other types, the<br />

control variable takes on each successive value of the type. For<br />

example, a control variable of type 'A' •. '2' is incremented (or<br />

decremented) by one character each time the loop is executed.<br />

If the FOR loop terminates normally, that is, if the loop exits upon<br />

completion and not because of a GOTO statement, the value of the<br />

control variable is left undefined. You cannot assume that it retains<br />

a value. Therefore, you must assign a new value to the control<br />

variable if you use it elsewhere in the program. However, if the FOR<br />

loop terminates because of a GOTO statement, it retains the value it<br />

had at termination; and you can use that value elsewhere.<br />

Example I<br />

FOR N := Lowbound TO HiShbound DO<br />

Sum := Sum + IntArra~[NJ;<br />

This FOR loop computes the sum of the elements of Int_luray with index<br />

values from Lowbound through Highbound.<br />

Example 2<br />

FOR Year := 1899 DOWN TO 1801 DO<br />

IF (Year MOD 4) = 0 THEN<br />

WRITELN(Year:4,' IS A LEAP YEAR');<br />

The DOWNTO form is used here to print a list of all the leap years<br />

the nineteenth century.<br />

in<br />

Example 3<br />

FOR I := 1 TO 10 DO<br />

FOR J := 1 TO 10 DO<br />

A[I,JJ := 0;<br />

This example shows how you can directly nest FOR loops. For each<br />

value of I, the system steps through all 10 values of J and assigns<br />

the value 0 to the appropriate array element.<br />

5-9

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

Saved successfully!

Ooh no, something went wrong!