25.07.2014 Views

VDM-10 Language Manual

VDM-10 Language Manual

VDM-10 Language Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13. Statements<br />

‘do’, statement ;<br />

set for loop = ‘for’, ‘all’, pattern, ‘in set’, expression,<br />

‘do’, statement ;<br />

index for loop = ‘for’, identifier, ‘=’, expression, ‘to’, expression,<br />

[ ‘by’, expression ], ‘do’, statement ;<br />

Semantics: There are three kinds of for-loop statements. The for-loop using an index is known<br />

from most high-level programming languages. In addition, there are two for-loops for<br />

traversing sets and sequences. These are especially useful if access to all elements from<br />

a set (or sequence) is needed one by one.<br />

An index for-loop statement has the form:<br />

✞<br />

✡✝<br />

for id = e1 to e2 by e3 do<br />

s<br />

where id is an identifier, e1 and e2 are integer expressions indicating the lower and upper<br />

bounds for the loop, e3 is an integer expression indicating the step size, and s is a statement<br />

where the identifier id can be used. It denotes the evaluation of the statement s as a sequence<br />

statement where the current context is extended with a binding of id. Thus, the first time<br />

s is evaluated id is bound to the value returned from the evaluation of the lower bound e1<br />

and so forth until the upper bound is reached ie. until s > e2 . Note that e1, e2 and e3<br />

are evaluated before entering the loop.<br />

A set for-loop statement has the form:<br />

✞<br />

✡✝<br />

for all e in set S do<br />

s<br />

where S is a set expression. The statement s is evaluated in the current environment extended<br />

with a binding of e to subsequent values from the set S.<br />

A sequence for-loop statement has the form:<br />

✞<br />

✡✝<br />

for e in l do<br />

s<br />

where l is a sequence expression. The statement s is evaluated in the current environment<br />

extended with a binding of e to subsequent values from the sequence l.<br />

<strong>10</strong>7<br />

✆<br />

✆<br />

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

Saved successfully!

Ooh no, something went wrong!